searchkick 0.8.4 → 0.8.5

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: a3c68977ac74916a49b5e7dd8c22cb18dd5793dc
4
- data.tar.gz: 3a03323a99f0ee6b726796aad8b2242a9606dfe0
3
+ metadata.gz: c50ef339d04f201523ff1f41aad11277d7168dab
4
+ data.tar.gz: d7f79bf282327a781e23475f64b7530dfac6beab
5
5
  SHA512:
6
- metadata.gz: 4618c81555aa422098d15533378efbc13cfb5070685649a8d3410e65a63628eaed3f0f60783b8cbe2972e3c98d6604c1fefc3cf6c6dba663fada4f7e8a215499
7
- data.tar.gz: 789ff34e062fa62c06f7fa6b1fed9c9f95ba64c99d35ceb58553d473bfc9a16b29f0b3f8ff56688822be9cf90a862d9b0d9633c3a1ef7f657186829693a4c84b
6
+ metadata.gz: 61072c05e3aa82903b699e2062dc0a2944f7fcec20545709d2b83f1f0a760778adffbc7cf7002815c56c2ba6f9484de13f35134c484842ebbf56785d549a6883
7
+ data.tar.gz: 5c95dd8df5ead5050d2b24175e01b6ad197d7f430f92554f2330cb3b0976360ab4182d956e62ae734ce38b77985eb84f6c0e7e95a0d147d18f061e0964ee61e6
@@ -1,3 +1,10 @@
1
+ ## 0.8.5
2
+
3
+ - Added support for Elasticsearch 1.4
4
+ - Added `unsearchable` option
5
+ - Added `select: true` option
6
+ - Added `body` option
7
+
1
8
  ## 0.8.4
2
9
 
3
10
  - Added `boost_by_distance`
data/README.md CHANGED
@@ -774,6 +774,8 @@ See [Production Rails](https://github.com/ankane/production_rails) for other goo
774
774
 
775
775
  Prefer to use the [Elasticsearch DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html) but still want awesome features like zero-downtime reindexing?
776
776
 
777
+ ### Advanced Mapping
778
+
777
779
  Create a custom mapping:
778
780
 
779
781
  ```ruby
@@ -788,32 +790,35 @@ class Product < ActiveRecord::Base
788
790
  end
789
791
  ```
790
792
 
791
- And use the `query` option to search:
793
+ To keep the mappings and settings generated by Searchkick, use:
792
794
 
793
795
  ```ruby
794
- products = Product.search query: {match: {name: "milk"}}
796
+ class Product < ActiveRecord::Base
797
+ searchkick merge_mappings: true, mappings: {...}
798
+ end
795
799
  ```
796
800
 
797
- View the response with:
801
+ ### Advanced Search
802
+
803
+ And use the `body` option to search:
798
804
 
799
805
  ```ruby
800
- products.response
806
+ products = Product.search body: {match: {name: "milk"}}
801
807
  ```
802
808
 
803
- To keep the mappings and settings generated by Searchkick, use:
809
+ View the response with:
804
810
 
805
811
  ```ruby
806
- class Product < ActiveRecord::Base
807
- searchkick merge_mappings: true, mappings: {...}
808
- end
812
+ products.response
809
813
  ```
810
814
 
811
815
  To modify the query generated by Searchkick, use:
812
816
 
813
817
  ```ruby
814
- query = Product.search "2% Milk", execute: false
815
- query.body[:query] = {match_all: {}}
816
- products = query.execute
818
+ products =
819
+ Product.search "2% Milk" do |body|
820
+ body[:query] = {match_all: {}}
821
+ end
817
822
  ```
818
823
 
819
824
  ## Reference
@@ -897,6 +902,14 @@ class Product < ActiveRecord::Base
897
902
  end
898
903
  ```
899
904
 
905
+ Make fields unsearchable but include in the source
906
+
907
+ ```ruby
908
+ class Product < ActiveRecord::Base
909
+ searchkick unsearchable: [:color]
910
+ end
911
+ ```
912
+
900
913
  Reindex asynchronously
901
914
 
902
915
  ```ruby
@@ -20,8 +20,11 @@ module Searchkick
20
20
  Searchkick::Index.new(index)
21
21
  end
22
22
 
23
- define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}|
23
+ define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}, &block|
24
24
  query = Searchkick::Query.new(self, term, options)
25
+ if block
26
+ block.call(query.body)
27
+ end
25
28
  if options[:execute] == false
26
29
  query
27
30
  else
@@ -16,6 +16,7 @@ module Searchkick
16
16
  @options = options
17
17
 
18
18
  below12 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.2")
19
+ below14 = Gem::Version.new(Searchkick.server_version) < Gem::Version.new("1.4")
19
20
 
20
21
  boost_fields = {}
21
22
  fields =
@@ -56,6 +57,7 @@ module Searchkick
56
57
  all = term == "*"
57
58
  facet_limits = {}
58
59
 
60
+ options[:json] ||= options[:body]
59
61
  if options[:json]
60
62
  payload = options[:json]
61
63
  else
@@ -279,7 +281,7 @@ module Searchkick
279
281
  payload[:facets][field] = {
280
282
  terms_stats: {
281
283
  key_field: field,
282
- value_script: "doc.score",
284
+ value_script: below14 ? "doc.score" : "_score",
283
285
  size: size
284
286
  }
285
287
  }
@@ -355,10 +357,10 @@ module Searchkick
355
357
 
356
358
  # An empty array will cause only the _id and _type for each hit to be returned
357
359
  # http://www.elasticsearch.org/guide/reference/api/search/fields/
358
- if load
360
+ if options[:select]
361
+ payload[:fields] = options[:select] if options[:select] != true
362
+ elsif load
359
363
  payload[:fields] = []
360
- elsif options[:select]
361
- payload[:fields] = options[:select]
362
364
  end
363
365
 
364
366
  if options[:type] or klass != searchkick_klass
@@ -40,7 +40,7 @@ module Searchkick
40
40
  # remove old indices that start w/ index_name
41
41
  def clean_indices
42
42
  all_indices = Searchkick.client.indices.get_aliases
43
- indices = all_indices.select{|k, v| v["aliases"].empty? && k =~ /\A#{Regexp.escape(searchkick_index.name)}_\d{14,17}\z/ }.keys
43
+ indices = all_indices.select{|k, v| (v.empty? || v["aliases"].empty?) && k =~ /\A#{Regexp.escape(searchkick_index.name)}_\d{14,17}\z/ }.keys
44
44
  indices.each do |index|
45
45
  Searchkick::Index.new(index).delete
46
46
  end
@@ -288,12 +288,19 @@ module Searchkick
288
288
  mapping[field] = field_mapping
289
289
  end
290
290
 
291
- (options[:locations] || []).each do |field|
291
+ (options[:locations] || []).map(&:to_s).each do |field|
292
292
  mapping[field] = {
293
293
  type: "geo_point"
294
294
  }
295
295
  end
296
296
 
297
+ (options[:unsearchable] || []).map(&:to_s).each do |field|
298
+ mapping[field] = {
299
+ type: "string",
300
+ index: "no"
301
+ }
302
+ end
303
+
297
304
  mappings = {
298
305
  _default_: {
299
306
  properties: mapping,
@@ -127,8 +127,6 @@ module Searchkick
127
127
  next_page.nil?
128
128
  end
129
129
 
130
- protected
131
-
132
130
  def hits
133
131
  @response["hits"]["hits"]
134
132
  end
@@ -1,3 +1,3 @@
1
1
  module Searchkick
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.5"
3
3
  end
@@ -41,6 +41,21 @@ class TestIndex < Minitest::Test
41
41
  assert_equal ["Dollar Tree"], Store.search(json: {query: {match: {name: "Dollar Tree"}}}, load: false).map(&:name)
42
42
  end
43
43
 
44
+ def test_body
45
+ store_names ["Dollar Tree"], Store
46
+ assert_equal [], Store.search(query: {match: {name: "dollar"}}).map(&:name)
47
+ assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "Dollar Tree"}}}, load: false).map(&:name)
48
+ end
49
+
50
+ def test_block
51
+ store_names ["Dollar Tree"]
52
+ products =
53
+ Product.search "boom" do |body|
54
+ body[:query] = {match_all: {}}
55
+ end
56
+ assert_equal ["Dollar Tree"], products.map(&:name)
57
+ end
58
+
44
59
  def test_tokens
45
60
  assert_equal ["dollar", "dollartre", "tree"], Product.searchkick_index.tokens("Dollar Tree")
46
61
  end
@@ -153,4 +153,11 @@ class TestMatch < Minitest::Test
153
153
  assert_search "to be", ["to be or not to be"]
154
154
  end
155
155
 
156
+ def test_unsearchable
157
+ store [
158
+ {name: "Unsearchable", description: "Almond"}
159
+ ]
160
+ assert_search "almond", []
161
+ end
162
+
156
163
  end
@@ -298,6 +298,13 @@ class TestSql < Minitest::Test
298
298
  assert_equal [1, 2], result.user_ids
299
299
  end
300
300
 
301
+ def test_select_all
302
+ store [{name: "Product A", user_ids: [1, 2]}]
303
+ hit = Product.search("product", select: true).hits.first
304
+ assert_equal hit["_source"]["name"], "Product A"
305
+ assert_equal hit["_source"]["user_ids"], [1, 2]
306
+ end
307
+
301
308
  def test_nested_object
302
309
  aisle = {"id" => 1, "name" => "Frozen"}
303
310
  store [{name: "Product A", aisle: aisle}]
@@ -53,6 +53,7 @@ if defined?(Mongoid)
53
53
  field :color
54
54
  field :latitude, type: BigDecimal
55
55
  field :longitude, type: BigDecimal
56
+ field :description
56
57
  end
57
58
 
58
59
  class Store
@@ -95,6 +96,7 @@ else
95
96
  t.string :color
96
97
  t.decimal :latitude, precision: 10, scale: 7
97
98
  t.decimal :longitude, precision: 10, scale: 7
99
+ t.text :description
98
100
  t.timestamps
99
101
  end
100
102
 
@@ -146,7 +148,8 @@ class Product
146
148
  word_start: [:name],
147
149
  word_middle: [:name],
148
150
  word_end: [:name],
149
- highlight: [:name]
151
+ highlight: [:name],
152
+ unsearchable: [:description]
150
153
 
151
154
  attr_accessor :conversions, :user_ids, :aisle
152
155
 
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.8.4
4
+ version: 0.8.5
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-11-06 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel