Class: Esse::Search::Query
- Inherits:
-
Object
- Object
- Esse::Search::Query
show all
- Includes:
- DSL
- Defined in:
- lib/esse/search/query.rb,
lib/esse/search/query/dsl.rb
Defined Under Namespace
Modules: DSL
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from DSL
#limit, #limit_value, #offset, #offset_value
Constructor Details
#initialize(transport, *indices, suffix: nil, **definition, &_block) ⇒ Query
Returns a new instance of Query.
13
14
15
16
17
|
# File 'lib/esse/search/query.rb', line 13
def initialize(transport, *indices, suffix: nil, **definition, &_block)
@transport = transport
@definition = definition
@definition[:index] = self.class.normalize_indices(*indices, suffix: suffix) if indices.size > 0
end
|
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
8
9
10
|
# File 'lib/esse/search/query.rb', line 8
def definition
@definition
end
|
#transport ⇒ Object
Returns the value of attribute transport.
8
9
10
|
# File 'lib/esse/search/query.rb', line 8
def transport
@transport
end
|
Class Method Details
.normalize_indices(*indices, suffix: nil) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/esse/search/query.rb', line 19
def self.normalize_indices(*indices, suffix: nil)
indices.map do |index|
if index.is_a?(Class) && index < Esse::Index
index.index_name(suffix: suffix)
elsif index.is_a?(String) || index.is_a?(Symbol)
[index, suffix].compact.join('_')
else
raise ArgumentError, format('Invalid index type: %<index>p. It should be a Esse::Index class or a String index name', index: index)
end
end.join(',')
end
|
Instance Method Details
#reset! ⇒ Object
86
87
88
|
# File 'lib/esse/search/query.rb', line 86
def reset!
@response = nil
end
|
#response ⇒ Object
31
32
33
|
# File 'lib/esse/search/query.rb', line 31
def response
@response ||= execute_search_query!
end
|
#results ⇒ Object
35
36
37
38
39
|
# File 'lib/esse/search/query.rb', line 35
def results
return paginated_results if respond_to?(:paginated_results, true)
response.hits
end
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/esse/search/query.rb', line 41
def scroll_hits(batch_size: 1_000, scroll: '1m')
response = execute_search_query!(size: batch_size, scroll: scroll)
scroll_id = response.raw_response['scroll_id'] || response.raw_response['_scroll_id']
fetched = 0
total = response.total
loop do
fetched += response.hits.size
yield(response.hits) if response.hits.any?
break if fetched >= total
break unless scroll_id
response = execute_scroll_query(scroll: scroll, scroll_id: scroll_id)
scroll_id = response.raw_response['scroll_id'] || response.raw_response['_scroll_id']
end
ensure
begin
transport.clear_scroll(body: {scroll_id: scroll_id}) if scroll_id
rescue Esse::Transport::NotFoundError
end
end
|
#search_after_hits(batch_size: 1_000) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/esse/search/query.rb', line 62
def search_after_hits(batch_size: 1_000)
body = HashUtils.deep_dup(definition.fetch(:body, {}))
body[:size] = batch_size
body.delete(:from)
body.delete('from')
unless body.key?(:sort) || body.key?('sort')
raise ArgumentError, 'The query body must include a :sort to use search_after'
end
loop do
response = execute_search_query!(body: body)
break if response.hits.empty?
yield(response.hits)
last_sort = response.hits.last['sort']
break unless last_sort
break if response.hits.size < batch_size
body[:search_after] = last_sort
end
end
|