Class: Elasticated::Query
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#inspect, #text_for_inspect
#evaluate
#==, #clone
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
169
170
171
172
173
174
175
176
177
|
# File 'lib/elasticated/query.rb', line 169
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.
18
19
20
|
# File 'lib/elasticated/query.rb', line 18
def _aggregations
@_aggregations
end
|
#_conditions ⇒ Object
Returns the value of attribute _conditions.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _conditions
@_conditions
end
|
#_filter_conditions ⇒ Object
Returns the value of attribute _filter_conditions.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _filter_conditions
@_filter_conditions
end
|
#_from ⇒ Object
Returns the value of attribute _from.
17
18
19
|
# File 'lib/elasticated/query.rb', line 17
def _from
@_from
end
|
#_post_conditions ⇒ Object
Returns the value of attribute _post_conditions.
16
17
18
|
# File 'lib/elasticated/query.rb', line 16
def _post_conditions
@_post_conditions
end
|
#_size ⇒ Object
Returns the value of attribute _size.
17
18
19
|
# File 'lib/elasticated/query.rb', line 17
def _size
@_size
end
|
#_sort ⇒ Object
Returns the value of attribute _sort.
17
18
19
|
# File 'lib/elasticated/query.rb', line 17
def _sort
@_sort
end
|
#_source ⇒ Object
Returns the value of attribute _source.
17
18
19
|
# File 'lib/elasticated/query.rb', line 17
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
#aggregated? ⇒ Boolean
108
109
110
|
# File 'lib/elasticated/query.rb', line 108
def aggregated?
!_aggregations.empty?
end
|
#aggregations(&block) ⇒ Object
Also known as:
aggs
84
85
86
87
|
# File 'lib/elasticated/query.rb', line 84
def aggregations(&block)
_aggregations.evaluate block
self
end
|
#build_for_aggregated_search ⇒ Object
Also known as:
build, to_h
144
145
146
147
148
|
# File 'lib/elasticated/query.rb', line 144
def build_for_aggregated_search
ret = build_for_search
ret.merge! aggs: _aggregations.build unless _aggregations.empty?
ret
end
|
#build_for_aggregations ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/elasticated/query.rb', line 121
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
114
115
116
117
118
119
|
# File 'lib/elasticated/query.rb', line 114
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
138
139
140
141
142
|
# File 'lib/elasticated/query.rb', line 138
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
129
130
131
132
133
134
135
136
|
# File 'lib/elasticated/query.rb', line 129
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 unless _source.nil? ret
end
|
#conditions(&block) ⇒ Object
29
30
31
32
|
# File 'lib/elasticated/query.rb', line 29
def conditions(&block)
_conditions.evaluate block
self
end
|
#fill_delimiter(field_delimiter) ⇒ Object
160
161
162
163
|
# File 'lib/elasticated/query.rb', line 160
def fill_delimiter(field_delimiter)
_conditions.fill_delimiter field_delimiter
_filter_conditions.fill_delimiter field_delimiter
end
|
#filter(&block) ⇒ Object
34
35
36
37
|
# File 'lib/elasticated/query.rb', line 34
def filter(&block)
_filter_conditions.evaluate block
self
end
|
#from(value) ⇒ Object
Also known as:
offset
51
52
53
54
|
# File 'lib/elasticated/query.rb', line 51
def from(value)
self._from = value
self
end
|
#heavy_for?(repository) ⇒ Boolean
96
97
98
|
# File 'lib/elasticated/query.rb', line 96
def heavy_for?(repository)
!_size || _size > repository.search_page_size
end
|
#limited? ⇒ Boolean
92
93
94
|
# File 'lib/elasticated/query.rb', line 92
def limited?
!!_size
end
|
#no_source ⇒ Object
79
80
81
82
|
# File 'lib/elasticated/query.rb', line 79
def no_source
self._source = false
self
end
|
#offset? ⇒ Boolean
100
101
102
|
# File 'lib/elasticated/query.rb', line 100
def offset?
_from && _from > 0
end
|
#parse_aggregations(response) ⇒ Object
154
155
156
|
# File 'lib/elasticated/query.rb', line 154
def parse_aggregations(response)
_aggregations.parse response
end
|
#post(&block) ⇒ Object
Also known as:
post_filter
39
40
41
42
|
# File 'lib/elasticated/query.rb', line 39
def post(&block)
_post_conditions.evaluate block
self
end
|
#size(value) ⇒ Object
Also known as:
limit
45
46
47
48
|
# File 'lib/elasticated/query.rb', line 45
def size(value)
self._size = value
self
end
|
#sort(field, method = nil) ⇒ Object
57
58
59
60
61
|
# File 'lib/elasticated/query.rb', line 57
def sort(field, method=nil)
self._sort ||= Array.new
_sort << { field => { order: method || :asc } }
self
end
|
#sort_randomly ⇒ Object
63
64
65
66
67
|
# File 'lib/elasticated/query.rb', line 63
def sort_randomly
self._sort ||= Array.new
_sort << { _script: { script: "Math.random()", type: "number" } }
self
end
|
#sorted? ⇒ Boolean
104
105
106
|
# File 'lib/elasticated/query.rb', line 104
def sorted?
!!_sort
end
|
#source(*fields_array) ⇒ Object
Also known as:
fields
69
70
71
72
73
74
75
76
|
# File 'lib/elasticated/query.rb', line 69
def source(*fields_array)
if fields_array.count == 1 && fields_array.first == false
no_source
else
self._source = fields_array.flatten
end
self
end
|