Commit 32a8a2aa603d2b8a0a0c5ec201e07bc17ee0fb24

Authored by Andrew Kane
1 parent 5454af25

Better highlighted fields

CHANGELOG.md
... ... @@ -2,7 +2,7 @@
2 2  
3 3 - Added `below` option to misspellings to improve performance
4 4 - Added `match` option
5   -- Added `word: false` option
  5 +- Added `word` option
6 6 - Added highlighted fields to `load: false`
7 7  
8 8 ## 1.0.3
... ...
lib/searchkick/query.rb
... ... @@ -96,7 +96,8 @@ module Searchkick
96 96 load: @load,
97 97 includes: options[:include] || options[:includes],
98 98 json: !options[:json].nil?,
99   - match_suffix: @match_suffix
  99 + match_suffix: @match_suffix,
  100 + highlighted_fields: @highlighted_fields || []
100 101 }
101 102 Searchkick::Results.new(searchkick_klass, response, opts)
102 103 end
... ... @@ -544,6 +545,8 @@ module Searchkick
544 545 end
545 546 end
546 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 550 end
548 551  
549 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 45  
46 46 if hit["highlight"]
47 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 49 result["highlighted_#{k}"] ||= (highlight[k] || result[k])
50 50 end
51 51 end
... ...