Commit fa5db0c87f3a76e38ce65d7e004a3878099473d8

Authored by Andrew Kane
1 parent a317c977

Added search_hit and search_highlights methods to models - closes #770

  1 +## 2.0.1
  2 +
  3 +- Added `search_hit` and `search_highlights` methods to models
  4 +
1 ## 2.0.0 5 ## 2.0.0
2 6
3 - Added support for `reindex` on associations 7 - Added support for `reindex` on associations
lib/searchkick/results.rb
@@ -32,7 +32,22 @@ module Searchkick @@ -32,7 +32,22 @@ module Searchkick
32 32
33 # sort 33 # sort
34 hits.map do |hit| 34 hits.map do |hit|
35 - results[hit["_type"]][hit["_id"].to_s] 35 + result = results[hit["_type"]][hit["_id"].to_s]
  36 + if result
  37 + unless result.respond_to?(:search_hit)
  38 + result.define_singleton_method(:search_hit) do
  39 + hit
  40 + end
  41 + end
  42 +
  43 + if hit["highlight"] && !result.respond_to?(:search_highlights)
  44 + highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]
  45 + result.define_singleton_method(:search_highlights) do
  46 + highlights
  47 + end
  48 + end
  49 + end
  50 + result
36 end.compact 51 end.compact
37 else 52 else
38 hits.map do |hit| 53 hits.map do |hit|
test/highlight_test.rb
@@ -3,42 +3,42 @@ require_relative "test_helper" @@ -3,42 +3,42 @@ require_relative "test_helper"
3 class HighlightTest < Minitest::Test 3 class HighlightTest < Minitest::Test
4 def test_basic 4 def test_basic
5 store_names ["Two Door Cinema Club"] 5 store_names ["Two Door Cinema Club"]
6 - assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] 6 + assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).first.search_highlights[:name]
7 end 7 end
8 8
9 def test_tag 9 def test_tag
10 store_names ["Two Door Cinema Club"] 10 store_names ["Two Door Cinema Club"]
11 - assert_equal "Two Door <strong>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong>"}).with_details.first[1][:highlight][:name] 11 + assert_equal "Two Door <strong>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong>"}).first.search_highlights[:name]
12 end 12 end
13 13
14 def test_multiple_fields 14 def test_multiple_fields
15 store [{name: "Two Door Cinema Club", color: "Cinema Orange"}] 15 store [{name: "Two Door Cinema Club", color: "Cinema Orange"}]
16 - highlight = Product.search("cinema", fields: [:name, :color], highlight: true).with_details.first[1][:highlight]  
17 - assert_equal "Two Door <em>Cinema</em> Club", highlight[:name]  
18 - assert_equal "<em>Cinema</em> Orange", highlight[:color] 16 + highlights = Product.search("cinema", fields: [:name, :color], highlight: true).first.search_highlights
  17 + assert_equal "Two Door <em>Cinema</em> Club", highlights[:name]
  18 + assert_equal "<em>Cinema</em> Orange", highlights[:color]
19 end 19 end
20 20
21 def test_fields 21 def test_fields
22 store [{name: "Two Door Cinema Club", color: "Cinema Orange"}] 22 store [{name: "Two Door Cinema Club", color: "Cinema Orange"}]
23 - highlight = Product.search("cinema", fields: [:name, :color], highlight: {fields: [:name]}).with_details.first[1][:highlight]  
24 - assert_equal "Two Door <em>Cinema</em> Club", highlight[:name]  
25 - assert_nil highlight[:color] 23 + highlights = Product.search("cinema", fields: [:name, :color], highlight: {fields: [:name]}).first.search_highlights
  24 + assert_equal "Two Door <em>Cinema</em> Club", highlights[:name]
  25 + assert_nil highlights[:color]
26 end 26 end
27 27
28 def test_field_options 28 def test_field_options
29 store_names ["Two Door Cinema Club are a Northern Irish indie rock band"] 29 store_names ["Two Door Cinema Club are a Northern Irish indie rock band"]
30 fragment_size = ENV["MATCH"] == "word_start" ? 26 : 20 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 + assert_equal "Two Door <em>Cinema</em> Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: fragment_size}}}).first.search_highlights[:name]
32 end 32 end
33 33
34 def test_multiple_words 34 def test_multiple_words
35 store_names ["Hello World Hello"] 35 store_names ["Hello World Hello"]
36 - assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] 36 + assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).first.search_highlights[:name]
37 end 37 end
38 38
39 def test_encoder 39 def test_encoder
40 store_names ["<b>Hello</b>"] 40 store_names ["<b>Hello</b>"]
41 - assert_equal "&lt;b&gt;<em>Hello</em>&lt;&#x2F;b&gt;", Product.search("hello", fields: [:name], highlight: {encoder: "html"}, misspellings: false).with_details.first[1][:highlight][:name] 41 + assert_equal "&lt;b&gt;<em>Hello</em>&lt;&#x2F;b&gt;", Product.search("hello", fields: [:name], highlight: {encoder: "html"}, misspellings: false).first.search_highlights[:name]
42 end 42 end
43 43
44 def test_body 44 def test_body
@@ -58,6 +58,11 @@ class HighlightTest &lt; Minitest::Test @@ -58,6 +58,11 @@ class HighlightTest &lt; Minitest::Test
58 } 58 }
59 } 59 }
60 } 60 }
61 - assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(body: body).with_details.first[1][:highlight][:"name.analyzed"] 61 + assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(body: body).first.search_highlights[:"name.analyzed"]
  62 + end
  63 +
  64 + def test_legacy
  65 + store_names ["Two Door Cinema Club"]
  66 + assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
62 end 67 end
63 end 68 end