Commit 32a8a2aa603d2b8a0a0c5ec201e07bc17ee0fb24

Authored by Andrew Kane
1 parent 5454af25

Better highlighted fields

@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 2
3 - Added `below` option to misspellings to improve performance 3 - Added `below` option to misspellings to improve performance
4 - Added `match` option 4 - Added `match` option
5 -- Added `word: false` option 5 +- Added `word` option
6 - Added highlighted fields to `load: false` 6 - Added highlighted fields to `load: false`
7 7
8 ## 1.0.3 8 ## 1.0.3
lib/searchkick/query.rb
@@ -96,7 +96,8 @@ module Searchkick @@ -96,7 +96,8 @@ module Searchkick
96 load: @load, 96 load: @load,
97 includes: options[:include] || options[:includes], 97 includes: options[:include] || options[:includes],
98 json: !options[:json].nil?, 98 json: !options[:json].nil?,
99 - match_suffix: @match_suffix 99 + match_suffix: @match_suffix,
  100 + highlighted_fields: @highlighted_fields || []
100 } 101 }
101 Searchkick::Results.new(searchkick_klass, response, opts) 102 Searchkick::Results.new(searchkick_klass, response, opts)
102 end 103 end
@@ -544,6 +545,8 @@ module Searchkick @@ -544,6 +545,8 @@ module Searchkick
544 end 545 end
545 end 546 end
546 end 547 end
  548 +
  549 + @highlighted_fields = payload[:highlight][:fields].keys.map { |k| k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end)\z/, "") }
547 end 550 end
548 551
549 # An empty array will cause only the _id and _type for each hit to be returned 552 # An empty array will cause only the _id and _type for each hit to be returned
lib/searchkick/results.rb
@@ -45,7 +45,7 @@ module Searchkick @@ -45,7 +45,7 @@ module Searchkick
45 45
46 if hit["highlight"] 46 if hit["highlight"]
47 highlight = Hash[hit["highlight"].map { |k, v| [k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end)\z/, ""), v.first] }] 47 highlight = Hash[hit["highlight"].map { |k, v| [k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end)\z/, ""), v.first] }]
48 - result.keys.each do |k| 48 + options[:highlighted_fields].each do |k|
49 result["highlighted_#{k}"] ||= (highlight[k] || result[k]) 49 result["highlighted_#{k}"] ||= (highlight[k] || result[k])
50 end 50 end
51 end 51 end