Commit cd7c1bf0e63a7b079c119fcf691e1ab9c3c29990

Authored by Andrew Kane
1 parent 049fbfd7

Fixed tests for match option

lib/searchkick/index.rb
... ... @@ -426,7 +426,7 @@ module Searchkick
426 426 end
427 427  
428 428 mapping_options.except(:highlight).each do |type, fields|
429   - if fields.include?(field)
  429 + if options[:match] == type || fields.include?(field)
430 430 field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
431 431 end
432 432 end
... ...
lib/searchkick/query.rb
... ... @@ -22,6 +22,7 @@ module Searchkick
22 22 @klass = klass
23 23 @term = term
24 24 @options = options
  25 + @match_suffix = options[:match] || searchkick_options[:match] || "analyzed"
25 26  
26 27 boost_fields = {}
27 28 fields =
... ... @@ -436,7 +437,7 @@ module Searchkick
436 437 payload[:highlight][:fields] = {}
437 438  
438 439 highlight_fields.each do |name, opts|
439   - payload[:highlight][:fields]["#{name}.analyzed"] = opts || {}
  440 + payload[:highlight][:fields]["#{name}.#{@match_suffix}"] = opts || {}
440 441 end
441 442 end
442 443 end
... ... @@ -530,7 +531,8 @@ module Searchkick
530 531 padding: @padding,
531 532 load: @load,
532 533 includes: options[:include] || options[:includes],
533   - json: !options[:json].nil?
  534 + json: !options[:json].nil?,
  535 + match_suffix: @match_suffix
534 536 }
535 537 Searchkick::Results.new(searchkick_klass, response, opts)
536 538 end
... ...
lib/searchkick/results.rb
... ... @@ -73,7 +73,7 @@ module Searchkick
73 73 each_with_hit.map do |model, hit|
74 74 details = {}
75 75 if hit["highlight"]
76   - details[:highlight] = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.analyzed\z/, "")).to_sym, v.first] }]
  76 + details[:highlight] = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]
77 77 end
78 78 [model, details]
79 79 end
... ...
test/highlight_test.rb
... ... @@ -27,7 +27,8 @@ class HighlightTest < Minitest::Test
27 27  
28 28 def test_field_options
29 29 store_names ["Two Door Cinema Club are a Northern Irish indie rock band"]
30   - assert_equal "Two Door <em>Cinema</em> Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 20}}}).with_details.first[1][:highlight][:name]
  30 + fragment_size = ENV["MATCH"] == "word_start" ? 26 : 20
  31 + assert_equal "Two Door <em>Cinema</em> Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: fragment_size}}}).with_details.first[1][:highlight][:name]
31 32 end
32 33  
33 34 def test_multiple_words
... ...
test/test_helper.rb
... ... @@ -216,7 +216,8 @@ class Product
216 216 word_middle: [:name],
217 217 word_end: [:name],
218 218 highlight: [:name],
219   - unsearchable: [:description]
  219 + unsearchable: [:description],
  220 + match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil
220 221  
221 222 attr_accessor :conversions, :user_ids, :aisle
222 223  
... ...