elastic_queue 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/elastic_queue/filters.rb +10 -1
- data/lib/elastic_queue/percolation.rb +14 -8
- data/lib/elastic_queue/query.rb +31 -4
- data/lib/elastic_queue/query_options.rb +7 -2
- data/lib/elastic_queue/results.rb +1 -0
- data/lib/elastic_queue/version.rb +1 -1
- metadata +1 -2
- data/lib/elastic_queue/search.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 874af79f54d54bd97ed1bc58b7f3c45a66c8d915
|
4
|
+
data.tar.gz: 99fea70408e60937b93936fdf7512cbcc64a33dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bee5165a495abd8785135c1eb8c45ad5d02ee9057183ce47db064582b38c7a9063a042aef642e3347f5dce6c02444fc593b0a50b0b6bf70275e9ee856a8de713
|
7
|
+
data.tar.gz: f754e63c2d2759ef31817a142ff33dc8c883a919aec2bd80980fbfe355f04ddb2ec449c202b208b97ef60ef0f10f39ae16001c3a4fa173fdf1bb543a4266493e
|
@@ -17,13 +17,17 @@ module ElasticQueue
|
|
17
17
|
elsif value.is_a? Hash
|
18
18
|
# date?
|
19
19
|
time_filter(key, value)
|
20
|
+
elsif value.nil?
|
21
|
+
null_filter(key, value)
|
20
22
|
else
|
21
23
|
term_filter(key, value)
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
def or_filter(term, values)
|
26
|
-
|
28
|
+
# flatten here because ranges return arrays
|
29
|
+
conditions = values.map { |v| option_to_filter(term, v) }.flatten
|
30
|
+
{ or: conditions }
|
27
31
|
end
|
28
32
|
|
29
33
|
def term_filter(term, value)
|
@@ -42,5 +46,10 @@ module ElasticQueue
|
|
42
46
|
def range_filter(term, value, comparator)
|
43
47
|
{ range: { term => { comparator => value } } }
|
44
48
|
end
|
49
|
+
|
50
|
+
def null_filter(term, value)
|
51
|
+
{ missing: { field: term, existence: true, null_value: true } }
|
52
|
+
end
|
53
|
+
|
45
54
|
end
|
46
55
|
end
|
@@ -5,13 +5,13 @@ module ElasticQueue
|
|
5
5
|
module ClassMethods
|
6
6
|
def percolator_queries
|
7
7
|
queries = {}
|
8
|
-
search = search_client.search index: '_percolator', body: {
|
9
|
-
search['hits']['hits'].each
|
8
|
+
search = search_client.search index: '_percolator', body: { query: { match_all: {} } }, size: 1000
|
9
|
+
search['hits']['hits'].each { |hit| queries[hit['_id']] = hit['_source'] }
|
10
10
|
queries
|
11
11
|
end
|
12
12
|
|
13
13
|
def reverse_search(model)
|
14
|
-
percolation = search_client.percolate index: index_name, body: {
|
14
|
+
percolation = search_client.percolate index: index_name, body: { doc: model.indexed_for_queue }
|
15
15
|
percolation['matches']
|
16
16
|
end
|
17
17
|
|
@@ -22,14 +22,20 @@ module ElasticQueue
|
|
22
22
|
def register_percolator_query(name, body)
|
23
23
|
search_client.index index: '_percolator', type: index_name, id: name, body: body
|
24
24
|
end
|
25
|
+
|
26
|
+
def dynamically_percolate(model, percolator_body)
|
27
|
+
search_id = SecureRandom.uuid
|
28
|
+
# create a dynamic percolator with the query params, index our model in it and see if there are any matches
|
29
|
+
search_client.index index: '_percolator', type: 'dynamic_percolator', id: search_id, body: percolator_body, refresh: true
|
30
|
+
search = search_client.percolate index: 'dynamic_percolator', body: { doc: model.indexed_for_queue, query: { term: { _id: search_id } } }
|
31
|
+
search_client.delete index: '_percolator', type: 'dynamic_percolator', id: search_id
|
32
|
+
search
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
26
36
|
|
27
37
|
def in_queue?(model)
|
28
|
-
|
29
|
-
search_id = SecureRandom.uuid
|
30
|
-
self.class.search_client.index index: '_percolator', type: 'dynamic_percolator', id: search_id, body: @query.percolator_body, refresh: true
|
31
|
-
search = self.class.search_client.percolate index: 'dynamic_percolator', body: { 'doc' => model.indexed_for_queue, 'query' => { 'term' => { '_id' => search_id } } }
|
32
|
-
self.class.search_client.delete index: '_percolator', type: 'dynamic_percolator', id: search_id
|
38
|
+
search = self.class.dynamically_percolate(model, @query.percolator_body)
|
33
39
|
search['matches'].length == 1
|
34
40
|
end
|
35
41
|
|
data/lib/elastic_queue/query.rb
CHANGED
@@ -26,6 +26,15 @@ module ElasticQueue
|
|
26
26
|
@options.sorts
|
27
27
|
end
|
28
28
|
|
29
|
+
def search(string)
|
30
|
+
@options.add_search(string)
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def searches
|
35
|
+
@options.search
|
36
|
+
end
|
37
|
+
|
29
38
|
def body
|
30
39
|
@options.body
|
31
40
|
end
|
@@ -47,23 +56,41 @@ module ElasticQueue
|
|
47
56
|
@results ||= Results.new(@queue, execute, @options)
|
48
57
|
end
|
49
58
|
|
59
|
+
# return just the ids of the records (useful when combined with SQL queries)
|
60
|
+
def ids
|
61
|
+
results = execute
|
62
|
+
results[:hits][:hits].map { |h| h[:_source][:id] }
|
63
|
+
end
|
64
|
+
|
50
65
|
def count
|
51
66
|
res = execute(count: true)
|
52
67
|
res[:hits][:total].to_i
|
53
68
|
end
|
54
69
|
|
55
70
|
def execute(count: false)
|
56
|
-
search_type = count ? 'count' : 'query_then_fetch'
|
57
71
|
begin
|
58
|
-
search =
|
59
|
-
|
60
|
-
# search = substitute_page(opts, search) if !count && opts[:page_substitution_ok] && search['hits']['hits'].length == 0 && search['hits']['total'] != 0
|
72
|
+
search = execute_query(count: false)
|
73
|
+
search = substitute_page(search) if !count && search['hits']['hits'].length == 0 && search['hits']['total'] != 0
|
61
74
|
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
|
62
75
|
search = failed_search
|
63
76
|
end
|
64
77
|
search.with_indifferent_access
|
65
78
|
end
|
66
79
|
|
80
|
+
private
|
81
|
+
|
82
|
+
def execute_query(count: false)
|
83
|
+
search_type = count ? 'count' : 'query_then_fetch'
|
84
|
+
@queue.search_client.search index: @queue.index_name, body: body, search_type: search_type, from: @options.from, size: @options.per_page
|
85
|
+
end
|
86
|
+
|
87
|
+
def substitute_page(search)
|
88
|
+
total_hits = search['hits']['total'].to_i
|
89
|
+
per_page = @options.per_page
|
90
|
+
@options.page = (total_hits / per_page.to_f).ceil
|
91
|
+
execute_query
|
92
|
+
end
|
93
|
+
|
67
94
|
def failed_search
|
68
95
|
{ page: 0, hits: { hits: [], total: 0 } }
|
69
96
|
end
|
@@ -6,7 +6,7 @@ module ElasticQueue
|
|
6
6
|
include Filters
|
7
7
|
include Sorts
|
8
8
|
|
9
|
-
attr_reader :filters, :sorts, :page
|
9
|
+
attr_reader :filters, :sorts, :page, :search
|
10
10
|
attr_accessor :per_page
|
11
11
|
|
12
12
|
def initialize(options = {})
|
@@ -25,6 +25,10 @@ module ElasticQueue
|
|
25
25
|
@sorts += options_to_sorts(options)
|
26
26
|
end
|
27
27
|
|
28
|
+
def add_search(string)
|
29
|
+
@search = string
|
30
|
+
end
|
31
|
+
|
28
32
|
def from
|
29
33
|
(page - 1) * per_page
|
30
34
|
end
|
@@ -37,13 +41,14 @@ module ElasticQueue
|
|
37
41
|
b = {}
|
38
42
|
b[:filter] = @filters unless @filters[:and].blank?
|
39
43
|
b[:sort] = @sorts unless @sorts.blank?
|
44
|
+
b[:query] = { query_string: { query: @search } } unless @search.blank?
|
40
45
|
b
|
41
46
|
end
|
42
47
|
|
43
48
|
def percolator_body
|
44
49
|
b = {}
|
45
50
|
b[:filter] = @filters unless @filters[:and].blank?
|
46
|
-
{
|
51
|
+
{ query: { constant_score: b } }
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
@@ -5,6 +5,7 @@ module ElasticQueue
|
|
5
5
|
attr_reader :paginate
|
6
6
|
|
7
7
|
delegate :empty?, :each, :total_entries, :total_pages, :current_page, to: :paginate
|
8
|
+
|
8
9
|
def initialize(queue, search_results, query_options)
|
9
10
|
@queue = queue
|
10
11
|
@instantiated_queue_items = instantiate_queue_items(search_results)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruth Thompson
|
@@ -135,7 +135,6 @@ files:
|
|
135
135
|
- lib/elastic_queue/query_options.rb
|
136
136
|
- lib/elastic_queue/queueable.rb
|
137
137
|
- lib/elastic_queue/results.rb
|
138
|
-
- lib/elastic_queue/search.rb
|
139
138
|
- lib/elastic_queue/sorts.rb
|
140
139
|
- lib/elastic_queue/version.rb
|
141
140
|
- spec/factories.rb
|