diff --git a/README.md b/README.md index d535b6d..027c7df 100644 --- a/README.md +++ b/README.md @@ -553,7 +553,7 @@ Highlight the search query in the results. bands = Band.search "cinema", fields: [:name], highlight: true ``` -**Note:** The `fields` option is required. +**Note:** The `fields` option is required, unless highlight options are given (see below). View the highlighted fields with: @@ -569,6 +569,22 @@ To change the tag, use: Band.search "cinema", fields: [:name], highlight: {tag: ""} ``` +To highlight and search different fields, use: + +```ruby +Band.search "cinema", fields: [:name], highlight: { fields: [:description] } +``` + +Additional options, including fragment size, can be specified for each field: + +```ruby +Band.search "cinema", fields: [:name], highlight: { fields: { name: { fragment_size: 200 } } } +``` + +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). + + + ### Similar Items Find similar items. diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index a791028..8121141 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -320,9 +320,20 @@ module Searchkick payload[:highlight] = { fields: Hash[ fields.map{|f| [f, {}] } ] } - if options[:highlight].is_a?(Hash) and tag = options[:highlight][:tag] - payload[:highlight][:pre_tags] = [tag] - payload[:highlight][:post_tags] = [tag.to_s.gsub(/\ACinema Orange", highlight[:color] end + def test_fields + store [{name: "Two Door Cinema Club", color: "Cinema Orange"}] + highlight = Product.search("cinema", fields: [:name, :color], highlight: {fields: [:name]}).with_details.first[1][:highlight] + assert_equal "Two Door Cinema Club", highlight[:name] + assert_equal nil, highlight[:color] + end + + def test_field_options + store_names ["Two Door Cinema Club are a Northern Irish indie rock band"] + assert_equal "Two Door Cinema Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 20}}}).with_details.first[1][:highlight][:name] + end + def test_multiple_words store_names ["Hello World Hello"] assert_equal "Hello World Hello", Product.search("hello", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] -- libgit2 0.21.0