|
20 | 20 | package org.elasticsearch.index.query.functionscore;
|
21 | 21 |
|
22 | 22 | import com.fasterxml.jackson.core.JsonParseException;
|
23 |
| - |
24 | 23 | import org.apache.lucene.index.Term;
|
25 | 24 | import org.apache.lucene.search.MatchAllDocsQuery;
|
| 25 | +import org.apache.lucene.search.MatchNoDocsQuery; |
26 | 26 | import org.apache.lucene.search.Query;
|
27 | 27 | import org.apache.lucene.search.TermQuery;
|
28 | 28 | import org.elasticsearch.common.ParsingException;
|
|
40 | 40 | import org.elasticsearch.common.xcontent.XContentType;
|
41 | 41 | import org.elasticsearch.index.mapper.SeqNoFieldMapper;
|
42 | 42 | import org.elasticsearch.index.query.MatchAllQueryBuilder;
|
| 43 | +import org.elasticsearch.index.query.MatchNoneQueryBuilder; |
43 | 44 | import org.elasticsearch.index.query.QueryBuilder;
|
44 | 45 | import org.elasticsearch.index.query.QueryShardContext;
|
45 | 46 | import org.elasticsearch.index.query.RandomQueryBuilder;
|
|
54 | 55 | import org.elasticsearch.search.MultiValueMode;
|
55 | 56 | import org.elasticsearch.test.AbstractQueryTestCase;
|
56 | 57 | import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin;
|
| 58 | +import org.hamcrest.CoreMatchers; |
57 | 59 | import org.hamcrest.Matcher;
|
58 | 60 | import org.joda.time.DateTime;
|
59 | 61 | import org.joda.time.DateTimeZone;
|
|
77 | 79 | import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.weightFactorFunction;
|
78 | 80 | import static org.hamcrest.Matchers.closeTo;
|
79 | 81 | import static org.hamcrest.Matchers.containsString;
|
80 |
| -import static org.hamcrest.Matchers.either; |
81 | 82 | import static org.hamcrest.Matchers.equalTo;
|
82 | 83 | import static org.hamcrest.Matchers.instanceOf;
|
83 | 84 | import static org.hamcrest.Matchers.nullValue;
|
@@ -254,7 +255,12 @@ private static DecayFunctionBuilder<?> createRandomDecayFunction() {
|
254 | 255 |
|
255 | 256 | @Override
|
256 | 257 | protected void doAssertLuceneQuery(FunctionScoreQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
|
257 |
| - assertThat(query, either(instanceOf(FunctionScoreQuery.class)).or(instanceOf(FunctionScoreQuery.class))); |
| 258 | + Query wrappedQuery = queryBuilder.query().rewrite(context).toQuery(context); |
| 259 | + if (wrappedQuery instanceof MatchNoDocsQuery) { |
| 260 | + assertThat(query, CoreMatchers.instanceOf(MatchNoDocsQuery.class)); |
| 261 | + } else { |
| 262 | + assertThat(query, CoreMatchers.instanceOf(FunctionScoreQuery.class)); |
| 263 | + } |
258 | 264 | }
|
259 | 265 |
|
260 | 266 | public void testIllegalArguments() {
|
@@ -674,11 +680,23 @@ public void testRewriteWithFunction() throws IOException {
|
674 | 680 | assertSame(rewrite.filterFunctionBuilders()[1].getFilter(), secondFunction);
|
675 | 681 | }
|
676 | 682 |
|
| 683 | + public void testRewriteToMatchNone() throws IOException { |
| 684 | + FunctionScoreQueryBuilder functionScoreQueryBuilder = |
| 685 | + new FunctionScoreQueryBuilder(new TermQueryBuilder("unmapped_field", "value")) |
| 686 | + .boostMode(CombineFunction.REPLACE) |
| 687 | + .scoreMode(FunctionScoreQuery.ScoreMode.SUM) |
| 688 | + .setMinScore(1) |
| 689 | + .maxBoost(100); |
| 690 | + |
| 691 | + QueryBuilder rewrite = functionScoreQueryBuilder.rewrite(createShardContext()); |
| 692 | + assertThat(rewrite, instanceOf(MatchNoneQueryBuilder.class)); |
| 693 | + } |
| 694 | + |
677 | 695 | /**
|
678 | 696 | * Please see https://github.com/elastic/elasticsearch/issues/35123 for context.
|
679 | 697 | */
|
680 | 698 | public void testSingleScriptFunction() throws IOException {
|
681 |
| - QueryBuilder queryBuilder = RandomQueryBuilder.createQuery(random()); |
| 699 | + QueryBuilder queryBuilder = termQuery(KEYWORD_FIELD_NAME, "value"); |
682 | 700 | ScoreFunctionBuilder<ScriptScoreFunctionBuilder> functionBuilder = new ScriptScoreFunctionBuilder(
|
683 | 701 | new Script(ScriptType.INLINE, MockScriptEngine.NAME, "1", Collections.emptyMap()));
|
684 | 702 |
|
|
0 commit comments