require_relative "test_helper" class TestHighlight < Minitest::Test def test_basic store_names ["Two Door Cinema Club"] assert_equal "Two Door Cinema Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name] end def test_tag store_names ["Two Door Cinema Club"] assert_equal "Two Door Cinema Club", Product.search("cinema", fields: [:name], highlight: {tag: ""}).with_details.first[1][:highlight][:name] end def test_multiple_fields store [{name: "Two Door Cinema Club", color: "Cinema Orange"}] highlight = Product.search("cinema", fields: [:name, :color], highlight: true).with_details.first[1][:highlight] assert_equal "Two Door Cinema Club", highlight[:name] assert_equal "Cinema 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] end def test_json store_names ["Two Door Cinema Club"] json = { query: { match: { _all: "cinema" } }, highlight: { pre_tags: [""], post_tags: [""], fields: { "name.analyzed" => {} } } } assert_equal "Two Door Cinema Club", Product.search(json: json).with_details.first[1][:highlight][:"name.analyzed"] end end