Commit ef2d577dc0d49f2c8a4cd80fd19b23041988867b

Authored by Andrew Kane
1 parent 7ec312d6

Added with_details method

README.md
... ... @@ -348,7 +348,7 @@ Advanced
348 348 Product.search "2% Milk", facets: {store_id: {where: {in_stock: true}, limit: 10}}
349 349 ```
350 350  
351   -### Highlight [master - subject to change]
  351 +### Highlight [master]
352 352  
353 353 Highlight the search query in the results.
354 354  
... ... @@ -361,13 +361,11 @@ bands = Band.search "cinema", fields: [:name], highlight: true
361 361 View the highlighted fields with:
362 362  
363 363 ```ruby
364   -bands.each_with_hit do |band, hit|
365   - puts hit["highlight"]["name.analyzed"].first # "Two Door <em>Cinema</em> Club"
  364 +bands.with_details.each do |band, details|
  365 + puts details[:highlight][:name] # "Two Door <em>Cinema</em> Club"
366 366 end
367 367 ```
368 368  
369   -**Note:** A simpler interface coming before the next release.
370   -
371 369 To change the tag, use:
372 370  
373 371 ```ruby
... ...
lib/searchkick/results.rb
... ... @@ -9,6 +9,16 @@ module Searchkick
9 9 end
10 10 end
11 11  
  12 + def with_details
  13 + each_with_hit.map do |model, hit|
  14 + details = {}
  15 + if hit["highlight"]
  16 + details[:highlight] = Hash[ hit["highlight"].map{|k, v| [k.sub(/\.analyzed\z/, "").to_sym, v.first] } ]
  17 + end
  18 + [model, details]
  19 + end
  20 + end
  21 +
12 22 # fixes deprecation warning
13 23 def __find_records_by_ids(klass, ids)
14 24 @options[:load] === true ? klass.find(ids) : klass.includes(@options[:load][:include]).find(ids)
... ...
test/highlight_test.rb
... ... @@ -4,19 +4,19 @@ class TestHighlight &lt; Minitest::Unit::TestCase
4 4  
5 5 def test_basic
6 6 store_names ["Two Door Cinema Club"]
7   - assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).each_with_hit.first[1]["highlight"]["name.analyzed"].first
  7 + assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
8 8 end
9 9  
10 10 def test_tag
11 11 store_names ["Two Door Cinema Club"]
12   - assert_equal "Two Door <strong>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong>"}).each_with_hit.first[1]["highlight"]["name.analyzed"].first
  12 + assert_equal "Two Door <strong>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong>"}).with_details.first[1][:highlight][:name]
13 13 end
14 14  
15 15 def test_multiple_fields
16 16 store [{name: "Two Door Cinema Club", color: "Cinema Orange"}]
17   - highlight = Product.search("cinema", fields: [:name, :color], highlight: true).each_with_hit.first[1]["highlight"]
18   - assert_equal "Two Door <em>Cinema</em> Club", highlight["name.analyzed"].first
19   - assert_equal "<em>Cinema</em> Orange", highlight["color.analyzed"].first
  17 + highlight = Product.search("cinema", fields: [:name, :color], highlight: true).with_details.first[1][:highlight]
  18 + assert_equal "Two Door <em>Cinema</em> Club", highlight[:name]
  19 + assert_equal "<em>Cinema</em> Orange", highlight[:color]
20 20 end
21 21  
22 22 end
... ...