co-elastic-query 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/co-elastic-query.rb +49 -16
- data/lib/co-elastic-query/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb06a0255fd72056eede70e58e97638b3b0187ff
|
4
|
+
data.tar.gz: 07e712c603aa04b0a4dc94dc4a424bbd86bb4729
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 366a55f2a4f06023536220dec23610ab01107f46b2d11399848424b489f8ad725c2bd3c00cbff527cc909e2eccee15829a1e77bc3654fecc9f53d6948080e558
|
7
|
+
data.tar.gz: 1b1d8c3dc6d5ec94b02af7aa859ac17eba936596f5a3203e4c9e864aec5c4302d4dbb7f1d1bad4025913bb7bd4c6bababd2b77548fabd1315f090e5a1f2477fd
|
data/lib/co-elastic-query.rb
CHANGED
@@ -2,8 +2,8 @@ require 'elasticsearch'
|
|
2
2
|
|
3
3
|
class Elastic
|
4
4
|
class Query
|
5
|
-
def initialize(params)
|
6
|
-
query = params.permit(:q, :limit, :offset)
|
5
|
+
def initialize(params = nil)
|
6
|
+
query = params ? params.permit(:q, :limit, :offset) : {}
|
7
7
|
|
8
8
|
@filters = nil
|
9
9
|
@search = query[:q]
|
@@ -240,6 +240,15 @@ class Elastic
|
|
240
240
|
@@client.search *args
|
241
241
|
end
|
242
242
|
|
243
|
+
def self.count *args
|
244
|
+
@@client.count *args
|
245
|
+
end
|
246
|
+
|
247
|
+
def self.client
|
248
|
+
@@client
|
249
|
+
end
|
250
|
+
|
251
|
+
COUNT = 'count'.freeze
|
243
252
|
HITS = 'hits'.freeze
|
244
253
|
TOTAL = 'total'.freeze
|
245
254
|
ID = '_id'.freeze
|
@@ -254,13 +263,49 @@ class Elastic
|
|
254
263
|
end
|
255
264
|
|
256
265
|
# Safely build the query
|
257
|
-
def query(params, filters = nil)
|
266
|
+
def query(params = nil, filters = nil)
|
258
267
|
builder = ::Elastic::Query.new(params)
|
259
268
|
builder.filter(filters) if filters
|
260
269
|
builder
|
261
270
|
end
|
262
271
|
|
263
272
|
def search(builder, &block)
|
273
|
+
query = generate_body(builder)
|
274
|
+
|
275
|
+
# if a formatter block is supplied, each loaded record is passed to it
|
276
|
+
# allowing annotation/conversion of records using data from the model
|
277
|
+
# and current request (e.g groups are annotated with 'admin' if the
|
278
|
+
# currently logged in user is an admin of the group). nils are removed
|
279
|
+
# from the list.
|
280
|
+
result = Elastic.search(query)
|
281
|
+
records = @klass.find_by_id(result[HITS][HITS].map {|entry| entry[ID]}) || []
|
282
|
+
{
|
283
|
+
total: result[HITS][TOTAL] || 0,
|
284
|
+
results: block_given? ? (records.map {|record| yield record}).compact : records
|
285
|
+
}
|
286
|
+
end
|
287
|
+
|
288
|
+
def count(builder)
|
289
|
+
query = generate_body(builder)
|
290
|
+
|
291
|
+
# Simplify the query
|
292
|
+
query[:body].delete(:from)
|
293
|
+
query[:body].delete(:size)
|
294
|
+
query[:body].delete(:sort)
|
295
|
+
|
296
|
+
# if a formatter block is supplied, each loaded record is passed to it
|
297
|
+
# allowing annotation/conversion of records using data from the model
|
298
|
+
# and current request (e.g groups are annotated with 'admin' if the
|
299
|
+
# currently logged in user is an admin of the group). nils are removed
|
300
|
+
# from the list.
|
301
|
+
Elastic.count(query)[COUNT]
|
302
|
+
end
|
303
|
+
|
304
|
+
|
305
|
+
protected
|
306
|
+
|
307
|
+
|
308
|
+
def generate_body(builder)
|
264
309
|
opt = builder.build
|
265
310
|
|
266
311
|
sort = opt[:sort] || []
|
@@ -277,7 +322,7 @@ class Elastic
|
|
277
322
|
filters.unshift({type: {value: @filter}})
|
278
323
|
end
|
279
324
|
|
280
|
-
|
325
|
+
{
|
281
326
|
index: @index,
|
282
327
|
body: {
|
283
328
|
sort: sort,
|
@@ -299,17 +344,5 @@ class Elastic
|
|
299
344
|
size: opt[:limit]
|
300
345
|
}
|
301
346
|
}
|
302
|
-
|
303
|
-
# if a formatter block is supplied, each loaded record is passed to it
|
304
|
-
# allowing annotation/conversion of records using data from the model
|
305
|
-
# and current request (e.g groups are annotated with 'admin' if the
|
306
|
-
# currently logged in user is an admin of the group). nils are removed
|
307
|
-
# from the list.
|
308
|
-
result = Elastic.search(query)
|
309
|
-
records = @klass.find_by_id(result[HITS][HITS].map {|entry| entry[ID]}) || []
|
310
|
-
{
|
311
|
-
total: result[HITS][TOTAL] || 0,
|
312
|
-
results: block_given? ? (records.map {|record| yield record}).compact : records
|
313
|
-
}
|
314
347
|
end
|
315
348
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: co-elastic-query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen von Takach
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|