1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using Bogus ;
3
5
using Nest ;
4
6
using Tests . Framework . Integration ;
5
7
using Tests . Framework . MockData ;
8
+ using FluentAssertions ;
9
+ using Newtonsoft . Json . Linq ;
10
+ using Xunit ;
6
11
7
12
namespace Tests . Search . Request
8
13
{
@@ -12,77 +17,207 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
12
17
13
18
protected override object ExpectJson => new
14
19
{
20
+ query = new
21
+ {
22
+ match = new JObject
23
+ {
24
+ { "name.standard" , new JObject
25
+ {
26
+ { "query" , "Upton Sons Shield Rice Rowe Roberts" }
27
+ }
28
+ }
29
+ }
30
+ } ,
15
31
highlight = new
16
32
{
17
33
pre_tags = new [ ] { "<tag1>" } ,
18
34
post_tags = new [ ] { "</tag1>" } ,
19
- fields = new
35
+ fields = new JObject
20
36
{
21
- name = new
22
- {
23
- type = "plain" ,
24
- force_source = true ,
25
- fragment_size = 150 ,
26
- number_of_fragments = 3 ,
27
- no_match_size = 150
37
+ { "name.standard" , new JObject
38
+ {
39
+ { "type" , "plain" } ,
40
+ { "force_source" , true } ,
41
+ { "fragment_size" , 150 } ,
42
+ { "number_of_fragments" , 3 } ,
43
+ { "no_match_size" , 150 }
44
+ }
45
+ } ,
46
+ { "leadDeveloper.firstName" , new JObject
47
+ {
48
+ { "type" , "fvh" } ,
49
+ { "pre_tags" , new JArray { "<name>" } } ,
50
+ { "post_tags" , new JArray { "</name>" } } ,
51
+ { "highlight_query" , new JObject
52
+ {
53
+ { "match" , new JObject
54
+ {
55
+ { "leadDeveloper.firstName" , new JObject
56
+ {
57
+ { "query" , "Kurt Edgardo Naomi Dariana Justice Felton" }
58
+ }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
28
65
} ,
29
- leadDeveloper = new { type = "fvh" } ,
30
- tags = new { type = "postings" }
66
+ { "state.offsets" , new JObject
67
+ {
68
+ { "type" , "postings" } ,
69
+ { "pre_tags" , new JArray { "<state>" } } ,
70
+ { "post_tags" , new JArray { "</state>" } } ,
71
+ { "highlight_query" , new JObject
72
+ {
73
+ { "terms" , new JObject
74
+ {
75
+ { "state.offsets" , new JArray { "stable" , "bellyup" } }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
31
82
}
32
83
}
33
84
} ;
34
85
35
86
protected override Func < SearchDescriptor < Project > , ISearchRequest > Fluent => s => s
87
+ . Query ( q => q
88
+ . Match ( m => m
89
+ . Field ( f => f . Name . Suffix ( "standard" ) )
90
+ . Query ( "Upton Sons Shield Rice Rowe Roberts" )
91
+ )
92
+ )
36
93
. Highlight ( h => h
37
94
. PreTags ( "<tag1>" )
38
95
. PostTags ( "</tag1>" )
39
96
. Fields (
40
97
fs => fs
41
- . Field ( p => p . Name )
98
+ . Field ( p => p . Name . Suffix ( "standard" ) )
42
99
. Type ( HighlighterType . Plain )
43
100
. ForceSource ( )
44
101
. FragmentSize ( 150 )
45
102
. NumberOfFragments ( 3 )
46
103
. NoMatchSize ( 150 ) ,
47
104
fs => fs
48
- . Field ( p => p . LeadDeveloper )
49
- . Type ( HighlighterType . Fvh ) ,
105
+ . Field ( p => p . LeadDeveloper . FirstName )
106
+ . Type ( HighlighterType . Fvh )
107
+ . PreTags ( "<name>" )
108
+ . PostTags ( "</name>" )
109
+ . HighlightQuery ( q => q
110
+ . Match ( m => m
111
+ . Field ( p => p . LeadDeveloper . FirstName )
112
+ . Query ( "Kurt Edgardo Naomi Dariana Justice Felton" )
113
+ )
114
+ ) ,
50
115
fs => fs
51
- . Field ( p => p . Tags )
116
+ . Field ( p => p . State . Suffix ( "offsets" ) )
52
117
. Type ( HighlighterType . Postings )
118
+ . PreTags ( "<state>" )
119
+ . PostTags ( "</state>" )
120
+ . HighlightQuery ( q => q
121
+ . Terms ( t => t
122
+ . Field ( f => f . State . Suffix ( "offsets" ) )
123
+ . Terms (
124
+ StateOfBeing . Stable . ToString ( ) . ToLowerInvariant ( ) ,
125
+ StateOfBeing . BellyUp . ToString ( ) . ToLowerInvariant ( )
126
+ )
127
+ )
128
+ )
53
129
)
54
130
) ;
55
131
56
132
protected override SearchRequest < Project > Initializer =>
57
133
new SearchRequest < Project >
58
134
{
135
+ Query = new MatchQuery
136
+ {
137
+ Query = "Upton Sons Shield Rice Rowe Roberts" ,
138
+ Field = "name.standard"
139
+ } ,
59
140
Highlight = new Highlight
60
141
{
61
142
PreTags = new [ ] { "<tag1>" } ,
62
143
PostTags = new [ ] { "</tag1>" } ,
63
144
Fields = new Dictionary < Field , IHighlightField >
64
145
{
65
- { "name" , new HighlightField
66
- {
67
- Type = HighlighterType . Plain ,
68
- ForceSource = true ,
69
- FragmentSize = 150 ,
70
- NumberOfFragments = 3 ,
71
- NoMatchSize = 150
72
- }
73
- } ,
74
- { "leadDeveloper" , new HighlightField
146
+ { "name.standard" , new HighlightField
147
+ {
148
+ Type = HighlighterType . Plain ,
149
+ ForceSource = true ,
150
+ FragmentSize = 150 ,
151
+ NumberOfFragments = 3 ,
152
+ NoMatchSize = 150
153
+ }
154
+ } ,
155
+ { "leadDeveloper.firstName" , new HighlightField
156
+ {
157
+ Type = HighlighterType . Fvh ,
158
+ PreTags = new [ ] { "<name>" } ,
159
+ PostTags = new [ ] { "</name>" } ,
160
+ HighlightQuery = new MatchQuery
75
161
{
76
- Type = HighlighterType . Fvh ,
162
+ Field = "leadDeveloper.firstName" ,
163
+ Query = "Kurt Edgardo Naomi Dariana Justice Felton"
77
164
}
78
- } ,
79
- { "tags" , new HighlightField
165
+ }
166
+ } ,
167
+ { "state.offsets" , new HighlightField
168
+ {
169
+ Type = HighlighterType . Postings ,
170
+ PreTags = new [ ] { "<state>" } ,
171
+ PostTags = new [ ] { "</state>" } ,
172
+ HighlightQuery = new TermsQuery
80
173
{
81
- Type = HighlighterType . Postings
174
+ Field = "state.offsets" ,
175
+ Terms = new [ ] { "stable" , "bellyup" }
82
176
}
83
177
}
178
+ }
84
179
}
85
180
}
86
181
} ;
182
+
183
+ protected override void ExpectResponse ( ISearchResponse < Project > response )
184
+ {
185
+ response . IsValid . Should ( ) . BeTrue ( ) ;
186
+
187
+ foreach ( var highlightsByDocumentId in response . Highlights )
188
+ {
189
+ foreach ( var highlightHit in highlightsByDocumentId . Value )
190
+ {
191
+ if ( highlightHit . Key == "name.standard" )
192
+ {
193
+ foreach ( var highlight in highlightHit . Value . Highlights )
194
+ {
195
+ highlight . Should ( ) . Contain ( "<tag1>" ) ;
196
+ highlight . Should ( ) . Contain ( "</tag1>" ) ;
197
+ }
198
+ }
199
+ else if ( highlightHit . Key == "leadDeveloper.firstName" )
200
+ {
201
+ foreach ( var highlight in highlightHit . Value . Highlights )
202
+ {
203
+ highlight . Should ( ) . Contain ( "<name>" ) ;
204
+ highlight . Should ( ) . Contain ( "</name>" ) ;
205
+ }
206
+ }
207
+ else if ( highlightHit . Key == "state.offsets" )
208
+ {
209
+ foreach ( var highlight in highlightHit . Value . Highlights )
210
+ {
211
+ highlight . Should ( ) . Contain ( "<state>" ) ;
212
+ highlight . Should ( ) . Contain ( "</state>" ) ;
213
+ }
214
+ }
215
+ else
216
+ {
217
+ Assert . True ( false , $ "highlights contains unexpected key { highlightHit . Key } ") ;
218
+ }
219
+ }
220
+ }
221
+ }
87
222
}
88
223
}
0 commit comments