elastic_record 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ module ElasticRecord
2
+ class Index
3
+ module Manage
4
+ def create(index_name = name)
5
+ model.elastic_connection.create_index(index_name)
6
+
7
+ unless mapping.empty?
8
+ model.elastic_connection.update_mapping(mapping, index: index_name)
9
+ end
10
+ end
11
+
12
+ def delete(index_name = name)
13
+ model.elastic_connection.delete_index(index_name)
14
+ end
15
+
16
+ def alias
17
+ # alias_actions = {add: {index_name => pending_index_alias}}
18
+ # elastic_connection.alias_index(alias_actions)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ module ElasticRecord
2
+ class Index
3
+ module Mapping
4
+ def mapping=(mapping)
5
+ mapping.deep_merge!(mapping)
6
+ end
7
+
8
+ def mapping
9
+ @mapping ||= {
10
+ _source: {
11
+ enabled: false
12
+ },
13
+ properties: {
14
+ created_at: {type: "date", index: "not_analyzed", format: "dateOptionalTime"},
15
+ updated_at: {type: "date", index: "not_analyzed", format: "dateOptionalTime"}
16
+ },
17
+ dynamic_templates: [
18
+ {
19
+ no_string_analyzing: {
20
+ match: "*",
21
+ match_mapping_type: "string",
22
+ mapping: {
23
+ type: "string",
24
+ index: "not_analyzed"
25
+ }
26
+ }
27
+ }
28
+ ]
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'elastic_record/index/manage'
2
+ require 'elastic_record/index/mapping'
3
+
4
+ module ElasticRecord
5
+ class Index
6
+ include Manage
7
+ include Mapping
8
+
9
+ attr_accessor :model
10
+
11
+ def initialize(model)
12
+ @model = model
13
+ end
14
+
15
+ def name
16
+ model.model_name.collection
17
+ end
18
+
19
+ def type
20
+ model.model_name.element
21
+ end
22
+ end
23
+ end
@@ -9,13 +9,25 @@ module ElasticRecord
9
9
  end
10
10
 
11
11
  module ClassMethods
12
- def relation
12
+ def elastic_relation
13
13
  ElasticRecord::Relation.new(self, arelastic)
14
14
  end
15
15
 
16
16
  def arelastic
17
17
  @arelastic ||= Arelastic::Builders::Search.new
18
18
  end
19
+
20
+ def elastic_index
21
+ @elastic_index ||= ElasticRecord::Index.new(self)
22
+ end
23
+ end
24
+
25
+ def arelastic
26
+ self.class.arelastic
27
+ end
28
+
29
+ def elastic_index
30
+ self.class.elastic_index
19
31
  end
20
32
  end
21
33
  end
@@ -0,0 +1,6 @@
1
+ module ElasticRecord
2
+ class Relation
3
+ MULTI_VALUE_METHODS = [:extending, :filter, :facet, :order]
4
+ SINGLE_VALUE_METHODS = [:query, :limit, :offset]
5
+ end
6
+ end
@@ -1,13 +1,12 @@
1
- # require 'elastic_record/relation/delegation'
2
- # require 'elastic_record/relation/finder_methods'
3
- # require 'elastic_record/relation/merging'
4
- # require 'elastic_record/relation/search_methods'
1
+ require 'elastic_record/relation/value_methods'
2
+ require 'elastic_record/relation/batches'
3
+ require 'elastic_record/relation/delegation'
4
+ require 'elastic_record/relation/finder_methods'
5
+ require 'elastic_record/relation/merging'
6
+ require 'elastic_record/relation/search_methods'
5
7
 
6
8
  module ElasticRecord
7
9
  class Relation
8
- MULTI_VALUE_METHODS = [:extending, :filter, :facet, :order]
9
- SINGLE_VALUE_METHODS = [:query, :limit, :offset]
10
-
11
10
  include Batches, Delegation, FinderMethods, Merging, SearchMethods
12
11
 
13
12
  attr_reader :klass, :arelastic, :values
@@ -4,7 +4,7 @@ module ElasticRecord
4
4
  if current_elastic_search
5
5
  current_elastic_search.clone
6
6
  else
7
- relation
7
+ elastic_relation
8
8
  end
9
9
  end
10
10
 
@@ -3,15 +3,8 @@ require 'arelastic'
3
3
  module ElasticRecord
4
4
  autoload :Config, 'elastic_record/config'
5
5
  autoload :Connection, 'elastic_record/connection'
6
-
6
+ autoload :Index, 'elastic_record/index'
7
+ autoload :Model, 'elastic_record/model'
7
8
  autoload :Relation, 'elastic_record/relation'
8
- autoload :Batches, 'elastic_record/relation/batches'
9
- autoload :Delegation, 'elastic_record/relation/delegation'
10
- autoload :FinderMethods, 'elastic_record/relation/finder_methods'
11
- autoload :Merging, 'elastic_record/relation/merging'
12
- autoload :SearchMethods, 'elastic_record/relation/search_methods'
13
-
14
9
  autoload :Searching, 'elastic_record/searching'
15
-
16
- autoload :Model, 'elastic_record/model'
17
10
  end
@@ -4,7 +4,7 @@ class ElasticRecord::ConnectionTest < MiniTest::Spec
4
4
  def test_elastic_connection
5
5
  connection = Widget.elastic_connection
6
6
 
7
- assert_equal 'widget', connection.default_type
8
- assert_equal 'widgets', connection.default_index
7
+ assert_equal Widget.elastic_index.type, connection.default_type
8
+ assert_equal Widget.elastic_index.name, connection.default_index
9
9
  end
10
10
  end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class ElasticRecord::Index::ManageTest < MiniTest::Spec
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class ElasticRecord::Index::MappingTest < MiniTest::Spec
4
+ end
@@ -0,0 +1,10 @@
1
+ require 'helper'
2
+
3
+ class ElasticRecord::IndexTest < MiniTest::Spec
4
+ def test_elastic_connection
5
+ index = Widget.elastic_index
6
+
7
+ assert_equal 'widget', index.type
8
+ assert_equal 'widgets', index.name
9
+ end
10
+ end
@@ -1,10 +1,16 @@
1
1
  require 'helper'
2
2
 
3
3
  class ElasticRecord::ModelTest < MiniTest::Spec
4
- def test_relation
5
- relation = Widget.relation
4
+ def test_elastic_relation
5
+ relation = Widget.elastic_relation
6
6
 
7
7
  assert_equal Widget, relation.klass
8
8
  assert_equal Widget.arelastic, relation.arelastic
9
9
  end
10
+
11
+ def test_elastic_index
12
+ index = Widget.elastic_index
13
+
14
+ assert_equal Widget, index.model
15
+ end
10
16
  end
@@ -8,7 +8,7 @@ class ElasticRecord::Relation::BatchesTest < MiniTest::Spec
8
8
 
9
9
  def test_find_each
10
10
  results = []
11
- Widget.relation.find_each do |widget|
11
+ Widget.elastic_relation.find_each do |widget|
12
12
  results << widget.id
13
13
  end
14
14
  assert_equal ['5', '10', '15'].to_set, results.to_set
@@ -16,7 +16,7 @@ class ElasticRecord::Relation::BatchesTest < MiniTest::Spec
16
16
 
17
17
  def test_find_each_with_scope
18
18
  results = []
19
- Widget.relation.filter(color: %w(red blue)).find_each do |widget|
19
+ Widget.elastic_relation.filter(color: %w(red blue)).find_each do |widget|
20
20
  results << widget.id
21
21
  end
22
22
  assert_equal ['5', '10'].to_set, results.to_set
@@ -10,7 +10,7 @@ class ElasticRecord::Relation::DelegationTest < MiniTest::Spec
10
10
  Widget.elastic_connection.refresh
11
11
 
12
12
  records = []
13
- Widget.relation.each do |record|
13
+ Widget.elastic_relation.each do |record|
14
14
  records << record
15
15
  end
16
16
 
@@ -24,7 +24,7 @@ class ElasticRecord::Relation::DelegationTest < MiniTest::Spec
24
24
  end
25
25
  end
26
26
 
27
- result = model.relation.filter('foo' => 'bar').do_it
27
+ result = model.elastic_relation.filter('foo' => 'bar').do_it
28
28
 
29
29
  expected = {"query"=>{"constant_score"=>{"filter"=>{"term"=>{"foo"=>"bar"}}}}}
30
30
  assert_equal expected, result
@@ -7,28 +7,28 @@ class ElasticRecord::Relation::FinderMethodsTest < MiniTest::Spec
7
7
  end
8
8
 
9
9
  def test_find
10
- refute_nil Widget.relation.find('05')
11
- refute_nil Widget.relation.filter('color' => 'red').find('05')
12
- assert_nil Widget.relation.filter('color' => 'blue').find('05')
10
+ refute_nil Widget.elastic_relation.find('05')
11
+ refute_nil Widget.elastic_relation.filter('color' => 'red').find('05')
12
+ assert_nil Widget.elastic_relation.filter('color' => 'blue').find('05')
13
13
  end
14
14
 
15
15
  def test_first
16
- assert_equal '05', Widget.relation.first.id
17
- assert_equal '05', Widget.relation.filter('color' => 'red').first.id
18
- assert_equal '10', Widget.relation.filter('color' => 'blue').first.id
19
- assert_nil Widget.relation.filter('color' => 'green').first
16
+ assert_equal '05', Widget.elastic_relation.first.id
17
+ assert_equal '05', Widget.elastic_relation.filter('color' => 'red').first.id
18
+ assert_equal '10', Widget.elastic_relation.filter('color' => 'blue').first.id
19
+ assert_nil Widget.elastic_relation.filter('color' => 'green').first
20
20
  end
21
21
 
22
22
  def test_last
23
- assert_equal '10', Widget.relation.last.id
24
- assert_equal '05', Widget.relation.filter('color' => 'red').last.id
25
- assert_equal '10', Widget.relation.filter('color' => 'blue').last.id
26
- assert_nil Widget.relation.filter('color' => 'green').last
23
+ assert_equal '10', Widget.elastic_relation.last.id
24
+ assert_equal '05', Widget.elastic_relation.filter('color' => 'red').last.id
25
+ assert_equal '10', Widget.elastic_relation.filter('color' => 'blue').last.id
26
+ assert_nil Widget.elastic_relation.filter('color' => 'green').last
27
27
  end
28
28
 
29
29
  def test_all
30
- assert_equal 2, Widget.relation.all.size
31
- assert_equal 1, Widget.relation.filter('color' => 'red').all.size
30
+ assert_equal 2, Widget.elastic_relation.all.size
31
+ assert_equal 1, Widget.elastic_relation.filter('color' => 'red').all.size
32
32
  end
33
33
 
34
34
  private
@@ -2,8 +2,8 @@ require 'helper'
2
2
 
3
3
  class ElasticRecord::Relation::MergingTest < MiniTest::Spec
4
4
  def test_merge_single_values
5
- relation = Widget.relation.limit(5)
6
- other = Widget.relation.limit(10)
5
+ relation = Widget.elastic_relation.limit(5)
6
+ other = Widget.elastic_relation.limit(10)
7
7
 
8
8
  relation.merge! other
9
9
 
@@ -11,8 +11,8 @@ class ElasticRecord::Relation::MergingTest < MiniTest::Spec
11
11
  end
12
12
 
13
13
  def test_merge_multi_values
14
- relation = Widget.relation.filter(color: 'green')
15
- other = Widget.relation.filter(weight: 1.0)
14
+ relation = Widget.elastic_relation.filter(color: 'green')
15
+ other = Widget.elastic_relation.filter(weight: 1.0)
16
16
 
17
17
  relation.merge! other
18
18
 
@@ -152,6 +152,6 @@ class ElasticRecord::Relation::SearchMethodsTest < MiniTest::Spec
152
152
 
153
153
  private
154
154
  def relation
155
- @relation ||= Widget.relation
155
+ @relation ||= Widget.elastic_relation
156
156
  end
157
157
  end
@@ -7,39 +7,39 @@ class ElasticRecord::RelationTest < MiniTest::Spec
7
7
  end
8
8
 
9
9
  def test_to_hits
10
- assert Widget.relation.to_hits.is_a?(ElasticSearch::Api::Hits)
10
+ assert Widget.elastic_relation.to_hits.is_a?(ElasticSearch::Api::Hits)
11
11
  end
12
12
 
13
13
  def test_to_ids
14
- assert_equal ['5', '10'], Widget.relation.to_ids
14
+ assert_equal ['5', '10'], Widget.elastic_relation.to_ids
15
15
  end
16
16
 
17
17
  def test_to_a
18
- array = Widget.relation.to_a
18
+ array = Widget.elastic_relation.to_a
19
19
 
20
20
  assert_equal 2, array.size
21
21
  assert array.first.is_a?(Widget)
22
22
  end
23
23
 
24
24
  def test_count
25
- assert_equal 2, Widget.relation.count
25
+ assert_equal 2, Widget.elastic_relation.count
26
26
  end
27
27
 
28
28
  def test_facets
29
- facets = Widget.relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets
29
+ facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets
30
30
 
31
31
  assert_equal 2, facets['popular_colors']['total']
32
32
  end
33
33
 
34
34
  def test_equal
35
- assert(Widget.relation.filter(color: 'green') == Widget.relation.filter(color: 'green'))
36
- assert(Widget.relation.filter(color: 'green') != Widget.relation.filter(color: 'blue'))
35
+ assert(Widget.elastic_relation.filter(color: 'green') == Widget.elastic_relation.filter(color: 'green'))
36
+ assert(Widget.elastic_relation.filter(color: 'green') != Widget.elastic_relation.filter(color: 'blue'))
37
37
 
38
- assert(Widget.relation.filter(color: 'magenta') == [])
38
+ assert(Widget.elastic_relation.filter(color: 'magenta') == [])
39
39
  end
40
40
 
41
41
  def test_inspect
42
- assert_equal [].inspect, Widget.relation.filter(color: 'magenta').inspect
42
+ assert_equal [].inspect, Widget.elastic_relation.filter(color: 'magenta').inspect
43
43
  end
44
44
 
45
45
  private
@@ -6,7 +6,7 @@ class ElasticRecord::SearchingTest < MiniTest::Spec
6
6
  end
7
7
 
8
8
  def test_elastic_scope
9
- model = Class.new(Widget) do
9
+ model = Widget.anon do
10
10
  elastic_scope :by_color, ->(color) { elastic_search.filter(color: color) } do
11
11
  def negative_offset
12
12
  -offset_value
@@ -16,7 +16,7 @@ class ElasticRecord::SearchingTest < MiniTest::Spec
16
16
 
17
17
  relation = model.by_color('blue')
18
18
 
19
- assert_equal model.relation.filter(color: 'blue'), relation
19
+ assert_equal model.elastic_relation.filter(color: 'blue'), relation
20
20
  assert_equal -5, relation.offset(5).negative_offset
21
21
  end
22
22
  end
@@ -7,10 +7,19 @@ class Widget
7
7
  ids.map { |id| new(id: id, color: 'red') }
8
8
  end
9
9
 
10
+ def anon(&block)
11
+ Class.new(self) do
12
+ def self.name
13
+ 'Widget'
14
+ end
15
+
16
+ instance_eval(&block)
17
+ end
18
+ end
10
19
 
11
20
  def reset_index!
12
- elastic_connection.delete_index('widgets')
13
- elastic_connection.create_index('widgets')
21
+ elastic_index.delete
22
+ elastic_index.create
14
23
  elastic_connection.update_mapping(
15
24
  {
16
25
  properties: {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic
@@ -68,6 +68,9 @@ extra_rdoc_files:
68
68
  files:
69
69
  - lib/elastic_record/config.rb
70
70
  - lib/elastic_record/connection.rb
71
+ - lib/elastic_record/index/manage.rb
72
+ - lib/elastic_record/index/mapping.rb
73
+ - lib/elastic_record/index.rb
71
74
  - lib/elastic_record/model.rb
72
75
  - lib/elastic_record/orm/active_record.rb
73
76
  - lib/elastic_record/relation/batches.rb
@@ -75,11 +78,15 @@ files:
75
78
  - lib/elastic_record/relation/finder_methods.rb
76
79
  - lib/elastic_record/relation/merging.rb
77
80
  - lib/elastic_record/relation/search_methods.rb
81
+ - lib/elastic_record/relation/value_methods.rb
78
82
  - lib/elastic_record/relation.rb
79
83
  - lib/elastic_record/searching.rb
80
84
  - lib/elastic_record.rb
81
85
  - test/elastic_record/config_test.rb
82
86
  - test/elastic_record/connection_test.rb
87
+ - test/elastic_record/index/manage_test.rb
88
+ - test/elastic_record/index/mapping_test.rb
89
+ - test/elastic_record/index_test.rb
83
90
  - test/elastic_record/model_test.rb
84
91
  - test/elastic_record/relation/batches_test.rb
85
92
  - test/elastic_record/relation/delegation_test.rb