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.
- data/lib/elastic_record/index/manage.rb +22 -0
- data/lib/elastic_record/index/mapping.rb +33 -0
- data/lib/elastic_record/index.rb +23 -0
- data/lib/elastic_record/model.rb +13 -1
- data/lib/elastic_record/relation/value_methods.rb +6 -0
- data/lib/elastic_record/relation.rb +6 -7
- data/lib/elastic_record/searching.rb +1 -1
- data/lib/elastic_record.rb +2 -9
- data/test/elastic_record/connection_test.rb +2 -2
- data/test/elastic_record/index/manage_test.rb +4 -0
- data/test/elastic_record/index/mapping_test.rb +4 -0
- data/test/elastic_record/index_test.rb +10 -0
- data/test/elastic_record/model_test.rb +8 -2
- data/test/elastic_record/relation/batches_test.rb +2 -2
- data/test/elastic_record/relation/delegation_test.rb +2 -2
- data/test/elastic_record/relation/finder_methods_test.rb +13 -13
- data/test/elastic_record/relation/merging_test.rb +4 -4
- data/test/elastic_record/relation/search_methods_test.rb +1 -1
- data/test/elastic_record/relation_test.rb +9 -9
- data/test/elastic_record/searching_test.rb +2 -2
- data/test/support/widget.rb +11 -2
- metadata +9 -2
@@ -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
|
data/lib/elastic_record/model.rb
CHANGED
@@ -9,13 +9,25 @@ module ElasticRecord
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def
|
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
|
@@ -1,13 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
data/lib/elastic_record.rb
CHANGED
@@ -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
|
8
|
-
assert_equal
|
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
|
@@ -1,10 +1,16 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class ElasticRecord::ModelTest < MiniTest::Spec
|
4
|
-
def
|
5
|
-
relation = Widget.
|
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.
|
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.
|
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.
|
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.
|
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.
|
11
|
-
refute_nil Widget.
|
12
|
-
assert_nil Widget.
|
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.
|
17
|
-
assert_equal '05', Widget.
|
18
|
-
assert_equal '10', Widget.
|
19
|
-
assert_nil Widget.
|
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.
|
24
|
-
assert_equal '05', Widget.
|
25
|
-
assert_equal '10', Widget.
|
26
|
-
assert_nil Widget.
|
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.
|
31
|
-
assert_equal 1, Widget.
|
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.
|
6
|
-
other = Widget.
|
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.
|
15
|
-
other = Widget.
|
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
|
|
@@ -7,39 +7,39 @@ class ElasticRecord::RelationTest < MiniTest::Spec
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_to_hits
|
10
|
-
assert Widget.
|
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.
|
14
|
+
assert_equal ['5', '10'], Widget.elastic_relation.to_ids
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_to_a
|
18
|
-
array = Widget.
|
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.
|
25
|
+
assert_equal 2, Widget.elastic_relation.count
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_facets
|
29
|
-
facets = Widget.
|
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.
|
36
|
-
assert(Widget.
|
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.
|
38
|
+
assert(Widget.elastic_relation.filter(color: 'magenta') == [])
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_inspect
|
42
|
-
assert_equal [].inspect, Widget.
|
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 =
|
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.
|
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
|
data/test/support/widget.rb
CHANGED
@@ -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
|
-
|
13
|
-
|
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.
|
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-
|
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
|