|
16 | 16 | import org.apache.lucene.search.LeafCollector; |
17 | 17 | import org.apache.lucene.search.MatchAllDocsQuery; |
18 | 18 | import org.apache.lucene.search.MatchNoDocsQuery; |
| 19 | +import org.apache.lucene.search.Query; |
19 | 20 | import org.apache.lucene.search.Scorable; |
20 | 21 | import org.apache.lucene.search.ScoreMode; |
21 | 22 | import org.apache.lucene.search.Sort; |
|
25 | 26 | import org.apache.lucene.util.BytesRef; |
26 | 27 | import org.elasticsearch.ElasticsearchException; |
27 | 28 | import org.elasticsearch.Version; |
| 29 | +import org.elasticsearch.common.bytes.BytesArray; |
28 | 30 | import org.elasticsearch.common.lucene.search.function.ScriptScoreQuery; |
29 | 31 | import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.common.xcontent.XContentParser.Token; |
| 33 | +import org.elasticsearch.common.xcontent.XContentType; |
| 34 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
30 | 35 | import org.elasticsearch.index.fielddata.ScriptDocValues; |
| 36 | +import org.elasticsearch.index.mapper.BooleanFieldMapper; |
| 37 | +import org.elasticsearch.index.mapper.ContentPath; |
31 | 38 | import org.elasticsearch.index.mapper.MappedFieldType; |
| 39 | +import org.elasticsearch.index.mapper.Mapper.BuilderContext; |
| 40 | +import org.elasticsearch.index.mapper.ParseContext; |
| 41 | +import org.elasticsearch.index.mapper.SourceToParse; |
32 | 42 | import org.elasticsearch.index.query.QueryShardContext; |
33 | 43 | import org.elasticsearch.plugins.ScriptPlugin; |
34 | 44 | import org.elasticsearch.script.ScoreScript; |
|
39 | 49 | import org.elasticsearch.script.ScriptService; |
40 | 50 | import org.elasticsearch.script.ScriptType; |
41 | 51 | import org.elasticsearch.search.MultiValueMode; |
| 52 | +import org.elasticsearch.test.ESTestCase; |
42 | 53 | import org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript; |
43 | 54 | import org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript; |
44 | 55 | import org.elasticsearch.xpack.runtimefields.RuntimeFields; |
|
56 | 67 | import static java.util.Collections.singletonList; |
57 | 68 | import static org.hamcrest.Matchers.equalTo; |
58 | 69 | import static org.hamcrest.Matchers.instanceOf; |
| 70 | +import static org.mockito.Mockito.mock; |
| 71 | +import static org.mockito.Mockito.when; |
59 | 72 |
|
60 | 73 | public class ScriptBooleanMappedFieldTypeTests extends AbstractNonTextScriptMappedFieldTypeTestCase { |
61 | 74 | @Override |
@@ -276,7 +289,80 @@ public void testTermsQueryIsExpensive() throws IOException { |
276 | 289 | checkExpensiveQuery((ft, ctx) -> ft.termsQuery(List.of(false), ctx)); |
277 | 290 | checkExpensiveQuery((ft, ctx) -> ft.termsQuery(List.of(false, true), ctx)); |
278 | 291 | // This is not an expensive query |
279 | | - assertThat(simpleMappedFieldType().termsQuery(List.of(), mockContext()), instanceOf(MatchAllDocsQuery.class)); |
| 292 | + assertThat(simpleMappedFieldType().termsQuery(List.of(), mockContext()), instanceOf(MatchNoDocsQuery.class)); |
| 293 | + } |
| 294 | + |
| 295 | + public void testDualingQueries() throws IOException { |
| 296 | + BooleanFieldMapper ootb = new BooleanFieldMapper.Builder("foo").build(new BuilderContext(Settings.EMPTY, new ContentPath())); |
| 297 | + try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { |
| 298 | + List<Boolean> values = randomList(0, 2, ESTestCase::randomBoolean); |
| 299 | + String source = "{\"foo\": " + values + "}"; |
| 300 | + ParseContext ctx = mock(ParseContext.class); |
| 301 | + when(ctx.parser()).thenReturn(createParser(JsonXContent.jsonXContent, source)); |
| 302 | + ParseContext.Document doc = new ParseContext.Document(); |
| 303 | + when(ctx.doc()).thenReturn(doc); |
| 304 | + when(ctx.sourceToParse()).thenReturn(new SourceToParse("test", "test", new BytesArray(source), XContentType.JSON)); |
| 305 | + doc.add(new StoredField("_source", new BytesRef(source))); |
| 306 | + ctx.parser().nextToken(); |
| 307 | + ctx.parser().nextToken(); |
| 308 | + ctx.parser().nextToken(); |
| 309 | + while (ctx.parser().nextToken() != Token.END_ARRAY) { |
| 310 | + ootb.parse(ctx); |
| 311 | + } |
| 312 | + iw.addDocument(doc); |
| 313 | + try (DirectoryReader reader = iw.getReader()) { |
| 314 | + IndexSearcher searcher = newSearcher(reader); |
| 315 | + assertSameCount( |
| 316 | + searcher, |
| 317 | + source, |
| 318 | + "*", |
| 319 | + simpleMappedFieldType().existsQuery(mockContext()), |
| 320 | + ootb.fieldType().existsQuery(mockContext()) |
| 321 | + ); |
| 322 | + boolean term = randomBoolean(); |
| 323 | + assertSameCount( |
| 324 | + searcher, |
| 325 | + source, |
| 326 | + term, |
| 327 | + simpleMappedFieldType().termQuery(term, mockContext()), |
| 328 | + ootb.fieldType().termQuery(term, mockContext()) |
| 329 | + ); |
| 330 | + List<Boolean> terms = randomList(0, 3, ESTestCase::randomBoolean); |
| 331 | + assertSameCount( |
| 332 | + searcher, |
| 333 | + source, |
| 334 | + terms, |
| 335 | + simpleMappedFieldType().termsQuery(terms, mockContext()), |
| 336 | + ootb.fieldType().termsQuery(terms, mockContext()) |
| 337 | + ); |
| 338 | + boolean low; |
| 339 | + boolean high; |
| 340 | + if (randomBoolean()) { |
| 341 | + low = high = randomBoolean(); |
| 342 | + } else { |
| 343 | + low = false; |
| 344 | + high = true; |
| 345 | + } |
| 346 | + boolean includeLow = randomBoolean(); |
| 347 | + boolean includeHigh = randomBoolean(); |
| 348 | + assertSameCount( |
| 349 | + searcher, |
| 350 | + source, |
| 351 | + (includeLow ? "[" : "(") + low + "," + high + (includeHigh ? "]" : ")"), |
| 352 | + simpleMappedFieldType().rangeQuery(low, high, includeLow, includeHigh, null, null, null, mockContext()), |
| 353 | + ootb.fieldType().rangeQuery(low, high, includeLow, includeHigh, null, null, null, mockContext()) |
| 354 | + ); |
| 355 | + } |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + private void assertSameCount(IndexSearcher searcher, String source, Object queryDescription, Query scriptedQuery, Query ootbQuery) |
| 360 | + throws IOException { |
| 361 | + assertThat( |
| 362 | + "source=" + source + ",query=" + queryDescription + ",scripted=" + scriptedQuery + ",ootb=" + ootbQuery, |
| 363 | + searcher.count(scriptedQuery), |
| 364 | + equalTo(searcher.count(ootbQuery)) |
| 365 | + ); |
280 | 366 | } |
281 | 367 |
|
282 | 368 | @Override |
|
0 commit comments