Class: Elastic::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/co-elastic-query.rb

Constant Summary collapse

DEFAULT_SORT =
[{
    'doc.created_at' => {
        order: :desc
    }
}]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/co-elastic-query.rb', line 6

def initialize(params = nil)
    query = params ? params.permit(:q, :limit, :offset) : {}

    @filters = nil
    @search = "#{query[:q]}*"
    @fields = ['_all'.freeze]

    @limit = query[:limit] || 20
    @limit = @limit.to_i
    @limit = 10000 if @limit > 10000

    @offset = query[:offset] || 0
    @offset = offset.to_i
    @offset = 10000 if offset > 10000
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



23
24
25
# File 'lib/co-elastic-query.rb', line 23

def fields
  @fields
end

#limitObject

Returns the value of attribute limit.



23
24
25
# File 'lib/co-elastic-query.rb', line 23

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



23
24
25
# File 'lib/co-elastic-query.rb', line 23

def offset
  @offset
end

#query_settingsObject

Returns the value of attribute query_settings.



23
24
25
# File 'lib/co-elastic-query.rb', line 23

def query_settings
  @query_settings
end

#sortObject

Returns the value of attribute sort.



23
24
25
# File 'lib/co-elastic-query.rb', line 23

def sort
  @sort
end

Instance Method Details

#and_filter(filters) ⇒ Object



51
52
53
54
55
# File 'lib/co-elastic-query.rb', line 51

def and_filter(filters)
    @andFilter ||= {}
    @andFilter.merge!(filters)
    self
end

#buildObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/co-elastic-query.rb', line 94

def build
    if @filters
        fieldfilters = []

        @filters.each do |key, value|
            fieldfilter = { :or => [] }
            build_filter(fieldfilter[:or], key, value)

            # TODO:: Discuss this - might be a security issue
            unless fieldfilter[:or].empty?
                fieldfilters.push(fieldfilter)
            end
        end
    end

    if @orFilter
        fieldfilters ||= []
        fieldfilter = { :or => [] }
        orArray = fieldfilter[:or]

        @orFilter.each do |key, value|
            build_filter(orArray, key, value)
        end

        unless orArray.empty?
            fieldfilters.push(fieldfilter)
        end
    end

    if @andFilter
        fieldfilters ||= []
        fieldfilter = { :and => [] }
        andArray = fieldfilter[:and]

        @andFilter.each do |key, value|
            build_filter(andArray, key, value)
        end

        unless andArray.empty?
            fieldfilters.push(fieldfilter)
        end
    end

    if @rangeFilter
        fieldfilters ||= []

        @rangeFilter.each do |value|
            fieldfilters.push({range: value})
        end
    end

    if @nots
        fieldfilters ||= []

        @nots.each do |key, value|
            fieldfilter = { not: { filter: { or: [] } } }
            build_filter(fieldfilter[:not][:filter][:or], key, value)
            unless fieldfilter[:not][:filter][:or].empty?
                fieldfilters.push(fieldfilter)
            end
        end
    end

    if @missing
        fieldfilters ||= []

        @missing.each do |field|
            fieldfilters.push({
                missing: { field: field }
            })
        end
    end

    if @exists
        fieldfilters ||= []

        @exists.each do |field|
            fieldfilters.push({
                exists: { field: field }
            })
        end
    end

    if @raw_filter
        fieldfilters ||= []
        fieldfilters += @raw_filter
    end

    if @search.length > 1
        # Break the terms up purely on whitespace
        query_obj = nil

        if @hasChild || @hasParent
            should = [{
                    simple_query_string: {
                        query: @search,
                        fields: @fields
                    }
                }]

            if @query_settings
                should[0][:simple_query_string].merge! @query_settings
            end

            if @hasChild
                should << {
                        has_child: {
                            type: @hasChild,
                            query: {
                                simple_query_string: {
                                    query: @search,
                                    fields: @fields
                                }
                            }
                        }
                    }
            end

            if @hasParent
                should << {
                        has_parent: {
                            parent_type: @hasParent,
                            query: {
                                simple_query_string: {
                                    query: @search,
                                    fields: @fields
                                }
                            }
                        }
                    }
            end

            query_obj = {
                query: {
                    bool: {
                        should: should
                    }
                },
                filters: fieldfilters,
                offset: @offset,
                limit: @limit
            }
        else
            query_obj = {
                query: {
                    simple_query_string: {
                        query: @search,
                        fields: @fields
                    }
                },
                filters: fieldfilters,
                offset: @offset,
                limit: @limit
            }
        end

        query_obj
    else
        {
            sort: @sort || DEFAULT_SORT,
            filters: fieldfilters,
            query: {
                match_all: {}
            },
            offset: @offset,
            limit: @limit
        }
    end
end

#build_filter(filters, key, values) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/co-elastic-query.rb', line 274

def build_filter(filters, key, values)
    values.each { |var|
        if var.nil?
            filters.push({
                missing: { field: key }
            })
        else
            filters.push({
                :term => {
                    key => var
                }
            })
        end
    }
end

#exists(*fields) ⇒ Object



87
88
89
90
91
# File 'lib/co-elastic-query.rb', line 87

def exists(*fields)
    @exists ||= Set.new
    @exists.merge(fields)
    self
end

#filter(filters) ⇒ Object

filters is in the form [‘var1’,‘var2’,…], fieldname2: [‘var1’,‘var2’…]

NOTE

may overwrite an existing filter in merge



38
39
40
41
42
# File 'lib/co-elastic-query.rb', line 38

def filter(filters)
    @filters ||= {}
    @filters.merge!(filters)
    self
end

#has_child(name) ⇒ Object

Applys the query to child objects



58
59
60
# File 'lib/co-elastic-query.rb', line 58

def has_child(name)
    @hasChild = name
end

#has_parent(name) ⇒ Object



62
63
64
# File 'lib/co-elastic-query.rb', line 62

def has_parent(name)
    @hasParent = name
end

#missing(*fields) ⇒ Object

Call to add fields that should be missing Effectively adds a filter that ensures a field is missing



74
75
76
77
78
# File 'lib/co-elastic-query.rb', line 74

def missing(*fields)
    @missing ||= Set.new
    @missing.merge(fields)
    self
end

#not(filters) ⇒ Object

The opposite of filter



81
82
83
84
85
# File 'lib/co-elastic-query.rb', line 81

def not(filters)
    @nots ||= {}
    @nots.merge!(filters)
    self
end

#or_filter(filters) ⇒ Object

Like filter however all keys are OR’s instead of AND’s



45
46
47
48
49
# File 'lib/co-elastic-query.rb', line 45

def or_filter(filters)
    @orFilter ||= {}
    @orFilter.merge!(filters)
    self
end

#range(filter) ⇒ Object



66
67
68
69
70
# File 'lib/co-elastic-query.rb', line 66

def range(filter)
    @rangeFilter ||= []
    @rangeFilter << filter
    self
end

#raw_filter(filter) ⇒ Object



25
26
27
28
29
# File 'lib/co-elastic-query.rb', line 25

def raw_filter(filter)
    @raw_filter ||= []
    @raw_filter << filter
    self
end

#search_field(field) ⇒ Object



31
32
33
# File 'lib/co-elastic-query.rb', line 31

def search_field(field)
    @fields.unshift(field)
end