Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExistsQueryBuilder to no longer rely on getMatchingFieldTypes #73617

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -67,8 +69,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 @@ -126,8 +127,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 @@ -149,14 +150,13 @@ public static Query newFilter(SearchExecutionContext context, String fieldPatter
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