Commit 499402534258f120cc4e864d07ad33fdf4f7a9c8

Authored by Andrew Kane
2 parents ab0e37bd 9eeb97f2

Merge branch 'theotherfirm-master'

  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. 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,20 @@ To change the tag, use: @@ -569,6 +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: [master]
  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: [master]
  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 +
572 ### Similar Items 586 ### Similar Items
573 587
574 Find similar items. 588 Find similar items.
lib/searchkick/query.rb
@@ -320,9 +320,21 @@ module Searchkick @@ -320,9 +320,21 @@ 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 + highlight_fields = options[:highlight][:fields]
  331 + if highlight_fields
  332 + payload[:highlight][:fields] = {}
  333 +
  334 + highlight_fields.each do |name, opts|
  335 + payload[:highlight][:fields]["#{name}.analyzed"] = opts || {}
  336 + end
  337 + end
326 end 338 end
327 end 339 end
328 340
test/highlight_test.rb
@@ -19,6 +19,18 @@ class TestHighlight &lt; Minitest::Test @@ -19,6 +19,18 @@ class TestHighlight &lt; 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]