searchkick 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2b3dc0e591516c6ddd76101c1053211ae7f9fe9
4
- data.tar.gz: f56695cbbfcc4e92b6d751add7c00ec6bbdb82ed
3
+ metadata.gz: 2d9d6517d9d49a09f8827ea95a866e4a162fa7f3
4
+ data.tar.gz: cb53edf08c5f05e95e6bb11aa2e00f010f6e2043
5
5
  SHA512:
6
- metadata.gz: 1a4ca76c448e90b4d838aa1e0076b01355866b837a046618222aaa7e65b9142b3370c8f93fc622be57e248a5ed8fd4941155fa915dda118c1781893266c832c6
7
- data.tar.gz: 2d7f62a4329cf8005f57ae60968f3e2aea626cff87e5073a8fa141250ec135424ba8bb2ccff46ddf8033bdf07d05ecad3dec59fceeb5d67d044911e7ffa9f561
6
+ metadata.gz: 3a9fb661de8eaa36d5f1c979ee1bb0a3779716b4983e17d94de72883fd1a549c874cbd2a41f8498b7f4110e5d62ed7511f3d873df9a42d51aa590cf55156d72f
7
+ data.tar.gz: 000f1e7fb07a5d00659f21a8116e8fd57dcaf6b41adcc261d7687e6cccccd2edb811738c7e3552f0ac1c594dffd818ae4966cdda36ed8fe334843df1350e4137
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.7.6
2
+
3
+ - Added `stats` option to facets
4
+ - Added `padding` option
5
+
1
6
  ## 0.7.5
2
7
 
3
8
  - Do not throw errors when index becomes out of sync with database
data/README.md CHANGED
@@ -108,6 +108,8 @@ Order
108
108
  order: {_score: :desc} # most relevant first - default
109
109
  ```
110
110
 
111
+ [All of these sort options are supported](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)
112
+
111
113
  Limit / offset
112
114
 
113
115
  ```ruby
@@ -441,6 +443,12 @@ price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}]
441
443
  Product.search "*", facets: {price: {ranges: price_ranges}}
