highlight_test.rb
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require_relative "test_helper"
class HighlightTest < Minitest::Test
def test_basic
store_names ["Two Door Cinema Club"]
assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).first.search_highlights[:name]
end
def test_tag
store_names ["Two Door Cinema Club"]
assert_equal "Two Door <strong>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong>"}).first.search_highlights[:name]
end
def test_tag_class
store_names ["Two Door Cinema Club"]
assert_equal "Two Door <strong class='classy'>Cinema</strong> Club", Product.search("cinema", fields: [:name], highlight: {tag: "<strong class='classy'>"}).first.search_highlights[:name]
end
def test_multiple_fields
store [{name: "Two Door Cinema Club", color: "Cinema Orange"}]
highlights = Product.search("cinema", fields: [:name, :color], highlight: true).first.search_highlights
assert_equal "Two Door <em>Cinema</em> Club", highlights[:name]
assert_equal "<em>Cinema</em> Orange", highlights[:color]
end
def test_fields
store [{name: "Two Door Cinema Club", color: "Cinema Orange"}]
highlights = Product.search("cinema", fields: [:name, :color], highlight: {fields: [:name]}).first.search_highlights
assert_equal "Two Door <em>Cinema</em> Club", highlights[:name]
assert_nil highlights[:color]
end
def test_field_options
store_names ["Two Door Cinema Club are a Northern Irish indie rock band"]
fragment_size = ENV["MATCH"] == "word_start" ? 26 : 20
assert_equal "Two Door <em>Cinema</em> Club are", Product.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: fragment_size}}}).first.search_highlights[:name]
end
def test_multiple_words
store_names ["Hello World Hello"]
assert_equal "<em>Hello</em> World <em>Hello</em>", Product.search("hello", fields: [:name], highlight: true).first.search_highlights[:name]
end
def test_encoder
store_names ["<b>Hello</b>"]
assert_equal "<b><em>Hello</em></b>", Product.search("hello", fields: [:name], highlight: {encoder: "html"}, misspellings: false).first.search_highlights[:name]
end
def test_word_middle
store_names ["Two Door Cinema Club"]
assert_equal "Two Door <em>Cinema</em> Club", Product.search("ine", fields: [:name], match: :word_middle, highlight: true).first.search_highlights[:name]
end
def test_body
skip if ENV["MATCH"] == "word_start"
store_names ["Two Door Cinema Club"]
body = {
query: {
match: {
"name.analyzed" => "cinema"
}
},
highlight: {
pre_tags: ["<strong>"],
post_tags: ["</strong>"],
fields: {
"name.analyzed" => {}
}
}
}
assert_equal "Two Door <strong>Cinema</strong> Club", Product.search(body: body).first.search_highlights[:"name.analyzed"]
end
def test_legacy
store_names ["Two Door Cinema Club"]
assert_equal "Two Door <em>Cinema</em> Club", Product.search("cinema", fields: [:name], highlight: true).with_details.first[1][:highlight][:name]
end
end