Commit 295bd6686b4d6449efd661f0a9cf7c14d56e68b1
1 parent
f36fbf83
Exists in
master
and in
21 other branches
Added rough version of highlight for #51
Showing
3 changed files
with
40 additions
and
1 deletions
Show diff stats
README.md
@@ -342,6 +342,32 @@ Advanced | @@ -342,6 +342,32 @@ Advanced | ||
342 | Product.search "2% Milk", facets: {store_id: {where: {in_stock: true}, limit: 10}} | 342 | Product.search "2% Milk", facets: {store_id: {where: {in_stock: true}, limit: 10}} |
343 | ``` | 343 | ``` |
344 | 344 | ||
345 | +### Highlight [master - subject to change] | ||
346 | + | ||
347 | +Highlight the search query in the results. | ||
348 | + | ||
349 | +```ruby | ||
350 | +bands = Band.search "cinema", fields: [:name], highlight: true | ||
351 | +``` | ||
352 | + | ||
353 | +**Note:** The `fields` option is required. | ||
354 | + | ||
355 | +View the highlighted fields with: | ||
356 | + | ||
357 | +```ruby | ||
358 | +bands.each_with_hit do |band, hit| | ||
359 | + puts hit["highlight"]["name.analyzed"].first # "Two Door <em>Cinema</em> Club" | ||
360 | +end | ||
361 | +``` | ||
362 | + | ||
363 | +**Note:** A simpler interface coming before the next release. | ||
364 | + | ||
365 | +To change the tag, use: | ||
366 | + | ||
367 | +```ruby | ||
368 | +Band.search "cinema", fields: [:name], highlight: {tag: "<strong>"} | ||
369 | +``` | ||
370 | + | ||
345 | ### Similar Items | 371 | ### Similar Items |
346 | 372 | ||
347 | Find similar items. | 373 | Find similar items. |
lib/searchkick/reindex.rb
@@ -202,7 +202,9 @@ module Searchkick | @@ -202,7 +202,9 @@ module Searchkick | ||
202 | type: "multi_field", | 202 | type: "multi_field", |
203 | fields: { | 203 | fields: { |
204 | field => {type: "string", index: "not_analyzed"}, | 204 | field => {type: "string", index: "not_analyzed"}, |
205 | - "analyzed" => {type: "string", index: "analyzed"} | 205 | + "analyzed" => {type: "string", index: "analyzed", term_vector: "with_positions_offsets"} |
206 | + # term_vector for fast / correct highlighting | ||
207 | + # http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_fast_vector_highlighter | ||
206 | } | 208 | } |
207 | } | 209 | } |
208 | if autocomplete.include?(field) | 210 | if autocomplete.include?(field) |
lib/searchkick/search.rb
@@ -288,6 +288,17 @@ module Searchkick | @@ -288,6 +288,17 @@ module Searchkick | ||
288 | end | 288 | end |
289 | end | 289 | end |
290 | 290 | ||
291 | + # highlight | ||
292 | + if options[:highlight] | ||
293 | + payload[:highlight] = { | ||
294 | + fields: Hash[ fields.map{|f| [f, {}] } ] | ||
295 | + } | ||
296 | + if options[:highlight].is_a?(Hash) and tag = options[:highlight][:tag] | ||
297 | + payload[:highlight][:pre_tags] = [tag] | ||
298 | + payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")] | ||
299 | + end | ||
300 | + end | ||
301 | + | ||
291 | # An empty array will cause only the _id and _type for each hit to be returned | 302 | # An empty array will cause only the _id and _type for each hit to be returned |
292 | # http://www.elasticsearch.org/guide/reference/api/search/fields/ | 303 | # http://www.elasticsearch.org/guide/reference/api/search/fields/ |
293 | payload[:fields] = [] if load | 304 | payload[:fields] = [] if load |