442
444
  ```
443
445
 
446
+ Use the `stats` option to get to max, min, mean, and total scores for each facet [master]
447
+
448
+ ```ruby
449
+ Product.search "*", facets: {store_id: {stats: true}}
450
+ ```
451
+
444
452
  ### Highlight
445
453
 
446
454
  Highlight the search query in the results.
@@ -44,7 +44,7 @@ module Searchkick
44
44
  client.bulk(
45
45
  index: name,
46
46
  type: type,
47
- body: batch.map{|r| data = search_data(r); {index: {_id: search_id(r), data: data}} }
47
+ body: batch.map{|r| {index: {_id: search_id(r), data: search_data(r)}} }
48
48
  )
49
49
  end
50
50
  end
@@ -58,7 +58,11 @@ module Searchkick
58
58
  end
59
59
 
60
60
  def klass_document_type(klass)
61
- klass.model_name.to_s.underscore
61
+ if klass.respond_to?(:document_type)
62
+ klass.document_type
63
+ else
64
+ klass.model_name.to_s.underscore
65
+ end
62
66
  end
63
67
 
64
68
  protected
@@ -77,11 +81,11 @@ module Searchkick
77
81
 
78
82
  def search_data(record)
79
83
  source = record.search_data
84
+ options = record.class.searchkick_options
80
85
 
81
86
  # stringify fields
82
- source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}.except("id", "_id")
83
-
84
- options = record.class.searchkick_options
87
+ # remove _id since search_id is used instead
88
+ source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}.except("_id")
85
89
 
86
90
  # conversions
87
91
  conversions_field = options[:conversions]
@@ -107,8 +111,6 @@ module Searchkick
107
111
 
108
112
  cast_big_decimal(source)
109
113
 
110
- # p search_data
111
-
112
114
  source.as_json
113
115
  end
114
116
 
@@ -38,7 +38,8 @@ module Searchkick
38
38
  # pagination
39
39
  page = [options[:page].to_i, 1].max
40
40
  per_page = (options[:limit] || options[:per_page] || 100000).to_i
41
- offset = options[:offset] || (page - 1) * per_page
41
+ padding = [options[:padding].to_i, 0].max
42
+ offset = options[:offset] || (page - 1) * per_page + padding
42
43
 
43
44
  conversions_field = searchkick_options[:conversions]
44
45
  personalize_field = searchkick_options[:personalize]
@@ -218,6 +219,7 @@ module Searchkick
218
219
  facets.each do |field, facet_options|
219
220
  # ask for extra facets due to
220
221
  # https://github.com/elasticsearch/elasticsearch/issues/1305
222
+ size = facet_options[:limit] ? facet_options[:limit] + 150 : 100000
221
223
 
222
224
  if facet_options[:ranges]
223
225
  payload[:facets][field] = {
@@ -225,11 +227,19 @@ module Searchkick
225
227
  field.to_sym => facet_options[:ranges]
226
228
  }
227
229
  }
230
+ elsif facet_options[:stats]
231
+ payload[:facets][field] = {
232
+ terms_stats: {
233
+ key_field: field,
234
+ value_script: "doc.score",
235
+ size: size
236
+ }
237
+ }
228
238
  else
229
239
  payload[:facets][field] = {
230
240
  terms: {
231
241
  field: field,
232
- size: facet_options[:limit] ? facet_options[:limit] + 150 : 100000
242
+ size: size
233
243
  }
234
244
  }
235
245
  end
@@ -298,6 +308,7 @@ module Searchkick
298
308
  @facet_limits = facet_limits
299
309
  @page = page
300
310
  @per_page = per_page
311
+ @padding = padding
301
312
  @load = load
302
313
  end
303
314
 
@@ -360,6 +371,7 @@ module Searchkick
360
371
  opts = {
361
372
  page: @page,
362
373
  per_page: @per_page,
374
+ padding: @padding,
363
375
  load: @load,
364
376
  includes: options[:include] || options[:includes]
365
377
  }
@@ -39,7 +39,7 @@ module Searchkick
39
39
  else
40
40
  hits.map do |hit|
41
41
  result = hit.except("_source").merge(hit["_source"])
42
- result["id"] = result["_id"]
42
+ result["id"] ||= result["_id"] # needed for legacy reasons
43
43
  Hashie::Mash.new(result)
44
44
  end
45
45
  end
@@ -90,13 +90,17 @@ module Searchkick
90
90
  end
91
91
  alias_method :limit_value, :per_page
92
92
 
93
+ def padding
94
+ options[:padding]
95
+ end
96
+
93
97
  def total_pages
94
98
  (total_count / per_page.to_f).ceil
95
99
  end
96
100
  alias_method :num_pages, :total_pages
97
101
 
98
102
  def offset_value
99
- (current_page - 1) * per_page
103
+ (current_page - 1) * per_page + padding
100
104
  end
101
105
  alias_method :offset, :offset_value
102
106
 
@@ -1,3 +1,3 @@
1
1
  module Searchkick
2
- VERSION = "0.7.5"
2
+ VERSION = "0.7.6"
3
3
  end
data/test/facets_test.rb CHANGED
@@ -59,6 +59,13 @@ class TestFacets < Minitest::Unit::TestCase
59
59
  assert_equal ({1 => 1, 2 => 1}), store_facet(where: {store_id: 2, price: {gt: 5}}, facets: [:store_id], smart_facets: true)
60
60
  end
61
61
 
62
+ def test_stats_facets
63
+ options = {where: {store_id: 2}, facets: {store_id: {stats: true}}}
64
+ facets = Product.search("Product", options).facets["store_id"]["terms"]
65
+ expected_facets_keys = %w[term count total_count min max total mean]
66
+ assert_equal expected_facets_keys, facets.first.keys
67
+ end
68
+
62
69
  protected
63
70
 
64
71
  def store_facet(options)
data/test/sql_test.rb CHANGED
@@ -19,19 +19,20 @@ class TestSql < Minitest::Unit::TestCase
19
19
  end
20
20
 
21
21
  def test_pagination
22
- store_names ["Product A", "Product B", "Product C", "Product D", "Product E"]
23
- products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2)
24
- assert_equal ["Product C", "Product D"], products.map(&:name)
22
+ store_names ["Product A", "Product B", "Product C", "Product D", "Product E", "Product F"]
23
+ products = Product.search("product", order: {name: :asc}, page: 2, per_page: 2, padding: 1)
24
+ assert_equal ["Product D", "Product E"], products.map(&:name)
25
25
  assert_equal 2, products.current_page
26
+ assert_equal 1, products.padding
26
27
  assert_equal 2, products.per_page
27
28
  assert_equal 2, products.size
28
29
  assert_equal 2, products.length
29
30
  assert_equal 3, products.total_pages
30
- assert_equal 5, products.total_count
31
- assert_equal 5, products.total_entries
31
+ assert_equal 6, products.total_count
32
+ assert_equal 6, products.total_entries
32
33
  assert_equal 2, products.limit_value
33
- assert_equal 2, products.offset_value
34
- assert_equal 2, products.offset
34
+ assert_equal 3, products.offset_value
35
+ assert_equal 3, products.offset
35
36
  assert !products.first_page?
36
37
  assert !products.last_page?
37
38
  assert !products.empty?
@@ -197,6 +198,10 @@ class TestSql < Minitest::Unit::TestCase
197
198
  assert_order "product", ["Product A", "Product B", "Product C"], order: {color: :asc, store_id: :desc}
198
199
  end
199
200
 
201
+ def test_order_ignore_unmapped
202
+ assert_order "product", [], order: {not_mapped: {ignore_unmapped: true}}, conversions: false
203
+ end
204
+
200
205
  def test_partial
201
206
  store_names ["Honey"]
202
207
  assert_search "fresh honey", []
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-14 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel