Commit 454b59cc7c19406fae438dd7b57a2c5ef9ee81e6

Authored by Andrew Kane
2 parents a39cc393 b5763d73

Merge branch 'master' into agg_docs

lib/searchkick/index.rb
... ... @@ -100,7 +100,7 @@ module Searchkick
100 100  
101 101 def similar_record(record, options = {})
102 102 like_text = retrieve(record).to_hash
103   - .keep_if { |k, v| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
  103 + .keep_if { |k, _| !options[:fields] || options[:fields].map(&:to_s).include?(k) }
104 104 .values.compact.join(" ")
105 105  
106 106 # TODO deep merge method
... ... @@ -118,9 +118,7 @@ module Searchkick
118 118  
119 119 def search_model(searchkick_klass, term = nil, options = {}, &block)
120 120 query = Searchkick::Query.new(searchkick_klass, term, options)
121   - if block
122   - block.call(query.body)
123   - end
  121 + block.call(query.body) if block
124 122 if options[:execute] == false
125 123 query
126 124 else
... ... @@ -350,9 +348,7 @@ module Searchkick
350 348 # synonyms
351 349 synonyms = options[:synonyms] || []
352 350  
353   - if synonyms.respond_to?(:call)
354   - synonyms = synonyms.call
355   - end
  351 + synonyms = synonyms.call if synonyms.respond_to?(:call)
356 352  
357 353 if synonyms.any?
358 354 settings[:analysis][:filter][:searchkick_synonym] = {
... ... @@ -384,7 +380,7 @@ module Searchkick
384 380 end
385 381  
386 382 if options[:special_characters] == false
387   - settings[:analysis][:analyzer].each do |analyzer, analyzer_settings|
  383 + settings[:analysis][:analyzer].each do |_, analyzer_settings|
388 384 analyzer_settings[:filter].reject! { |f| f == "asciifolding" }
389 385 end
390 386 end
... ... @@ -566,6 +562,5 @@ module Searchkick
566 562 obj
567 563 end
568 564 end
569   -
570 565 end
571 566 end
... ...
lib/searchkick/logging.rb
... ... @@ -62,7 +62,8 @@ module Searchkick
62 62 end
63 63  
64 64 def self.reset_runtime
65   - rt, self.runtime = runtime, 0
  65 + rt = runtime
  66 + self.runtime = 0
66 67 rt
67 68 end
68 69  
... ... @@ -122,7 +123,8 @@ module Searchkick
122 123  
123 124 module ClassMethods
124 125 def log_process_action(payload)
125   - messages, runtime = super, payload[:searchkick_runtime]
  126 + messages = super
  127 + runtime = payload[:searchkick_runtime]
126 128 messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0
127 129 messages
128 130 end
... ...
lib/searchkick/model.rb
... ... @@ -2,7 +2,6 @@ module Searchkick
2 2 module Reindex; end # legacy for Searchjoy
3 3  
4 4 module Model
5   -
6 5 def searchkick(options = {})
7 6 raise "Only call searchkick once per model" if respond_to?(:searchkick_index)
8 7  
... ... @@ -94,9 +93,7 @@ module Searchkick
94 93 def should_index?
95 94 true
96 95 end unless method_defined?(:should_index?)
97   -
98 96 end
99 97 end
100   -
101 98 end
102 99 end
... ...
lib/searchkick/query.rb
... ... @@ -190,11 +190,9 @@ module Searchkick
190 190 if boost_by.is_a?(Array)
191 191 boost_by = Hash[boost_by.map { |f| [f, {factor: 1}] }]
192 192 elsif boost_by.is_a?(Hash)
193   - multiply_by, boost_by = boost_by.partition { |k,v| v[:boost_mode] == "multiply" }.map{ |i| Hash[i] }
194   - end
195   - if options[:boost]
196   - boost_by[options[:boost]] = {factor: 1}
  193 + multiply_by, boost_by = boost_by.partition { |_, v| v[:boost_mode] == "multiply" }.map { |i| Hash[i] }
197 194 end
  195 + boost_by[options[:boost]] = {factor: 1} if options[:boost]
198 196  
199 197 custom_filters.concat boost_filters(boost_by, log: true)
200 198 multiply_filters.concat boost_filters(multiply_by || {})
... ... @@ -209,12 +207,10 @@ module Searchkick
209 207 boost_where.each do |field, value|
210 208 if value.is_a?(Array) && value.first.is_a?(Hash)
211 209 value.each do |value_factor|
212   - value, factor = value_factor[:value], value_factor[:factor]
213   - custom_filters << custom_filter(field, value, factor)
  210 + custom_filters << custom_filter(field, value_factor[:value], value_factor[:factor])
214 211 end
215 212 elsif value.is_a?(Hash)
216   - value, factor = value[:value], value[:factor]
217   - custom_filters << custom_filter(field, value, factor)
  213 + custom_filters << custom_filter(field, value[:value], value[:factor])
218 214 else
219 215 factor = 1000
220 216 custom_filters << custom_filter(field, value, factor)
... ... @@ -227,7 +223,7 @@ module Searchkick
227 223 if !boost_by_distance[:field] || !boost_by_distance[:origin]
228 224 raise ArgumentError, "boost_by_distance requires :field and :origin"
229 225 end
230   - function_params = boost_by_distance.select { |k, v| [:origin, :scale, :offset, :decay].include?(k) }
  226 + function_params = boost_by_distance.select { |k, _| [:origin, :scale, :offset, :decay].include?(k) }
231 227 function_params[:origin] = function_params[:origin].reverse
232 228 custom_filters << {
233 229 boost_by_distance[:function] => {
... ... @@ -293,9 +289,7 @@ module Searchkick
293 289 # facets
294 290 if options[:facets]
295 291 facets = options[:facets] || {}
296   - if facets.is_a?(Array) # convert to more advanced syntax
297   - facets = Hash[facets.map { |f| [f, {}] }]
298   - end
  292 + facets = Hash[facets.map { |f| [f, {}] }] if facets.is_a?(Array) # convert to more advanced syntax
299 293  
300 294 payload[:facets] = {}
301 295 facets.each do |field, facet_options|
... ... @@ -331,7 +325,7 @@ module Searchkick
331 325 # offset is not possible
332 326 # http://elasticsearch-users.115913.n3.nabble.com/Is-pagination-possible-in-termsStatsFacet-td3422943.html
333 327  
334   - facet_options.deep_merge!(where: options.fetch(:where, {}).reject { |k| k == field } ) if options[:smart_facets] == true
  328 + facet_options.deep_merge!(where: options.fetch(:where, {}).reject { |k| k == field }) if options[:smart_facets] == true
335 329 facet_filters = where_filters(facet_options[:where])
336 330 if facet_filters.any?
337 331 payload[:facets][field][:facet_filter] = {
... ... @@ -348,9 +342,7 @@ module Searchkick
348 342 aggs = options[:aggs]
349 343 payload[:aggs] = {}
350 344  
351   - if aggs.is_a?(Array) # convert to more advanced syntax
352   - aggs = aggs.map { |f| [f, {}] }.to_h
353   - end
  345 + aggs = aggs.map { |f| [f, {}] }.to_h if aggs.is_a?(Array) # convert to more advanced syntax
354 346  
355 347 aggs.each do |field, agg_options|
356 348 size = agg_options[:limit] ? agg_options[:limit] : 100_000
... ... @@ -441,9 +433,7 @@ module Searchkick
441 433 end
442 434  
443 435 # routing
444   - if options[:routing]
445   - @routing = options[:routing]
446   - end
  436 + @routing = options[:routing] if options[:routing]
447 437 end
448 438  
449 439 @body = payload
... ... @@ -539,9 +529,7 @@ module Searchkick
539 529 value = {gte: value.first, (value.exclude_end? ? :lt : :lte) => value.last}
540 530 end
541 531  
542   - if value.is_a?(Array)
543   - value = {in: value}
544   - end
  532 + value = {in: value} if value.is_a?(Array)
545 533  
546 534 if value.is_a?(Hash)
547 535 value.each do |op, op_value|
... ... @@ -623,7 +611,7 @@ module Searchkick
623 611 def custom_filter(field, value, factor)
624 612 {
625 613 filter: {
626   - and: where_filters({field => value})
  614 + and: where_filters(field => value)
627 615 },
628 616 boost_factor: factor
629 617 }
... ...
lib/searchkick/reindex_job.rb
1 1 module Searchkick
2 2 class ReindexJob
3   -
4 3 def initialize(klass, id)
5 4 @klass = klass
6 5 @id = id
... ... @@ -23,6 +22,5 @@ module Searchkick
23 22 index.store record
24 23 end
25 24 end
26   -
27 25 end
28 26 end
... ...
lib/searchkick/reindex_v2_job.rb
... ... @@ -19,6 +19,5 @@ module Searchkick
19 19 index.store record
20 20 end
21 21 end
22   -
23 22 end
24 23 end
... ...
lib/searchkick/results.rb
... ... @@ -26,7 +26,7 @@ module Searchkick
26 26 # results can have different types
27 27 results = {}
28 28  
29   - hits.group_by { |hit, i| hit["_type"] }.each do |type, grouped_hits|
  29 + hits.group_by { |hit, _| hit["_type"] }.each do |type, grouped_hits|
30 30 results[type] = results_query(type.camelize.constantize, grouped_hits).to_a.index_by { |r| r.id.to_s }
31 31 end
32 32  
... ...
lib/searchkick/tasks.rb
1 1 require "rake"
2 2  
3 3 namespace :searchkick do
4   -
5 4 desc "reindex model"
6 5 task reindex: :environment do
7 6 if ENV["CLASS"]
... ... @@ -31,5 +30,4 @@ namespace :searchkick do
31 30 end
32 31  
33 32 end
34   -
35 33 end
... ...
test/aggs_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestAggs < Minitest::Test
4   -
  3 +class AggsTest < Minitest::Test
5 4 def setup
6 5 super
7 6 store [
... ... @@ -79,5 +78,4 @@ class TestAggs &lt; Minitest::Test
79 78 [field, buckets_as_hash(filtered_agg)]
80 79 end.to_h
81 80 end
82   -
83 81 end
... ...
test/autocomplete_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestAutocomplete < Minitest::Test
4   -
  3 +class AutocompleteTest < Minitest::Test
5 4 def test_autocomplete
6 5 store_names ["Hummus"]
7 6 assert_search "hum", ["Hummus"], autocomplete: true
... ... @@ -63,5 +62,4 @@ class TestAutocomplete &lt; Minitest::Test
63 62 store_names ["hi@example.org"]
64 63 assert_search "hi@example.org", ["hi@example.org"], fields: [{name: :exact}]
65 64 end
66   -
67 65 end
... ...
test/boost_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestBoost < Minitest::Test
4   -
  3 +class BoostTest < Minitest::Test
5 4 # conversions
6 5  
7 6 def test_conversions
... ... @@ -133,5 +132,4 @@ class TestBoost &lt; Minitest::Test
133 132 ]
134 133 assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: [37, -122], scale: "1000mi"}
135 134 end
136   -
137 135 end
... ...
test/facets_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestFacets < Minitest::Test
4   -
  3 +class FacetsTest < Minitest::Test
5 4 def setup
6 5 skip if elasticsearch2?
7 6 super
... ... @@ -79,14 +78,13 @@ class TestFacets &lt; Minitest::Test
79 78 skip if Gem::Version.new(Searchkick.server_version) >= Gem::Version.new("1.4.0")
80 79 options = {where: {store_id: 2}, facets: {store_id: {stats: true}}}
81 80 facets = Product.search("Product", options).facets["store_id"]["terms"]
82   - expected_facets_keys = %w[term count total_count min max total mean]
  81 + expected_facets_keys = %w(term count total_count min max total mean)
83 82 assert_equal expected_facets_keys, facets.first.keys
84 83 end
85 84  
86 85 protected
87 86  
88   - def store_facet(options, facet_key="store_id")
  87 + def store_facet(options, facet_key = "store_id")
89 88 Hash[Product.search("Product", options).facets[facet_key]["terms"].map { |v| [v["term"], v["count"]] }]
90 89 end
91   -
92 90 end
... ...
test/highlight_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestHighlight < Minitest::Test
4   -
  3 +class HighlightTest < Minitest::Test
5 4 def test_basic
6 5 store_names ["Two Door Cinema Club"]
7 6 assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
... ... @@ -54,5 +53,4 @@ class TestHighlight &lt; Minitest::Test
54 53 }
55 54 assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(json: json).with_details.first[1][:highlight][:"name.analyzed"]
56 55 end
57   -
58 56 end
... ...
test/index_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestIndex < Minitest::Test
4   -
  3 +class IndexTest < Minitest::Test
5 4 def test_clean_indices
6 5 old_index = Searchkick::Index.new("products_test_20130801000000000")
7 6 different_index = Searchkick::Index.new("items_test_20130801000000000")
... ... @@ -93,7 +92,7 @@ class TestIndex &lt; Minitest::Test
93 92 end
94 93  
95 94 def test_unsupported_version
96   - raises_exception = ->(s) { raise Elasticsearch::Transport::Transport::Error.new("[500] No query registered for [multi_match]") }
  95 + raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error.new("[500] No query registered for [multi_match]") }
97 96 Searchkick.client.stub :search, raises_exception do
98 97 assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test") }
99 98 end
... ... @@ -115,5 +114,4 @@ class TestIndex &lt; Minitest::Test
115 114 end
116 115  
117 116 end
118   -
119 117 end
... ...
test/inheritance_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestInheritance < Minitest::Test
4   -
  3 +class InheritanceTest < Minitest::Test
5 4 def test_child_reindex
6 5 store_names ["Max"], Cat
7 6 assert Dog.reindex
... ... @@ -76,5 +75,4 @@ class TestInheritance &lt; Minitest::Test
76 75 store_names ["Product B"], Animal
77 76 assert_search "product", ["Product A", "Product B"], index_name: [Product.searchkick_index.name, Animal.searchkick_index.name], conversions: false
78 77 end
79   -
80 78 end
... ...
test/match_test.rb
... ... @@ -2,8 +2,7 @@
2 2  
3 3 require_relative "test_helper"
4 4  
5   -class TestMatch < Minitest::Test
6   -
  5 +class MatchTest < Minitest::Test
7 6 # exact
8 7  
9 8 def test_match
... ... @@ -195,5 +194,4 @@ class TestMatch &lt; Minitest::Test
195 194 ]
196 195 assert_search "almond", []
197 196 end
198   -
199 197 end
... ...
test/model_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestModel < Minitest::Test
4   -
  3 +class ModelTest < Minitest::Test
5 4 def test_disable_callbacks_model
6 5 store_names ["product a"]
7 6  
... ... @@ -20,7 +19,7 @@ class TestModel &lt; Minitest::Test
20 19 def test_disable_callbacks_global
21 20 # make sure callbacks default to on
22 21 assert Searchkick.callbacks?
23   -
  22 +
24 23 store_names ["product a"]
25 24  
26 25 Searchkick.disable_callbacks
... ... @@ -34,5 +33,4 @@ class TestModel &lt; Minitest::Test
34 33  
35 34 assert_search "product", ["product a", "product b"]
36 35 end
37   -
38 36 end
... ...
test/query_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestQuery < Minitest::Test
4   -
  3 +class QueryTest < Minitest::Test
5 4 def test_basic
6 5 store_names ["Milk", "Apple"]
7 6 query = Product.search("milk", execute: false)
... ... @@ -10,5 +9,4 @@ class TestQuery &lt; Minitest::Test
10 9 query.body[:query] = {match_all: {}}
11 10 assert_equal ["Apple", "Milk"], query.execute.map(&:name).sort
12 11 end
13   -
14 12 end
... ...
test/records_test.rb
1 1 require_relative "test_helper"
2 2  
3 3 class RecordsTest < Minitest::Test
4   -
5 4 def test_records
6 5 store_names ["Milk", "Apple"]
7 6 assert_equal Product.search("milk").records.where(name: "Milk").count, 1
8 7 end
9   -
10 8 end
... ...
test/reindex_job_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestReindexJob < Minitest::Test
4   -
  3 +class ReindexJobTest < Minitest::Test
5 4 def setup
6 5 super
7 6 Searchkick.disable_callbacks
... ... @@ -29,5 +28,4 @@ class TestReindexJob &lt; Minitest::Test
29 28 Product.searchkick_index.refresh
30 29 assert_search "*", []
31 30 end
32   -
33 31 end
... ...
test/reindex_v2_job_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestReindexV2Job < Minitest::Test
4   -
  3 +class ReindexV2JobTest < Minitest::Test
5 4 def setup
6 5 skip unless defined?(ActiveJob)
7 6 super
... ... @@ -30,5 +29,4 @@ class TestReindexV2Job &lt; Minitest::Test
30 29 Product.searchkick_index.refresh
31 30 assert_search "*", []
32 31 end
33   -
34 32 end
... ...
test/routing_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestRouting < Minitest::Test
4   -
  3 +class RoutingTest < Minitest::Test
5 4 def test_routing_query
6 5 skip if elasticsearch2?
7 6 query = Store.search("Dollar Tree", routing: "Dollar Tree", execute: false)
... ... @@ -11,6 +10,6 @@ class TestRouting &lt; Minitest::Test
11 10 def test_routing_mappings
12 11 skip if elasticsearch2?
13 12 index_options = Store.searchkick_index.index_options
14   - assert_equal index_options[:mappings][:_default_][:_routing], {required: true, path: "name"}
  13 + assert_equal index_options[:mappings][:_default_][:_routing], required: true, path: "name"
15 14 end
16 15 end
... ...
test/should_index_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestShouldIndex < Minitest::Test
4   -
  3 +class ShouldIndexTest < Minitest::Test
5 4 def test_basic
6 5 store_names ["INDEX", "DO NOT INDEX"]
7 6 assert_search "index", ["INDEX"]
... ... @@ -30,5 +29,4 @@ class TestShouldIndex &lt; Minitest::Test
30 29 Product.searchkick_index.refresh
31 30 assert_search "index", []
32 31 end
33   -
34 32 end
... ...
test/similar_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestSimilar < Minitest::Test
4   -
  3 +class SimilarTest < Minitest::Test
5 4 def test_similar
6 5 store_names ["Annie's Naturals Organic Shiitake & Sesame Dressing"]
7 6 assert_search "Annie's Naturals Shiitake & Sesame Vinaigrette", ["Annie's Naturals Organic Shiitake & Sesame Dressing"], similar: true
... ... @@ -16,5 +15,4 @@ class TestSimilar &lt; Minitest::Test
16 15 store_names ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"]
17 16 assert_order "Lucerne Fat Free Chocolate Milk", ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"], similar: true
18 17 end
19   -
20 18 end
... ...
test/sql_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestSql < Minitest::Test
4   -
  3 +class SqlTest < Minitest::Test
5 4 def test_limit
6 5 store_names ["Product A", "Product B", "Product C", "Product D"]
7 6 assert_order "product", ["Product A", "Product B"], order: {name: :asc}, limit: 2
... ... @@ -333,7 +332,7 @@ class TestSql &lt; Minitest::Test
333 332 def test_select
334 333 store [{name: "Product A", store_id: 1}]
335 334 result = Product.search("product", load: false, select: [:name, :store_id]).first
336   - assert_equal %w[id name store_id], result.keys.reject { |k| k.start_with?("_") }.sort
  335 + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
337 336 assert_equal ["Product A"], result.name # this is not great
338 337 end
339 338  
... ... @@ -363,5 +362,4 @@ class TestSql &lt; Minitest::Test
363 362 assert Product.search("product", include: [:store]).first.association(:store).loaded?
364 363 end
365 364 end
366   -
367 365 end
... ...
test/suggest_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestSuggest < Minitest::Test
4   -
  3 +class SuggestTest < Minitest::Test
5 4 def test_basic
6 5 store_names ["Great White Shark", "Hammerhead Shark", "Tiger Shark"]
7 6 assert_suggest "How Big is a Tigre Shar", "how big is a tiger shark", fields: [:name]
... ... @@ -78,5 +77,4 @@ class TestSuggest &lt; Minitest::Test
78 77 def assert_suggest_all(term, expected, options = {})
79 78 assert_equal expected.sort, Product.search(term, options.merge(suggest: true)).suggestions.sort
80 79 end
81   -
82 80 end
... ...
test/synonyms_test.rb
1 1 require_relative "test_helper"
2 2  
3   -class TestSynonyms < Minitest::Test
4   -
  3 +class SynonymsTest < Minitest::Test
5 4 def test_bleach
6 5 store_names ["Clorox Bleach", "Kroger Bleach"]
7 6 assert_search "clorox", ["Clorox Bleach", "Kroger Bleach"]
... ... @@ -46,5 +45,4 @@ class TestSynonyms &lt; Minitest::Test
46 45 # store_names ["Creature", "Beast", "Dragon"], Animal
47 46 # assert_search "animal", ["Creature", "Beast"], {}, Animal
48 47 # end
49   -
50 48 end
... ...
test/test_helper.rb
... ... @@ -101,7 +101,7 @@ elsif defined?(NoBrainer)
101 101 field :color, type: String
102 102 field :latitude
103 103 field :longitude
104   - field :description, type: String
  104 + field :description, type: String
105 105  
106 106 belongs_to :store, validates: false
107 107 end
... ... @@ -230,10 +230,10 @@ class Store
230 230 mappings: {
231 231 store: {
232 232 properties: {
233   - name: {type: "string", analyzer: "keyword"},
  233 + name: {type: "string", analyzer: "keyword"}
234 234 }
235 235 }
236   - }
  236 + }
237 237 end
238 238  
239 239 class Animal
... ... @@ -252,7 +252,6 @@ Store.reindex
252 252 Animal.reindex
253 253  
254 254 class Minitest::Test
255   -
256 255 def setup
257 256 Product.destroy_all
258 257 Store.destroy_all
... ... @@ -284,5 +283,4 @@ class Minitest::Test
284 283 def assert_first(term, expected, options = {}, klass = Product)
285 284 assert_equal expected, klass.search(term, options).map(&:name).first
286 285 end
287   -
288 286 end
... ...