Commit 9eeb97f2ea8eb8f1d28a185e1f82f5fb8e0e4cd0

Authored by Andrew Kane
1 parent 82e5db4a

Better highlight options

  1 +## 0.8.4 [unreleased]
  2 +
  3 +- More flexible highlight options
  4 +
1 ## 0.8.3 5 ## 0.8.3
2 6
3 - Added support for ActiveJob 7 - Added support for ActiveJob
@@ -553,7 +553,7 @@ Highlight the search query in the results. @@ -553,7 +553,7 @@ Highlight the search query in the results.
553 bands = Band.search "cinema", fields: [:name], highlight: true 553 bands = Band.search "cinema", fields: [:name], highlight: true
554 ``` 554 ```
555 555
556 -**Note:** The `fields` option is required, unless highlight options are given (see below). 556 +**Note:** The `fields` option is required, unless highlight options are given - see below.
557 557
558 View the highlighted fields with: 558 View the highlighted fields with:
559 559
@@ -569,22 +569,20 @@ To change the tag, use: @@ -569,22 +569,20 @@ To change the tag, use:
569 Band.search "cinema", fields: [:name], highlight: {tag: "<strong>"} 569 Band.search "cinema", fields: [:name], highlight: {tag: "<strong>"}
570 ``` 570 ```
571 571
572 -To highlight and search different fields, use: 572 +To highlight and search different fields, use: [master]
573 573
574 ```ruby 574 ```ruby
575 -Band.search "cinema", fields: [:name], highlight: { fields: [:description] } 575 +Band.search "cinema", fields: [:name], highlight: {fields: [:description]}
576 ``` 576 ```
577 577
578 -Additional options, including fragment size, can be specified for each field: 578 +Additional options, including fragment size, can be specified for each field: [master]
579 579
580 ```ruby 580 ```ruby
581 -Band.search "cinema", fields: [:name], highlight: { fields: { name: { fragment_size: 200 } } } 581 +Band.search "cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 200}}}
582 ``` 582 ```
583 583
584 You can find available highlight options in the [Elasticsearch reference](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_highlighted_fragments). 584 You can find available highlight options in the [Elasticsearch reference](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_highlighted_fragments).
585 585
586 -  
587 -  
588 ### Similar Items 586 ### Similar Items
589 587
590 Find similar items. 588 Find similar items.
lib/searchkick/query.rb
@@ -327,7 +327,8 @@ module Searchkick @@ -327,7 +327,8 @@ module Searchkick
327 payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")] 327 payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")]
328 end 328 end
329 329
330 - if highlight_fields = options[:highlight][:fields] 330 + highlight_fields = options[:highlight][:fields]
  331 + if highlight_fields
331 payload[:highlight][:fields] = {} 332 payload[:highlight][:fields] = {}
332 333
333 highlight_fields.each do |name, opts| 334 highlight_fields.each do |name, opts|