Commit 9eeb97f2ea8eb8f1d28a185e1f82f5fb8e0e4cd0
1 parent
82e5db4a
Exists in
master
and in
21 other branches
Better highlight options
Showing
3 changed files
with
11 additions
and
8 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -553,7 +553,7 @@ Highlight the search query in the results. |
553 | 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 | 558 | View the highlighted fields with: |
559 | 559 | |
... | ... | @@ -569,22 +569,20 @@ To change the tag, use: |
569 | 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 | 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 | 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 | 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 | 586 | ### Similar Items |
589 | 587 | |
590 | 588 | Find similar items. | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -327,7 +327,8 @@ module Searchkick |
327 | 327 | payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")] |
328 | 328 | end |
329 | 329 | |
330 | - if highlight_fields = options[:highlight][:fields] | |
330 | + highlight_fields = options[:highlight][:fields] | |
331 | + if highlight_fields | |
331 | 332 | payload[:highlight][:fields] = {} |
332 | 333 | |
333 | 334 | highlight_fields.each do |name, opts| | ... | ... |