diff --git a/server/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java index b7470b287fd5d..e5e6dd0ff3616 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java @@ -25,6 +25,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. @@ -67,8 +69,7 @@ public String fieldName() { protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { SearchExecutionContext context = queryRewriteContext.convertToSearchExecutionContext(); if (context != null) { - Collection fields = getMappedFields(context, fieldName); - if (fields.isEmpty()) { + if (getMappedFields(context, fieldName).isEmpty()) { return new MatchNoneQueryBuilder(); } } @@ -126,8 +127,8 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { } public static Query newFilter(SearchExecutionContext context, String fieldPattern, boolean checkRewrite) { - - Collection fields = getMappedFields(context, fieldPattern); + Collection fields = getMappedFields(context, fieldPattern) + .stream().map(context::getFieldType).collect(Collectors.toList()); if (fields.isEmpty()) { if (checkRewrite) { @@ -149,14 +150,13 @@ public static Query newFilter(SearchExecutionContext context, String fieldPatter return new ConstantScoreQuery(boolFilterBuilder.build()); } - private static Collection getMappedFields(SearchExecutionContext context, String fieldPattern) { - Collection fields = context.getMatchingFieldTypes(fieldPattern); - if (fields.isEmpty()) { + private static Collection getMappedFields(SearchExecutionContext context, String fieldPattern) { + Set 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 diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 04f664c0af11d..b0e16ea83da24 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -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);