Skip to content

Commit

Permalink
ExistsQueryBuilder to no longer rely on getMatchingFieldTypes (#73617)
Browse files Browse the repository at this point in the history
We've been discussing possibly removing `FieldTypeLookup#getMatchingFieldTypes`, or at least its `SearchExecutionContext` variant that applies runtime mappings.

This is another step in that direction: the exists query can rely on getMatchingFieldNames instead, and look up field types by name.
  • Loading branch information
javanna authored Jun 2, 2021
1 parent 273b3c5 commit 9e63ea0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Constructs a query that only match on documents that the field has a value in them.
Expand Down Expand Up @@ -71,8 +73,7 @@ public String fieldName() {
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
SearchExecutionContext context = queryRewriteContext.convertToSearchExecutionContext();
if (context != null) {
Collection<MappedFieldType> fields = getMappedFields(context, fieldName);
if (fields.isEmpty()) {
if (getMappedFields(context, fieldName).isEmpty()) {
return new MatchNoneQueryBuilder();
}
}
Expand Down Expand Up @@ -130,8 +131,8 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
}

public static Query newFilter(SearchExecutionContext context, String fieldPattern, boolean checkRewrite) {

Collection<MappedFieldType> fields = getMappedFields(context, fieldPattern);
Collection<MappedFieldType> fields = getMappedFields(context, fieldPattern)
.stream().map(context::getFieldType).collect(Collectors.toList());

if (fields.isEmpty()) {
if (checkRewrite) {
Expand All @@ -142,7 +143,7 @@ public static Query newFilter(SearchExecutionContext context, String fieldPatter
}

if (context.indexVersionCreated().before(Version.V_6_1_0)) {
return newLegacyExistsQuery(context, fields);
return newLegacyExistsQuery(fields);
}

if (fields.size() == 1) {
Expand All @@ -157,7 +158,7 @@ public static Query newFilter(SearchExecutionContext context, String fieldPatter
return new ConstantScoreQuery(boolFilterBuilder.build());
}

private static Query newLegacyExistsQuery(SearchExecutionContext context, Collection<MappedFieldType> fields) {
private static Query newLegacyExistsQuery(Collection<MappedFieldType> fields) {
// We create TermsQuery directly here rather than using FieldNamesFieldType.termsQuery()
// so we don't end up with deprecation warnings
if (fields.size() == 1) {
Expand All @@ -172,14 +173,13 @@ private static Query newLegacyExistsQuery(SearchExecutionContext context, Collec
return new ConstantScoreQuery(boolFilterBuilder.build());
}

private static Collection<MappedFieldType> getMappedFields(SearchExecutionContext context, String fieldPattern) {
Collection<MappedFieldType> fields = context.getMatchingFieldTypes(fieldPattern);
if (fields.isEmpty()) {
private static Collection<String> getMappedFields(SearchExecutionContext context, String fieldPattern) {
Set<String> matchingFieldNames = context.getMatchingFieldNames(fieldPattern);
if (matchingFieldNames.isEmpty()) {
// might be an object field, so try matching it as an object prefix pattern
fields = context.getMatchingFieldTypes(fieldPattern + ".*");
matchingFieldNames = context.getMatchingFieldNames(fieldPattern + ".*");
}

return fields;
return matchingFieldNames;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Query geoShapeQuery(Geometry shape, String fieldName, SpatialStrategy str
// before, including creating lucene fieldcache (!)
// in this case, execute disjoint as exists && !intersects
BooleanQuery.Builder bool = new BooleanQuery.Builder();
Query exists = ExistsQueryBuilder.newFilter(context, fieldName,false);
Query exists = ExistsQueryBuilder.newFilter(context, fieldName, false);
Query intersects = prefixTreeStrategy.makeQuery(getArgs(shape, ShapeRelation.INTERSECTS));
bool.add(exists, BooleanClause.Occur.MUST);
bool.add(intersects, BooleanClause.Occur.MUST_NOT);
Expand Down

0 comments on commit 9e63ea0

Please sign in to comment.