From b5bfe4c54c0c540b8486d8cd1b9c2c2cca178183 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 10 May 2018 09:12:34 -0700 Subject: [PATCH] Fixed error with highlights and match all - fixes #1140 --- CHANGELOG.md | 1 + lib/searchkick/query.rb | 1 + lib/searchkick/results.rb | 18 +++++++++++++----- test/highlight_test.rb | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17eaa02..fd3ef70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Added `:inline` as alias for `true` for `callbacks` and `mode` options - Friendlier error message for bad mapping with partial matches - Warn when records in search index do not exist in database +- Fixed error with highlights and match all ## 3.0.3 diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 38d67b6..d89ed1c 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -111,6 +111,7 @@ module Searchkick model_includes: options[:model_includes], json: !@json.nil?, match_suffix: @match_suffix, + highlight: options[:highlight], highlighted_fields: @highlighted_fields || [], misspellings: @misspellings, term: term, diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 8ef1147..9212ecb 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -31,8 +31,8 @@ module Searchkick hits.map do |hit| result = results[hit["_type"]][hit["_id"].to_s] if result && !(options[:load].is_a?(Hash) && options[:load][:dumpable]) - if hit["highlight"] && !result.respond_to?(:search_highlights) - highlights = Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, v.first] }] + if options[:highlight] && !result.respond_to?(:search_highlights) + highlights = hit_highlights(hit) result.define_singleton_method(:search_highlights) do highlights end @@ -57,8 +57,8 @@ module Searchkick hit end - if hit["highlight"] - highlight = Hash[hit["highlight"].map { |k, v| [base_field(k), v.first] }] + if @options[:highlight] + highlight = Hash[hit["highlight"].to_a.map { |k, v| [base_field(k), v.first] }] options[:highlighted_fields].map { |k| base_field(k) }.each do |k| result["highlighted_#{k}"] ||= (highlight[k] || result[k]) end @@ -185,7 +185,7 @@ module Searchkick def highlights(multiple: false) hits.map do |hit| - Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, multiple ? v : v.first] }] + hit_highlights(hit, multiple: multiple) end end @@ -238,5 +238,13 @@ module Searchkick def base_field(k) k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "") end + + def hit_highlights(hit, multiple: false) + if hit["highlight"] + Hash[hit["highlight"].map { |k, v| [(options[:json] ? k : k.sub(/\.#{@options[:match_suffix]}\z/, "")).to_sym, multiple ? v : v.first] }] + else + {} + end + end end end diff --git a/test/highlight_test.rb b/test/highlight_test.rb index 0f6c501..2caa99c 100644 --- a/test/highlight_test.rb +++ b/test/highlight_test.rb @@ -91,4 +91,19 @@ class HighlightTest < Minitest::Test store_names ["Two Door Cinema Club"] assert_equal "Two Door Cinema Club", Product.search("cinema", highlight: true).first.search_highlights[:name] end + + def test_match_all + store_names ["Two Door Cinema Club"] + assert_nil Product.search("*", highlight: true).highlights.first[:name] + end + + def test_match_all_load_false + store_names ["Two Door Cinema Club"] + assert_nil Product.search("*", highlight: true, load: false).highlights.first[:name] + end + + def test_match_all_search_highlights + store_names ["Two Door Cinema Club"] + assert_nil Product.search("*", highlight: true).first.search_highlights[:name] + end end -- libgit2 0.21.0