diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb
index 124aafb..4ce3659 100644
--- a/lib/searchkick/index.rb
+++ b/lib/searchkick/index.rb
@@ -426,7 +426,7 @@ module Searchkick
end
mapping_options.except(:highlight).each do |type, fields|
- if fields.include?(field)
+ if options[:match] == type || fields.include?(field)
field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
end
end
diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb
index c201100..c7e2735 100644
--- a/lib/searchkick/query.rb
+++ b/lib/searchkick/query.rb
@@ -22,6 +22,7 @@ module Searchkick
@klass = klass
@term = term
@options = options
+ @match_suffix = options[:match] || searchkick_options[:match] || "analyzed"
boost_fields = {}
fields =
@@ -436,7 +437,7 @@ module Searchkick
payload[:highlight][:fields] = {}
highlight_fields.each do |name, opts|
- payload[:highlight][:fields]["#{name}.analyzed"] = opts || {}
+ payload[:highlight][:fields]["#{name}.#{@match_suffix}"] = opts || {}
end
end
end
@@ -530,7 +531,8 @@ module Searchkick
padding: @padding,
load: @load,
includes: options[:include] || options[:includes],
- json: !options[:json].nil?
+ json: !options[:json].nil?,
+ match_suffix: @match_suffix
}
Searchkick::Results.new(searchkick_klass, response, opts)
end
diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb
index 875c407..7d66f88 100644
--- a/lib/searchkick/results.rb
+++ b/lib/searchkick/results.rb
@@ -73,7 +73,7 @@ module Searchkick
each_with_hit.map do |model, hit|
details = {}
if hit["highlight"]
- details[:highlight] = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.analyzed\z/, "")).to_sym, v.first] }]
+ details[:highlight] = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]
end
[model, details]
end
diff --git a/test/highlight_test.rb b/test/highlight_test.rb
index d9a74ff..41d3555 100644
--- a/test/highlight_test.rb
+++ b/test/highlight_test.rb
@@ -27,7 +27,8 @@ class HighlightTest < Minitest::Test
def test_field_options
store_names ["Two Door Cinema Club are a Northern Irish indie rock band"]
- assert_equal "Two Door Cinema Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 20}}}).with_details.first[1][:highlight][:name]
+ fragment_size = ENV["MATCH"] == "word_start" ? 26 : 20
+ assert_equal "Two Door Cinema Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: fragment_size}}}).with_details.first[1][:highlight][:name]
end
def test_multiple_words
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 743b87f..22edeab 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -216,7 +216,8 @@ class Product
word_middle: [:name],
word_end: [:name],
highlight: [:name],
- unsearchable: [:description]
+ unsearchable: [:description],
+ match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil
attr_accessor :conversions, :user_ids, :aisle
--
libgit2 0.21.0