co-elastic-query 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 272f832b729096f2ffc296f06276461db55df1ec
4
- data.tar.gz: cc92ac8f7b830a06e6b6529800974b1c50a24802
3
+ metadata.gz: cb06a0255fd72056eede70e58e97638b3b0187ff
4
+ data.tar.gz: 07e712c603aa04b0a4dc94dc4a424bbd86bb4729
5
5
  SHA512:
6
- metadata.gz: d094797eeb4d609456b156a1da5287740002f7865b933a5ba243c49f46f1cb32db68463e0139685aaf430ef5a122b0255d1c32208ce7268409ce3000f72c613e
7
- data.tar.gz: 260883cc679078087afe9fa4dc89a1673adc89c2dee149d62d4c061ad443a8ccb2216bbbb63aedf13b4ee626ab4fe8dff505ac4edc5c53d8069b493fe66b4fda
6
+ metadata.gz: 366a55f2a4f06023536220dec23610ab01107f46b2d11399848424b489f8ad725c2bd3c00cbff527cc909e2eccee15829a1e77bc3654fecc9f53d6948080e558
7
+ data.tar.gz: 1b1d8c3dc6d5ec94b02af7aa859ac17eba936596f5a3203e4c9e864aec5c4302d4dbb7f1d1bad4025913bb7bd4c6bababd2b77548fabd1315f090e5a1f2477fd
@@ -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
- query = {
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
@@ -1,3 +1,3 @@
1
1
  module CoElasticQuery
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  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.1
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-04-22 00:00:00.000000000 Z
12
+ date: 2015-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler