Commit fa5db0c87f3a76e38ce65d7e004a3878099473d8

Authored by Andrew Kane
1 parent a317c977

Added search_hit and search_highlights methods to models - closes #770

CHANGELOG.md
  1 +## 2.0.1
  2 +
  3 +- Added `search_hit` and `search_highlights` methods to models
  4 +
1 5 ## 2.0.0
2 6  
3 7 - Added support for `reindex` on associations
... ...
lib/searchkick/results.rb
... ... @@ -32,7 +32,22 @@ module Searchkick
32 32  
33 33 # sort
34 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 51 end.compact
37 52 else
38 53 hits.map do |hit|
... ...
test/highlight_test.rb
... ... @@ -3,42 +3,42 @@ require_relative "test_helper"
3 3 class HighlightTest < Minitest::Test
4 4 def test_basic
5 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 7 end
8 8  
9 9 def test_tag
10 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 12 end
13 13  
14 14 def test_multiple_fields
15 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 19 end
20 20  
21 21 def test_fields
22 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 26 end
27 27  
28 28 def test_field_options
29 29 store_names ["Two Door Cinema Club are a Northern Irish indie rock band"]
30 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 32 end
33 33  
34 34 def test_multiple_words
35 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 37 end
38 38  
39 39 def test_encoder
40 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 42 end
43 43  
44 44 def test_body
... ... @@ -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 67 end
63 68 end
... ...