Commit eb89a25914a5731addaee902bf649c4151888f91
1 parent
e3967bee
Exists in
master
and in
21 other branches
Enable the use of highlighting options, including fragment length.
Showing
3 changed files
with
43 additions
and
4 deletions
Show diff stats
README.md
@@ -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. | 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,6 +569,22 @@ To change the tag, use: | @@ -569,6 +569,22 @@ 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: | ||
573 | + | ||
574 | +```ruby | ||
575 | +Band.search "cinema", fields: [:name], highlight: { fields: [:description] } | ||
576 | +``` | ||
577 | + | ||
578 | +Additional options, including fragment size, can be specified for each field: | ||
579 | + | ||
580 | +```ruby | ||
581 | +Band.search "cinema", fields: [:name], highlight: { fields: { name: { fragment_size: 200 } } } | ||
582 | +``` | ||
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). | ||
585 | + | ||
586 | + | ||
587 | + | ||
572 | ### Similar Items | 588 | ### Similar Items |
573 | 589 | ||
574 | Find similar items. | 590 | Find similar items. |
lib/searchkick/query.rb
@@ -320,9 +320,20 @@ module Searchkick | @@ -320,9 +320,20 @@ module Searchkick | ||
320 | payload[:highlight] = { | 320 | payload[:highlight] = { |
321 | fields: Hash[ fields.map{|f| [f, {}] } ] | 321 | fields: Hash[ fields.map{|f| [f, {}] } ] |
322 | } | 322 | } |
323 | - if options[:highlight].is_a?(Hash) and tag = options[:highlight][:tag] | ||
324 | - payload[:highlight][:pre_tags] = [tag] | ||
325 | - payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")] | 323 | + |
324 | + if options[:highlight].is_a?(Hash) | ||
325 | + if tag = options[:highlight][:tag] | ||
326 | + payload[:highlight][:pre_tags] = [tag] | ||
327 | + payload[:highlight][:post_tags] = [tag.to_s.gsub(/\A</, "</")] | ||
328 | + end | ||
329 | + | ||
330 | + if highlight_fields = options[:highlight][:fields] | ||
331 | + payload[:highlight][:fields] = {} | ||
332 | + | ||
333 | + highlight_fields.each do |name, opts| | ||
334 | + payload[:highlight][:fields]["#{name}.analyzed"] = opts || {} | ||
335 | + end | ||
336 | + end | ||
326 | end | 337 | end |
327 | end | 338 | end |
328 | 339 |
test/highlight_test.rb
@@ -19,6 +19,18 @@ class TestHighlight < Minitest::Test | @@ -19,6 +19,18 @@ class TestHighlight < Minitest::Test | ||
19 | assert_equal "<em>Cinema</em> Orange", highlight[:color] | 19 | assert_equal "<em>Cinema</em> Orange", highlight[:color] |
20 | end | 20 | end |
21 | 21 | ||
22 | + def test_fields | ||
23 | + store [{name: "Two Door Cinema Club", color: "Cinema Orange"}] | ||
24 | + highlight = Product.search("cinema", fields: [:name, :color], highlight: {fields: [:name]}).with_details.first[1][:highlight] | ||
25 | + assert_equal "Two Door <em>Cinema</em> Club", highlight[:name] | ||
26 | + assert_equal nil, highlight[:color] | ||
27 | + end | ||
28 | + | ||
29 | + def test_field_options | ||
30 | + store_names ["Two Door Cinema Club are a Northern Irish indie rock band"] | ||
31 | + assert_equal "Two Door <em>Cinema</em> Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 20}}}).with_details.first[1][:highlight][:name] | ||
32 | + end | ||
33 | + | ||
22 | def test_multiple_words | 34 | def test_multiple_words |
23 | store_names ["Hello World Hello"] | 35 | store_names ["Hello World Hello"] |
24 | assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] | 36 | assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] |