diff --git a/README.md b/README.md index 3e9e521..1b7555a 100644 --- a/README.md +++ b/README.md @@ -539,6 +539,14 @@ Product.search "*", facets: {store_id: {stats: true}} ### Highlight +Specify which fields to index with highlighting. [master] + +```ruby +class Product < ActiveRecord::Base + searchkick highlight: [:name] +end +``` + Highlight the search query in the results. ```ruby diff --git a/lib/searchkick/reindex.rb b/lib/searchkick/reindex.rb index 13235a5..861eb04 100644 --- a/lib/searchkick/reindex.rb +++ b/lib/searchkick/reindex.rb @@ -261,7 +261,7 @@ module Searchkick end mapping_options = Hash[ - [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end] + [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight] .map{|type| [type, (options[type] || []).map(&:to_s)] } ] @@ -276,12 +276,16 @@ module Searchkick } } - mapping_options.each do |type, fields| + mapping_options.except(:highlight).each do |type, fields| if fields.include?(field) field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"} end end + if mapping_options[:highlight].include?(field) + field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets" + end + mapping[field] = field_mapping end diff --git a/test/highlight_test.rb b/test/highlight_test.rb index 91188cd..9069ecc 100644 --- a/test/highlight_test.rb +++ b/test/highlight_test.rb @@ -20,7 +20,6 @@ class TestHighlight < Minitest::Test end def test_multiple_words - skip "Issue #265" store_names ["Hello World Hello"] assert_equal "Hello World Hello", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5272029..0094da0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -141,7 +141,8 @@ class Product text_end: [:name], word_start: [:name], word_middle: [:name], - word_end: [:name] + word_end: [:name], + highlight: [:name] attr_accessor :conversions, :user_ids, :aisle -- libgit2 0.21.0