Class: Elasticated::Query
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Clonable
#==, #clone
#evaluate
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/elasticated/query.rb', line 163
def method_missing(name, *args, &block)
conditions_obj = name.to_s.start_with?('filter_') ? _filter_conditions : _conditions
real_method_name = name.to_s.gsub(/^filter\_/, '').to_sym
super unless conditions_obj.respond_to? real_method_name
conditions_obj.send real_method_name, *args, &block
self
end
|
Instance Attribute Details
#_aggregations ⇒ Object
Returns the value of attribute _aggregations.
17
18
19
|
# File 'lib/elasticated/query.rb', line 17
def _aggregations
@_aggregations
end
|
#_conditions ⇒ Object
Returns the value of attribute _conditions.
15
16
17
|
# File 'lib/elasticated/query.rb', line 15
def _conditions
@_conditions
end
|
#_filter_conditions ⇒ Object
Returns the value of attribute _filter_conditions.
15
16
17
|
# File 'lib/elasticated/query.rb', line 15
def _filter_conditions
@_filter_conditions
end
|
#_from ⇒ Object
Returns the value of attribute _from.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _from
@_from
end
|
#_post_conditions ⇒ Object
Returns the value of attribute _post_conditions.
15
16
17
|
# File 'lib/elasticated/query.rb', line 15
def _post_conditions
@_post_conditions
end
|
#_size ⇒ Object
Returns the value of attribute _size.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _size
@_size
end
|
#_sort ⇒ Object
Returns the value of attribute _sort.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _sort
@_sort
end
|
#_source ⇒ Object
Returns the value of attribute _source.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _source
@_source
end
|
Class Method Details
.build(&block) ⇒ Object
5
6
7
8
9
|
# File 'lib/elasticated/query.rb', line 5
def build(&block)
q = new
q.evaluate block
q
end
|
Instance Method Details
#accept_visitor(visitor) ⇒ Object
155
156
157
|
# File 'lib/elasticated/query.rb', line 155
def accept_visitor(visitor)
visitor.visit_query(self)
end
|
#aggregated? ⇒ Boolean
104
105
106
|
# File 'lib/elasticated/query.rb', line 104
def aggregated?
!_aggregations.empty?
end
|
#aggregations(&block) ⇒ Object
81
82
83
84
|
# File 'lib/elasticated/query.rb', line 81
def aggregations(&block)
_aggregations.evaluate block
self
end
|
#build_for_aggregated_search ⇒ Object
Also known as:
build
140
141
142
143
144
|
# File 'lib/elasticated/query.rb', line 140
def build_for_aggregated_search
ret = build_for_search
ret.merge! aggs: _aggregations.build unless _aggregations.empty?
ret
end
|
#build_for_aggregations ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/elasticated/query.rb', line 117
def build_for_aggregations
raise "No aggregations present in query" unless aggregated?
ret = build_for_count
ret.merge! size: 0
ret.merge! aggs: _aggregations.build
ret
end
|
#build_for_count ⇒ Object
110
111
112
113
114
115
|
# File 'lib/elasticated/query.rb', line 110
def build_for_count
return { query: _conditions.build } if _filter_conditions.empty?
filtered = { filter: _filter_conditions.build }
filtered.merge!(query: _conditions.build) unless _conditions.empty?
{ query: { filtered: filtered } }
end
|
#build_for_search ⇒ Object
134
135
136
137
138
|
# File 'lib/elasticated/query.rb', line 134
def build_for_search
ret = build_for_count.merge build_for_top_hits
ret.merge! post_filter: _post_conditions.build unless _post_conditions.empty?
ret
end
|
#build_for_top_hits ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/elasticated/query.rb', line 125
def build_for_top_hits
ret = Hash.new
ret.merge! sort: _sort if _sort
ret.merge! size: _size if _size
ret.merge! from: _from if _from
ret.merge! _source: _source if _source
ret
end
|
#conditions(&block) ⇒ Object
28
29
30
31
|
# File 'lib/elasticated/query.rb', line 28
def conditions(&block)
_conditions.evaluate block
self
end
|
#custom_sort(custom_hash) ⇒ Object
56
57
58
59
60
|
# File 'lib/elasticated/query.rb', line 56
def custom_sort(custom_hash)
self._sort ||= Array.new
_sort << custom_hash
self
end
|
#filter(&block) ⇒ Object
33
34
35
36
|
# File 'lib/elasticated/query.rb', line 33
def filter(&block)
_filter_conditions.evaluate block
self
end
|
#from(value) ⇒ Object
Also known as:
offset
50
51
52
53
|
# File 'lib/elasticated/query.rb', line 50
def from(value)
self._from = value
self
end
|
#has_offset? ⇒ Boolean
96
97
98
|
# File 'lib/elasticated/query.rb', line 96
def has_offset?
_from && _from > 0
end
|
#heavy_for?(repository) ⇒ Boolean
92
93
94
|
# File 'lib/elasticated/query.rb', line 92
def heavy_for?(repository)
!_size || _size > repository.search_page_size
end
|
#limited? ⇒ Boolean
88
89
90
|
# File 'lib/elasticated/query.rb', line 88
def limited?
!!_size
end
|
#parse_aggregations(response) ⇒ Object
149
150
151
|
# File 'lib/elasticated/query.rb', line 149
def parse_aggregations(response)
_aggregations.parse response
end
|
#post(&block) ⇒ Object
Also known as:
post_filter
38
39
40
41
|
# File 'lib/elasticated/query.rb', line 38
def post(&block)
_post_conditions.evaluate block
self
end
|
#size(value) ⇒ Object
Also known as:
limit
44
45
46
47
|
# File 'lib/elasticated/query.rb', line 44
def size(value)
self._size = value
self
end
|
#sort(field, method = nil) ⇒ Object
Also known as:
sort_by_field
62
63
64
|
# File 'lib/elasticated/query.rb', line 62
def sort(field, method=nil)
custom_sort(field => { order: method || :asc })
end
|
#sort_by_script(script_hash) ⇒ Object
67
68
69
|
# File 'lib/elasticated/query.rb', line 67
def sort_by_script(script_hash)
custom_sort(_script: script_hash)
end
|
#sort_randomly ⇒ Object
71
72
73
|
# File 'lib/elasticated/query.rb', line 71
def sort_randomly
sort_by_script(script: "Math.random()", type: "number")
end
|
#sorted? ⇒ Boolean
100
101
102
|
# File 'lib/elasticated/query.rb', line 100
def sorted?
!!_sort
end
|
#source(*fields_array) ⇒ Object
Also known as:
fields
75
76
77
78
|
# File 'lib/elasticated/query.rb', line 75
def source(*fields_array)
self._source = fields_array.flatten
self
end
|