Skip to content

Commit fa64ba3

Browse files
committed
Wrapper for MatchAllDocsQuery
1 parent de50079 commit fa64ba3

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,7 @@ public void testConstantKeywordAndOtherFieldsMatchMultipleHitsHighlighted() thro
36403640
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
36413641
assertAcked(prepareCreate(index).setMapping(mappings));
36423642

3643-
XContentBuilder firstDocument = jsonBuilder().startObject().field("foo", "bar").endObject();
3643+
XContentBuilder firstDocument = jsonBuilder().startObject().field("foo", constantValue).endObject();
36443644
XContentBuilder secondDocument = jsonBuilder().startObject().field("foo", constantValue).endObject();
36453645
saveDocumentIntoIndex(index, "1", firstDocument);
36463646
saveDocumentIntoIndex(index, "2", secondDocument);
@@ -3649,8 +3649,9 @@ public void testConstantKeywordAndOtherFieldsMatchMultipleHitsHighlighted() thro
36493649

36503650
assertNoFailures(search);
36513651
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
3652-
assertHighlight(search, 1, "foo", 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
3652+
assertHighlight(search, 0, "foo", 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
36533653
assertHighlight(search, 1, constantKeywordFieldName, 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
3654+
assertHighlight(search, 1, "foo", 0, 1, equalTo("<em>%s</em>".formatted(constantValue)));
36543655
}
36553656

36563657
public void testDoubleConstantKeywordJustOneHighlighted() throws IOException {

server/src/main/java/org/elasticsearch/common/lucene/search/Queries.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.index.IndexVersion;
2424
import org.elasticsearch.index.mapper.NestedPathFieldMapper;
2525
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
26+
import org.elasticsearch.index.query.MatchAllDocumentQueryWrapper;
2627

2728
import java.util.Collection;
2829
import java.util.Collections;
@@ -35,6 +36,10 @@ public static Query newMatchAllQuery() {
3536
return new MatchAllDocsQuery();
3637
}
3738

39+
public static Query newMatchAllQueryWrapper(String pattern) {
40+
return new MatchAllDocumentQueryWrapper(pattern);
41+
}
42+
3843
/** Return a query that matches no document. */
3944
public static Query newMatchNoDocsQuery(String reason) {
4045
return new MatchNoDocsQuery(reason);

server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static String valueToString(Object value) {
5454
public final Query termQuery(Object value, SearchExecutionContext context) {
5555
String pattern = valueToString(value);
5656
if (matches(pattern, false, context)) {
57-
return Queries.newMatchAllQuery();
57+
return Queries.newMatchAllQueryWrapper(pattern);
5858
} else {
5959
return new MatchNoDocsQuery();
6060
}
@@ -64,7 +64,7 @@ public final Query termQuery(Object value, SearchExecutionContext context) {
6464
public final Query termQueryCaseInsensitive(Object value, SearchExecutionContext context) {
6565
String pattern = valueToString(value);
6666
if (matches(pattern, true, context)) {
67-
return Queries.newMatchAllQuery();
67+
return Queries.newMatchAllQueryWrapper(pattern);
6868
} else {
6969
return new MatchNoDocsQuery();
7070
}
@@ -76,7 +76,7 @@ public final Query termsQuery(Collection<?> values, SearchExecutionContext conte
7676
String pattern = valueToString(value);
7777
if (matches(pattern, false, context)) {
7878
// `terms` queries are a disjunction, so one matching term is enough
79-
return Queries.newMatchAllQuery();
79+
return Queries.newMatchAllQueryWrapper(pattern);
8080
}
8181
}
8282
return new MatchNoDocsQuery();
@@ -91,7 +91,7 @@ public final Query prefixQuery(
9191
) {
9292
String pattern = prefix + "*";
9393
if (matches(pattern, caseInsensitive, context)) {
94-
return Queries.newMatchAllQuery();
94+
return Queries.newMatchAllQueryWrapper(pattern);
9595
} else {
9696
return new MatchNoDocsQuery();
9797
}
@@ -105,7 +105,7 @@ public final Query wildcardQuery(
105105
SearchExecutionContext context
106106
) {
107107
if (matches(value, caseInsensitive, context)) {
108-
return Queries.newMatchAllQuery();
108+
return Queries.newMatchAllQueryWrapper(value);
109109
} else {
110110
return new MatchNoDocsQuery();
111111
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.index.query;
10+
11+
import org.apache.lucene.search.IndexSearcher;
12+
import org.apache.lucene.search.MatchAllDocsQuery;
13+
import org.apache.lucene.search.Query;
14+
import org.apache.lucene.search.QueryVisitor;
15+
import org.apache.lucene.search.ScoreMode;
16+
import org.apache.lucene.search.Weight;
17+
18+
public final class MatchAllDocumentQueryWrapper extends Query {
19+
private final String pattern;
20+
private final MatchAllDocsQuery matchAllDocsQuery;
21+
22+
public MatchAllDocumentQueryWrapper(String pattern) {
23+
this.matchAllDocsQuery = new MatchAllDocsQuery();
24+
this.pattern = pattern;
25+
}
26+
27+
public String getPattern() {
28+
return pattern;
29+
}
30+
31+
@Override
32+
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) {
33+
return matchAllDocsQuery.createWeight(searcher, scoreMode, boost);
34+
}
35+
36+
@Override
37+
public String toString(String field) {
38+
return matchAllDocsQuery.toString();
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
return matchAllDocsQuery.equals(o);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return matchAllDocsQuery.hashCode();
49+
}
50+
51+
@Override
52+
public void visit(QueryVisitor visitor) {
53+
visitor.visitLeaf(matchAllDocsQuery);
54+
}
55+
}

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
import org.apache.lucene.index.LeafReaderContext;
1212
import org.apache.lucene.index.Term;
1313
import org.apache.lucene.search.DisjunctionMaxQuery;
14-
import org.apache.lucene.search.MatchAllDocsQuery;
1514
import org.apache.lucene.search.MatchNoDocsQuery;
1615
import org.apache.lucene.search.Query;
1716
import org.apache.lucene.search.TermQuery;
17+
import org.elasticsearch.common.regex.Regex;
1818
import org.elasticsearch.index.mapper.ConstantFieldType;
1919
import org.elasticsearch.index.mapper.KeywordFieldMapper;
2020
import org.elasticsearch.index.mapper.MappedFieldType;
2121
import org.elasticsearch.index.mapper.TextFieldMapper;
22+
import org.elasticsearch.index.query.MatchAllDocumentQueryWrapper;
2223
import org.elasticsearch.search.fetch.FetchContext;
2324
import org.elasticsearch.search.fetch.FetchSubPhase;
2425
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor;
@@ -174,12 +175,14 @@ private Query getHighlighterQuery(FetchContext context, SearchHighlightContext.F
174175
}
175176

176177
private Query getHighlighterQueryForConstantFieldType(FetchContext context, MappedFieldType fieldType) {
177-
if (context.query() instanceof MatchAllDocsQuery) {
178-
return new TermQuery(new Term(fieldType.name(), getValueForConstantFieldType(context, fieldType)));
179-
} else if (context.query() instanceof DisjunctionMaxQuery disjunctionMaxQuery) {
180-
for (Query anyDisjunctQuery : disjunctionMaxQuery.getDisjuncts()) {
181-
if (anyDisjunctQuery instanceof MatchAllDocsQuery) {
182-
return new TermQuery(new Term(fieldType.name(), getValueForConstantFieldType(context, fieldType)));
178+
if (context.query() instanceof MatchAllDocumentQueryWrapper persist) {
179+
return new TermQuery(new Term(fieldType.name(), persist.getPattern()));
180+
} else if (context.query() instanceof DisjunctionMaxQuery changeName) {
181+
for (Query query : changeName.getDisjuncts()) {
182+
if (query instanceof MatchAllDocumentQueryWrapper anyPersist) {
183+
if (Regex.simpleMatch(anyPersist.getPattern(), getValueForConstantFieldType(context, fieldType))) {
184+
return new TermQuery(new Term(fieldType.name(), getValueForConstantFieldType(context, fieldType)));
185+
}
183186
}
184187
}
185188
}

0 commit comments

Comments
 (0)