Commit b5bfe4c54c0c540b8486d8cd1b9c2c2cca178183

Authored by Andrew
1 parent 85bb8db3

Fixed error with highlights and match all - fixes #1140

CHANGELOG.md
... ... @@ -3,6 +3,7 @@
3 3 - Added `:inline` as alias for `true` for `callbacks` and `mode` options
4 4 - Friendlier error message for bad mapping with partial matches
5 5 - Warn when records in search index do not exist in database
  6 +- Fixed error with highlights and match all
6 7  
7 8 ## 3.0.3
8 9  
... ...
lib/searchkick/query.rb
... ... @@ -111,6 +111,7 @@ module Searchkick
111 111 model_includes: options[:model_includes],
112 112 json: !@json.nil?,
113 113 match_suffix: @match_suffix,
  114 + highlight: options[:highlight],
114 115 highlighted_fields: @highlighted_fields || [],
115 116 misspellings: @misspellings,
116 117 term: term,
... ...
lib/searchkick/results.rb
... ... @@ -31,8 +31,8 @@ module Searchkick
31 31 hits.map do |hit|
32 32 result = results[hit["_type"]][hit["_id"].to_s]
33 33 if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable])
34   - if hit["highlight"] && !result.respond_to?(:search_highlights)
35   - highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }]
  34 + if options[:highlight] && !result.respond_to?(:search_highlights)
  35 + highlights = hit_highlights(hit)
36 36 result.define_singleton_method(:search_highlights) do
37 37 highlights
38 38 end
... ... @@ -57,8 +57,8 @@ module Searchkick
57 57 hit
58 58 end
59 59  
60   - if hit["highlight"]
61   - highlight = Hash[hit["highlight"].map { |k, v| [base_field(k), v.first] }]
  60 + if @options[:highlight]
  61 + highlight = Hash[hit["highlight"].to_a.map { |k, v| [base_field(k), v.first] }]
62 62 options[:highlighted_fields].map { |k| base_field(k) }.each do |k|
63 63 result["highlighted_#{k}"] ||= (highlight[k] || result[k])
64 64 end
... ... @@ -185,7 +185,7 @@ module Searchkick
185 185  
186 186 def highlights(multiple: false)
187 187 hits.map do |hit|
188   - Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, multiple ? v : v.first] }]
  188 + hit_highlights(hit, multiple: multiple)
189 189 end
190 190 end
191 191  
... ... @@ -238,5 +238,13 @@ module Searchkick
238 238 def base_field(k)
239 239 k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "")
240 240 end
  241 +
  242 + def hit_highlights(hit, multiple: false)
  243 + if hit["highlight"]
  244 + Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, multiple ? v : v.first] }]
  245 + else
  246 + {}
  247 + end
  248 + end
241 249 end
242 250 end
... ...
test/highlight_test.rb
... ... @@ -91,4 +91,19 @@ class HighlightTest < Minitest::Test
91 91 store_names ["Two Door Cinema Club"]
92 92 assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", highlight: true).first.search_highlights[:name]
93 93 end
  94 +
  95 + def test_match_all
  96 + store_names ["Two Door Cinema Club"]
  97 + assert_nil Product.search("*", highlight: true).highlights.first[:name]
  98 + end
  99 +
  100 + def test_match_all_load_false
  101 + store_names ["Two Door Cinema Club"]
  102 + assert_nil Product.search("*", highlight: true, load: false).highlights.first[:name]
  103 + end
  104 +
  105 + def test_match_all_search_highlights
  106 + store_names ["Two Door Cinema Club"]
  107 + assert_nil Product.search("*", highlight: true).first.search_highlights[:name]
  108 + end
94 109 end
... ...