elastic_record 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record.rb +2 -0
- data/lib/elastic_record/index.rb +33 -9
- data/lib/elastic_record/index/manage.rb +3 -4
- data/lib/elastic_record/index/percolator.rb +31 -0
- data/lib/elastic_record/relation.rb +4 -0
- data/lib/elastic_record/relation/batches.rb +18 -16
- data/lib/elastic_record/relation/delegation.rb +18 -16
- data/lib/elastic_record/relation/finder_methods.rb +18 -16
- data/lib/elastic_record/relation/merging.rb +26 -24
- data/lib/elastic_record/relation/search_methods.rb +170 -168
- data/test/elastic_record/index/percolator_test.rb +22 -0
- data/test/elastic_record/relation_test.rb +25 -10
- data/test/helper.rb +0 -3
- metadata +4 -2
data/elastic_record.gemspec
CHANGED
data/lib/elastic_record.rb
CHANGED
data/lib/elastic_record/index.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
require 'elastic_record/index/documents'
|
2
2
|
require 'elastic_record/index/manage'
|
3
3
|
require 'elastic_record/index/mapping'
|
4
|
+
require 'elastic_record/index/percolator'
|
5
|
+
require 'net/http'
|
4
6
|
|
5
7
|
module ElasticRecord
|
6
8
|
class Index
|
7
9
|
include Documents
|
8
10
|
include Manage
|
9
11
|
include Mapping
|
12
|
+
include Percolator
|
10
13
|
|
11
14
|
attr_accessor :model
|
12
15
|
attr_accessor :disabled
|
@@ -32,25 +35,46 @@ module ElasticRecord
|
|
32
35
|
@disabled = false
|
33
36
|
end
|
34
37
|
|
35
|
-
private
|
38
|
+
# private
|
36
39
|
def new_index_name
|
37
40
|
"#{alias_name}_#{Time.now.to_i}"
|
38
41
|
end
|
39
42
|
|
40
|
-
def
|
41
|
-
|
43
|
+
def head(path)
|
44
|
+
http_request(Net::HTTP::Head, path).code
|
42
45
|
end
|
43
46
|
|
44
|
-
def
|
45
|
-
|
47
|
+
def json_get(path, json = nil)
|
48
|
+
json_request Net::HTTP::Get, path, json
|
46
49
|
end
|
47
50
|
|
48
|
-
def
|
49
|
-
|
51
|
+
def json_post(path, json = nil)
|
52
|
+
json_request Net::HTTP::Post, path, json
|
50
53
|
end
|
51
54
|
|
52
|
-
def
|
53
|
-
|
55
|
+
def json_put(path, json = nil)
|
56
|
+
json_request Net::HTTP::Put, path, json
|
57
|
+
end
|
58
|
+
|
59
|
+
def json_delete(path, json = nil)
|
60
|
+
json_request Net::HTTP::Delete, path, json
|
61
|
+
end
|
62
|
+
|
63
|
+
def json_request(request_klass, path, json)
|
64
|
+
body = json ? ActiveSupport::JSON.encode(json) : nil
|
65
|
+
response = http_request(request_klass, path, body)
|
66
|
+
json = ActiveSupport::JSON.decode response.body
|
67
|
+
|
68
|
+
raise json['error'] if json['error']
|
69
|
+
|
70
|
+
json
|
71
|
+
end
|
72
|
+
|
73
|
+
def http_request(request_klass, path, body = nil)
|
74
|
+
request = request_klass.new(path)
|
75
|
+
request.body = body
|
76
|
+
|
77
|
+
http.request(request)
|
54
78
|
end
|
55
79
|
|
56
80
|
def connection
|
@@ -8,13 +8,13 @@ module ElasticRecord
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def create(index_name = new_index_name)
|
11
|
-
|
11
|
+
json_put "/#{index_name}"
|
12
12
|
update_mapping(index_name)
|
13
13
|
index_name
|
14
14
|
end
|
15
15
|
|
16
16
|
def delete(index_name)
|
17
|
-
|
17
|
+
json_delete "/#{index_name}"
|
18
18
|
end
|
19
19
|
|
20
20
|
def delete_all
|
@@ -24,7 +24,7 @@ module ElasticRecord
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def exists?(index_name)
|
27
|
-
|
27
|
+
head("/#{index_name}") == '200'
|
28
28
|
end
|
29
29
|
|
30
30
|
def deploy(index_name)
|
@@ -50,7 +50,6 @@ module ElasticRecord
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def update_mapping(index_name)
|
53
|
-
# connection.update_mapping(mapping, index: index_name)
|
54
53
|
json_put "/#{index_name}/#{type}/_mapping", type => mapping
|
55
54
|
end
|
56
55
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ElasticRecord
|
2
|
+
class Index
|
3
|
+
module Percolator
|
4
|
+
def create_percolator(name, elastic_query)
|
5
|
+
unless exists? percolator_index_name
|
6
|
+
create percolator_index_name
|
7
|
+
else
|
8
|
+
update_mapping percolator_index_name
|
9
|
+
end
|
10
|
+
|
11
|
+
json_put "/_percolator/#{percolator_index_name}/#{name}", elastic_query
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete_percolator(name)
|
15
|
+
json_delete "/_percolator/#{percolator_index_name}/#{name}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def percolator_exists?(name)
|
19
|
+
json_get("/_percolator/#{percolator_index_name}/#{name}")['exists']
|
20
|
+
end
|
21
|
+
|
22
|
+
def percolate(document)
|
23
|
+
json_get("/#{percolator_index_name}/#{type}/_percolate", 'doc' => document)['matches']
|
24
|
+
end
|
25
|
+
|
26
|
+
def percolator_index_name
|
27
|
+
@percolator_index_name ||= "percolate_#{alias_name}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,26 +1,28 @@
|
|
1
1
|
module ElasticRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Relation
|
3
|
+
module Batches
|
4
|
+
def find_each
|
5
|
+
find_in_batches do |records|
|
6
|
+
records.each { |record| yield record }
|
7
|
+
end
|
6
8
|
end
|
7
|
-
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def find_in_batches(options = {})
|
11
|
+
options[:size] ||= 100
|
12
|
+
options[:scroll] ||= '20m'
|
13
|
+
options[:search_type] = 'scan'
|
13
14
|
|
14
|
-
|
15
|
+
hits = klass.elastic_connection.search(as_elastic, options)
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
klass.elastic_connection.scroll(hits.scroll_id, scroll: options[:scroll], ids_only: true) do |hits|
|
18
|
+
yield klass.find(hits.to_a)
|
19
|
+
end
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def reindex
|
23
|
+
relation.find_in_batches do |batch|
|
24
|
+
elastic_index.bulk_add(batch)
|
25
|
+
end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
module ElasticRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Relation
|
3
|
+
module Delegation
|
4
|
+
def to_ary
|
5
|
+
to_a.to_ary
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def include?(obj)
|
9
|
+
to_a.include?(obj)
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
private
|
13
|
+
def method_missing(method, *args, &block)
|
14
|
+
if klass.respond_to?(method)
|
15
|
+
scoping { klass.send(method, *args, &block) }
|
16
|
+
elsif Array.method_defined?(method)
|
17
|
+
to_a.send(method, *args, &block)
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
19
21
|
end
|
20
|
-
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,25 +1,27 @@
|
|
1
1
|
module ElasticRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Relation
|
3
|
+
module FinderMethods
|
4
|
+
def find(id)
|
5
|
+
filter(arelastic.filter.ids(id)).to_a.first
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def first
|
9
|
+
find_one order('_uid')
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def last
|
13
|
+
find_one order('color' => 'reverse')
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def all
|
17
|
+
to_a
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
+
private
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
def find_one(relation)
|
23
|
+
relation.limit(1).to_a.first
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -1,34 +1,36 @@
|
|
1
1
|
module ElasticRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def merge(other)
|
8
|
-
clone.merge!(other)
|
9
|
-
end
|
2
|
+
class Relation
|
3
|
+
module Merging
|
4
|
+
def merge!(other)
|
5
|
+
Merger.new(self, other).merge
|
6
|
+
end
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
def merge(other)
|
9
|
+
clone.merge!(other)
|
10
|
+
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
12
|
+
private
|
13
|
+
class Merger
|
14
|
+
attr_accessor :relation, :values
|
19
15
|
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def initialize(relation, other)
|
17
|
+
@relation = relation
|
18
|
+
@values = other.values
|
19
|
+
end
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
value = values[name]
|
27
|
-
relation.send("#{name}!", value) unless value.blank?
|
21
|
+
def normal_values
|
22
|
+
Relation::MULTI_VALUE_METHODS + Relation::SINGLE_VALUE_METHODS
|
28
23
|
end
|
29
24
|
|
30
|
-
|
25
|
+
def merge
|
26
|
+
normal_values.each do |name|
|
27
|
+
value = values[name]
|
28
|
+
relation.send("#{name}!", value) unless value.blank?
|
29
|
+
end
|
30
|
+
|
31
|
+
relation
|
32
|
+
end
|
31
33
|
end
|
32
|
-
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
@@ -1,213 +1,215 @@
|
|
1
1
|
module ElasticRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
2
|
+
class Relation
|
3
|
+
module SearchMethods
|
4
|
+
Relation::MULTI_VALUE_METHODS.each do |name|
|
5
|
+
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
6
|
+
def #{name}_values # def filter_values
|
7
|
+
@values[:#{name}] || [] # @values[:filter] || []
|
8
|
+
end # end
|
9
|
+
#
|
10
|
+
def #{name}_values=(values) # def filter_values=(values)
|
11
|
+
@values[:#{name}] = values # @values[:filter] = values
|
12
|
+
end # end
|
13
|
+
CODE
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
Relation::SINGLE_VALUE_METHODS.each do |name|
|
17
|
+
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
18
|
+
def #{name}_value # def offset_value
|
19
|
+
@values[:#{name}] # @values[:offset]
|
20
|
+
end # end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def #{name}_value=(value) # def offset_value=(value)
|
23
|
+
@values[:#{name}] = value # @values[:offset] = value
|
24
|
+
end # end
|
25
|
+
CODE
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
def query(value)
|
33
|
-
clone.query! value
|
34
|
-
end
|
28
|
+
def query!(value)
|
29
|
+
self.query_value = value
|
30
|
+
self
|
31
|
+
end
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
33
|
+
def query(value)
|
34
|
+
clone.query! value
|
35
|
+
end
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
def filter!(*args)
|
38
|
+
self.filter_values += args.flatten
|
39
|
+
self
|
40
|
+
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
42
|
+
def filter(*args)
|
43
|
+
clone.filter!(*args)
|
44
|
+
end
|
49
45
|
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
def limit!(value)
|
47
|
+
self.limit_value = value
|
48
|
+
self
|
49
|
+
end
|
53
50
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
51
|
+
def limit(value)
|
52
|
+
clone.limit!(value)
|
53
|
+
end
|
58
54
|
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
def offset!(value)
|
56
|
+
self.offset_value = value
|
57
|
+
self
|
58
|
+
end
|
62
59
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
60
|
+
def offset(value)
|
61
|
+
clone.offset! value
|
62
|
+
end
|
67
63
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
else
|
72
|
-
clone.select! *args
|
64
|
+
def select!(*args)
|
65
|
+
self.select_values += args.flatten
|
66
|
+
self
|
73
67
|
end
|
74
|
-
end
|
75
68
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
69
|
+
def select(*args, &block)
|
70
|
+
if block_given?
|
71
|
+
to_a.select(&block)
|
72
|
+
else
|
73
|
+
clone.select! *args
|
74
|
+
end
|
81
75
|
end
|
82
76
|
|
83
|
-
|
84
|
-
|
77
|
+
def facet!(name_or_facet, options = {})
|
78
|
+
if name_or_facet.is_a?(String)
|
79
|
+
self.facet_values += [arelastic.facet[name_or_facet].terms(name_or_facet, options)]
|
80
|
+
else
|
81
|
+
self.facet_values += [name_or_facet]
|
82
|
+
end
|
85
83
|
|
86
|
-
|
87
|
-
|
88
|
-
end
|
84
|
+
self
|
85
|
+
end
|
89
86
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
end
|
87
|
+
def facet(facet_or_name, options = {})
|
88
|
+
clone.facet! facet_or_name, options = {}
|
89
|
+
end
|
94
90
|
|
95
|
-
|
96
|
-
|
97
|
-
|
91
|
+
def order!(*args)
|
92
|
+
self.order_values += args.flatten
|
93
|
+
self
|
94
|
+
end
|
98
95
|
|
99
|
-
|
100
|
-
|
96
|
+
def order(*args)
|
97
|
+
clone.order! *args
|
98
|
+
end
|
101
99
|
|
102
|
-
|
103
|
-
|
100
|
+
def extending!(*modules, &block)
|
101
|
+
modules << Module.new(&block) if block_given?
|
104
102
|
|
105
|
-
|
106
|
-
|
103
|
+
self.extending_values += modules.flatten
|
104
|
+
extend(*extending_values)
|
107
105
|
|
108
|
-
|
109
|
-
|
110
|
-
end
|
106
|
+
self
|
107
|
+
end
|
111
108
|
|
112
|
-
|
113
|
-
|
114
|
-
|
109
|
+
def extending(*modules, &block)
|
110
|
+
clone.extending!(*modules, &block)
|
111
|
+
end
|
115
112
|
|
116
|
-
|
117
|
-
|
118
|
-
searches = [
|
119
|
-
build_query_and_filter(query_value, filter_values),
|
120
|
-
build_limit(limit_value),
|
121
|
-
build_offset(offset_value),
|
122
|
-
build_facets(facet_values),
|
123
|
-
build_orders(order_values)
|
124
|
-
].compact
|
125
|
-
|
126
|
-
Arelastic::Nodes::HashGroup.new searches
|
127
|
-
end
|
128
|
-
|
129
|
-
def build_query_and_filter(query, filters)
|
130
|
-
query = build_query(query)
|
131
|
-
filter = build_filter(filters)
|
132
|
-
if query && filter
|
133
|
-
arelastic.query.filtered(query, filter)
|
134
|
-
elsif query
|
135
|
-
query
|
136
|
-
elsif filter
|
137
|
-
arelastic.query.constant_score(filter)
|
138
|
-
else
|
139
|
-
arelastic.query.match_all
|
140
|
-
end
|
113
|
+
def as_elastic
|
114
|
+
build_search.as_elastic
|
141
115
|
end
|
142
116
|
|
143
|
-
|
144
|
-
|
145
|
-
|
117
|
+
private
|
118
|
+
def build_search
|
119
|
+
searches = [
|
120
|
+
build_query_and_filter(query_value, filter_values),
|
121
|
+
build_limit(limit_value),
|
122
|
+
build_offset(offset_value),
|
123
|
+
build_facets(facet_values),
|
124
|
+
build_orders(order_values)
|
125
|
+
].compact
|
126
|
+
|
127
|
+
Arelastic::Nodes::HashGroup.new searches
|
146
128
|
end
|
147
129
|
|
148
|
-
|
149
|
-
|
130
|
+
def build_query_and_filter(query, filters)
|
131
|
+
query = build_query(query)
|
132
|
+
filter = build_filter(filters)
|
133
|
+
if query && filter
|
134
|
+
arelastic.query.filtered(query, filter)
|
135
|
+
elsif query
|
136
|
+
query
|
137
|
+
elsif filter
|
138
|
+
arelastic.query.constant_score(filter)
|
139
|
+
else
|
140
|
+
arelastic.query.match_all
|
141
|
+
end
|
150
142
|
end
|
151
|
-
end
|
152
143
|
|
153
|
-
|
154
|
-
|
144
|
+
def build_query(query)
|
145
|
+
if query.is_a?(String)
|
146
|
+
query = Arelastic::Queries::QueryString.new query
|
147
|
+
end
|
155
148
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
149
|
+
if query
|
150
|
+
Arelastic::Searches::Query.new query
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def build_filter(filters)
|
155
|
+
nodes = []
|
156
|
+
|
157
|
+
filters.map do |filter|
|
158
|
+
if filter.is_a?(Arelastic::Filters::Filter)
|
159
|
+
nodes << filter
|
160
|
+
else
|
161
|
+
filter.each do |field, terms|
|
162
|
+
case terms
|
163
|
+
when Array, Range
|
164
|
+
nodes << arelastic[field].in(terms)
|
165
|
+
when Hash
|
166
|
+
nodes << {field => terms}
|
167
|
+
else
|
168
|
+
nodes << arelastic[field].eq(terms)
|
169
|
+
end
|
168
170
|
end
|
169
171
|
end
|
170
172
|
end
|
171
|
-
end
|
172
173
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
if nodes.size == 1
|
175
|
+
Arelastic::Searches::Filter.new nodes.first
|
176
|
+
elsif nodes.size > 1
|
177
|
+
Arelastic::Searches::Filter.new Arelastic::Filters::And.new(nodes)
|
178
|
+
end
|
177
179
|
end
|
178
|
-
end
|
179
180
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
181
|
+
# def normalize_value(value)
|
182
|
+
# case value
|
183
|
+
# when true
|
184
|
+
# "T"
|
185
|
+
# when false
|
186
|
+
# "F"
|
187
|
+
# when Time
|
188
|
+
# value.iso8601
|
189
|
+
# else
|
190
|
+
# value
|
191
|
+
# end
|
192
|
+
# end
|
193
|
+
|
194
|
+
def build_limit(limit)
|
195
|
+
if limit
|
196
|
+
Arelastic::Searches::Size.new(limit)
|
197
|
+
end
|
196
198
|
end
|
197
|
-
end
|
198
199
|
|
199
|
-
|
200
|
-
|
201
|
-
|
200
|
+
def build_offset(offset)
|
201
|
+
if offset
|
202
|
+
Arelastic::Searches::From.new(offset)
|
203
|
+
end
|
202
204
|
end
|
203
|
-
end
|
204
205
|
|
205
|
-
|
206
|
-
|
207
|
-
|
206
|
+
def build_facets(facets)
|
207
|
+
Arelastic::Searches::Facets.new(facets) unless facets.empty?
|
208
|
+
end
|
208
209
|
|
209
|
-
|
210
|
-
|
211
|
-
|
210
|
+
def build_orders(orders)
|
211
|
+
Arelastic::Searches::Sort.new(orders) unless orders.empty?
|
212
|
+
end
|
213
|
+
end
|
212
214
|
end
|
213
215
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ElasticRecord::Index::PercolatorTest < MiniTest::Spec
|
4
|
+
def test_create_percolator
|
5
|
+
index.delete(index.percolator_index_name) if index.exists?(index.percolator_index_name)
|
6
|
+
|
7
|
+
index.create_percolator('green', 'color' => 'green')
|
8
|
+
|
9
|
+
assert index.exists?(index.percolator_index_name)
|
10
|
+
assert index.percolator_exists?('green')
|
11
|
+
refute index.percolator_exists?('blue')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_percolator_index_name
|
15
|
+
assert_equal 'percolate_widgets', index.percolator_index_name
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def index
|
20
|
+
Widget.elastic_index
|
21
|
+
end
|
22
|
+
end
|
@@ -3,7 +3,28 @@ require 'helper'
|
|
3
3
|
class ElasticRecord::RelationTest < MiniTest::Spec
|
4
4
|
def setup
|
5
5
|
Widget.elastic_index.reset
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_count
|
6
9
|
create_widgets
|
10
|
+
|
11
|
+
assert_equal 2, Widget.elastic_relation.count
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_facets
|
15
|
+
create_widgets
|
16
|
+
|
17
|
+
facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets
|
18
|
+
|
19
|
+
assert_equal 2, facets['popular_colors']['total']
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_percolate
|
23
|
+
Widget.elastic_relation.filter(color: 'green').create_percolator('green')
|
24
|
+
Widget.elastic_relation.filter(color: 'blue').create_percolator('blue')
|
25
|
+
widget = Widget.new(color: 'green')
|
26
|
+
|
27
|
+
assert_equal ['green'], Widget.elastic_index.percolate(widget.as_search)
|
7
28
|
end
|
8
29
|
|
9
30
|
def test_to_hits
|
@@ -11,27 +32,21 @@ class ElasticRecord::RelationTest < MiniTest::Spec
|
|
11
32
|
end
|
12
33
|
|
13
34
|
def test_to_ids
|
35
|
+
create_widgets
|
14
36
|
assert_equal ['5', '10'], Widget.elastic_relation.to_ids
|
15
37
|
end
|
16
38
|
|
17
39
|
def test_to_a
|
40
|
+
create_widgets
|
18
41
|
array = Widget.elastic_relation.to_a
|
19
42
|
|
20
43
|
assert_equal 2, array.size
|
21
44
|
assert array.first.is_a?(Widget)
|
22
45
|
end
|
23
46
|
|
24
|
-
def test_count
|
25
|
-
assert_equal 2, Widget.elastic_relation.count
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_facets
|
29
|
-
facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets
|
30
|
-
|
31
|
-
assert_equal 2, facets['popular_colors']['total']
|
32
|
-
end
|
33
|
-
|
34
47
|
def test_equal
|
48
|
+
create_widgets
|
49
|
+
|
35
50
|
assert(Widget.elastic_relation.filter(color: 'green') == Widget.elastic_relation.filter(color: 'green'))
|
36
51
|
assert(Widget.elastic_relation.filter(color: 'green') != Widget.elastic_relation.filter(color: 'blue'))
|
37
52
|
|
data/test/helper.rb
CHANGED
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.5.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-09-
|
12
|
+
date: 2012-09-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: arelastic
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/elastic_record/index/documents.rb
|
81
81
|
- lib/elastic_record/index/manage.rb
|
82
82
|
- lib/elastic_record/index/mapping.rb
|
83
|
+
- lib/elastic_record/index/percolator.rb
|
83
84
|
- lib/elastic_record/model.rb
|
84
85
|
- lib/elastic_record/orm/active_record.rb
|
85
86
|
- lib/elastic_record/railtie.rb
|
@@ -98,6 +99,7 @@ files:
|
|
98
99
|
- test/elastic_record/index/documents_test.rb
|
99
100
|
- test/elastic_record/index/manage_test.rb
|
100
101
|
- test/elastic_record/index/mapping_test.rb
|
102
|
+
- test/elastic_record/index/percolator_test.rb
|
101
103
|
- test/elastic_record/index_test.rb
|
102
104
|
- test/elastic_record/model_test.rb
|
103
105
|
- test/elastic_record/relation/batches_test.rb
|