Commit 9d067a770e605fa71bc28ad328532937278d58c4

Authored by Andrew Kane
1 parent fded93c7

Added highlight option - closes #265

README.md
... ... @@ -539,6 +539,14 @@ Product.search "*", facets: {store_id: {stats: true}}
539 539  
540 540 ### Highlight
541 541  
  542 +Specify which fields to index with highlighting. [master]
  543 +
  544 +```ruby
  545 +class Product < ActiveRecord::Base
  546 + searchkick highlight: [:name]
  547 +end
  548 +```
  549 +
542 550 Highlight the search query in the results.
543 551  
544 552 ```ruby
... ...
lib/searchkick/reindex.rb
... ... @@ -261,7 +261,7 @@ module Searchkick
261 261 end
262 262  
263 263 mapping_options = Hash[
264   - [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end]
  264 + [:autocomplete, :suggest, :text_start, :text_middle, :text_end, :word_start, :word_middle, :word_end, :highlight]
265 265 .map{|type| [type, (options[type] || []).map(&:to_s)] }
266 266 ]
267 267  
... ... @@ -276,12 +276,16 @@ module Searchkick
276 276 }
277 277 }
278 278  
279   - mapping_options.each do |type, fields|
  279 + mapping_options.except(:highlight).each do |type, fields|
280 280 if fields.include?(field)
281 281 field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
282 282 end
283 283 end
284 284  
  285 + if mapping_options[:highlight].include?(field)
  286 + field_mapping[:fields]["analyzed"][:term_vector] = "with_positions_offsets"
  287 + end
  288 +
285 289 mapping[field] = field_mapping
286 290 end
287 291  
... ...
test/highlight_test.rb
... ... @@ -20,7 +20,6 @@ class TestHighlight &lt; Minitest::Test
20 20 end
21 21  
22 22 def test_multiple_words
23   - skip "Issue #265"
24 23 store_names ["Hello World Hello"]
25 24 assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
26 25 end
... ...
test/test_helper.rb
... ... @@ -141,7 +141,8 @@ class Product
141 141 text_end: [:name],
142 142 word_start: [:name],
143 143 word_middle: [:name],
144   - word_end: [:name]
  144 + word_end: [:name],
  145 + highlight: [:name]
145 146  
146 147 attr_accessor :conversions, :user_ids, :aisle
147 148  
... ...