Commit eaf977d1382d0502b067afbe4161c8db86bfa0bb

Authored by Andrew Kane
1 parent 342879c9

Added highlight [skip ci]

lib/searchkick/relation.rb
@@ -9,7 +9,7 @@ module Searchkick @@ -9,7 +9,7 @@ module Searchkick
9 :took, :error, :model_name, :entry_name, :total_count, :total_entries, 9 :took, :error, :model_name, :entry_name, :total_count, :total_entries,
10 :current_page, :per_page, :limit_value, :total_pages, :num_pages, 10 :current_page, :per_page, :limit_value, :total_pages, :num_pages,
11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, 11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
12 - :out_of_range?, :hits, :response, :to_a, :first, :scroll 12 + :out_of_range?, :hits, :response, :to_a, :first, :scroll, :highlights
13 13
14 def initialize(klass, term = "*", **options) 14 def initialize(klass, term = "*", **options)
15 unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost, 15 unknown_keywords = options.keys - [:aggs, :block, :body, :body_options, :boost,
@@ -245,6 +245,15 @@ module Searchkick @@ -245,6 +245,15 @@ module Searchkick
245 self 245 self
246 end 246 end
247 247
  248 + def highlight(value)
  249 + spawn.highlight!(value)
  250 + end
  251 +
  252 + def highlight!(value)
  253 + options[:highlight] = value
  254 + self
  255 + end
  256 +
248 # same as Active Record 257 # same as Active Record
249 def inspect 258 def inspect
250 entries = results.first(11).map!(&:inspect) 259 entries = results.first(11).map!(&:inspect)
test/highlight_test.rb
@@ -4,6 +4,7 @@ class HighlightTest < Minitest::Test @@ -4,6 +4,7 @@ class HighlightTest < Minitest::Test
4 def test_basic 4 def test_basic
5 store_names ["Two Door Cinema Club"] 5 store_names ["Two Door Cinema Club"]
6 assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", highlight: true).highlights.first[:name] 6 assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", highlight: true).highlights.first[:name]
  7 + assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", relation: true).highlight(true).highlights.first[:name]
7 end 8 end
8 9
9 def test_with_highlights 10 def test_with_highlights