-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueryParserTest.java
executable file
·284 lines (240 loc) · 10.4 KB
/
QueryParserTest.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package nl.luminis.blog.gsa.dsl.query;
import static nl.luminis.blog.gsa.dto.MusicAlbum.SEARCHABLE_FIELDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.elasticsearch.index.query.Operator.AND;
import static org.elasticsearch.index.query.Operator.OR;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import nl.luminis.blog.gsa.dsl.BooleanConverter;
@RunWith(MockitoJUnitRunner.class)
public class QueryParserTest {
private static final String ID = "351046";
private static final String ALBUM = "Pet Sounds";
private static final String INFORMATION = "Additional album information";
private QueryParser parser;
@Mock
private BooleanConverter booleanConverter;
@Before
public void setUp() {
QueryVisitor visitor = new QueryVisitor(booleanConverter);
parser = new QueryParser(visitor);
// The default behaviour of the converter should be to return the argument untouched.
when(booleanConverter.convert(anyString())).thenAnswer(i -> i.getArgument(0));
}
@Test
public void testSimpleValueQuery() {
QueryBuilder builder = parser.visit("value");
assertThat(builder).isEqualTo(freeTextQuery("value"));
}
@Test
public void testSimpleNumericQuery() {
QueryBuilder builder = parser.visit("69346054");
assertThat(builder).isEqualTo(freeTextQuery("69346054"));
}
@Test
public void testSimpleValueQueryWithNonLetterCharacter() {
QueryBuilder builder = parser.visit("value.value");
assertThat(builder).isEqualTo(freeTextQuery("value.value"));
}
@Test
public void testTrailingWhitespaceQuery() {
QueryBuilder builder = parser.visit("value ");
assertThat(builder).isEqualTo(freeTextQuery("value"));
}
@Test
public void testDoubleValueQuery() {
QueryBuilder builder = parser.visit("value1 value2");
assertThat(builder).isEqualTo(freeTextQuery("value1 value2"));
}
@Test
public void testDoubleValueQueryWithNonLetterCharacter() {
QueryBuilder builder = parser.visit("value1 value2 .");
assertThat(builder).isEqualTo(freeTextQuery("value1 value2 ."));
}
@Test
public void testAllInTextSingleValueQuery() {
QueryBuilder builder = parser.visit("allintext:value");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value")));
}
@Test
public void testAllInTextDoubleValueQuery() {
QueryBuilder builder = parser.visit("allintext:value1 value2");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value1 value2")));
}
@Test
public void testAllInTextDoubleKeyQuery() {
QueryBuilder builder = parser.visit("allintext:value1 allintext:value2");
assertThat(builder).isEqualTo(
boolQuery()
.must(freeTextQuery("value1"))
.must(freeTextQuery("value2"))
);
}
@Test
public void testAllInTextTripleValueQuery() {
QueryBuilder builder = parser.visit("allintext:value1 value2 value3");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value1 value2 value3")));
}
@Test
public void testSingleSpacedAllInTextQuery() {
QueryBuilder builder = parser.visit("allintext: value");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value")));
}
@Test
public void testDoubleSpacedAllInTextQuery() {
QueryBuilder builder = parser.visit("allintext: value");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value")));
}
@Test
public void testTrailingSpaceAllInTextQuery() {
QueryBuilder builder = parser.visit("allintext: value ");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("value")));
}
@Test
public void testInMetaSingleValueQuery() {
QueryBuilder builder = parser.visit("inmeta:id=" + ID);
assertThat(builder).isEqualTo(
boolQuery()
.must(new MatchQueryBuilder("id", ID)));
}
@Test
public void testInMetaDoubleValueQuery() {
QueryBuilder builder = parser.visit("inmeta:id=" + ID + " inmeta:album=" + ALBUM);
assertThat(builder).isEqualTo(
boolQuery()
.must(new MatchQueryBuilder("id", ID))
.must(new MatchQueryBuilder("album", ALBUM)));
}
@Test
public void testInMetaTripleValueQuery() {
QueryBuilder builder = parser.visit("inmeta:id=" + ID + " inmeta:album=" + ALBUM + " inmeta:information=" + INFORMATION);
assertThat(builder).isEqualTo(
boolQuery()
.must(new MatchQueryBuilder("id", ID))
.must(new MatchQueryBuilder("album", ALBUM))
.must(new MatchQueryBuilder("information", INFORMATION)));
}
@Test
public void testInvalidInMetaValue() {
QueryBuilder builder = parser.visit("inmeta:invalid-key");
assertThat(builder).isEqualTo(
boolQuery()
.must(new MatchQueryBuilder("invalid-key", "")));
}
@Test
public void testAllInTextAndInMetaQuery() {
QueryBuilder builder = parser.visit("allintext:" + ALBUM + " inmeta:id=" + ID);
assertThat(builder).isEqualTo(
boolQuery()
.must(freeTextQuery(ALBUM))
.must(new MatchQueryBuilder("id", ID))
);
}
@Test
public void testExtendedUnicodeCharacters() {
String diacritic = "à";
String nonLetterTerm = "[value]";
String highUnicodeCharacter = "ـش\u200E";
for (String term : Arrays.asList(diacritic, nonLetterTerm, highUnicodeCharacter)) {
QueryBuilder builder = parser.visit(term);
assertThat(builder).isEqualTo(freeTextQuery(term));
}
}
@Test
public void testValueWithEquals() {
QueryBuilder builder = parser.visit("notAKeyWord=someOtherValue");
assertThat(builder).isEqualTo(freeTextQuery("notAKeyWord someOtherValue"));
}
@Test
public void testValueWithColon() {
QueryBuilder builder = parser.visit("notAKeyWord:someOtherValue");
assertThat(builder).isEqualTo(freeTextQuery("notAKeyWord someOtherValue"));
}
@Test
public void testQueryValueWithColon() {
QueryBuilder builder = parser.visit("allintext:text:more text");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("text more text")));
}
@Test
public void testQueryValueWithParentheses() {
QueryBuilder builder = parser.visit("allintext:title of album (subtitle)");
assertThat(builder).isEqualTo(boolQuery().must(freeTextQuery("title of album subtitle")));
}
@Test
public void testOrAllInTextQuery() {
QueryBuilder builder = parser.visit("allintext:Elton John OR allintext:The Beach Boys");
assertThat(builder).isEqualTo(
boolQuery()
.should(freeTextQuery("Elton John"))
.should(freeTextQuery("The Beach Boys"))
.minimumShouldMatch(1)
);
}
@Test
public void testOrInMetaQuery() {
QueryBuilder builder = parser.visit("inmeta:id=" + ID + " OR inmeta:album=" + ALBUM);
assertThat(builder).isEqualTo(
boolQuery()
.should(new MatchQueryBuilder("id", ID))
.should(new MatchQueryBuilder("album", ALBUM))
.minimumShouldMatch(1));
}
@Test
public void testMultipleOrAllInTextQuery() {
QueryBuilder builder = parser.visit("allintext:Elton John OR The Beach Boys");
assertThat(builder).isEqualTo(
boolQuery().must(
new MultiMatchQueryBuilder("Elton John The Beach Boys", SEARCHABLE_FIELDS)
.operator(OR)
.minimumShouldMatch("1")
.lenient(true)));
}
@Test
public void testNestedOrInMetaQuery() {
QueryBuilder builder = parser.visit("(inmeta:id=" + ID + " OR inmeta:album=" + ALBUM + ")");
assertThat(builder).isEqualTo(
boolQuery()
.should(new MatchQueryBuilder("id", ID))
.should(new MatchQueryBuilder("album", ALBUM))
.minimumShouldMatch(1));
}
@Test
public void testNestedOrInMetaQueryWithExtraClause() {
BoolQueryBuilder expectedQuery = boolQuery()
.must(new MatchQueryBuilder("id", ID))
.should(new MatchQueryBuilder("information", INFORMATION))
.should(new MatchQueryBuilder("album", ALBUM))
.minimumShouldMatch(1);
QueryBuilder pairAndSubQueryBuilder = parser.visit("inmeta:id=" + ID + " (inmeta:information=" + INFORMATION + " OR inmeta:album=" + ALBUM + ")");
assertThat(pairAndSubQueryBuilder).isEqualTo(expectedQuery);
QueryBuilder subQueryAndPairBuilder = parser.visit("(inmeta:information=" + INFORMATION + " OR inmeta:album=" + ALBUM + ") inmeta:id=" + ID);
assertThat(subQueryAndPairBuilder).isEqualTo(expectedQuery);
}
@Test
public void testPipedQuery() {
QueryBuilder builder = parser.visit("Elton John|The Beach Boys");
assertThat(builder).isEqualTo(freeTextQuery("Elton John The Beach Boys"));
}
@Test
public void testRangeQuery() {
QueryBuilder builder = parser.visit("inmeta:year:1970..1975");
assertThat(builder).isEqualTo(
boolQuery()
.must(new RangeQueryBuilder("year").gte("1970").lte("1975")));
}
private MultiMatchQueryBuilder freeTextQuery(String terms) {
return new MultiMatchQueryBuilder(terms, SEARCHABLE_FIELDS).operator(AND).lenient(true);
}
}