Skip to content

Commit

Permalink
Use filter clause for Elasticsearch queries
Browse files Browse the repository at this point in the history
Filter clauses are more efficient to compute (they ignore scoring),
and can be cached by Elasticsearch.

Cherry-pick of trinodb/trino@56aa0d7

Co-authored-by: Martin Traverso mtraverso@gmail.com
  • Loading branch information
zhenxiao committed Jun 10, 2020
1 parent 566e79c commit e169f88
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static QueryBuilder buildSearchQuery(ConnectorSession session, TupleDomai

checkArgument(!domain.isNone(), "Unexpected NONE domain for %s", column.getName());
if (!domain.isAll()) {
queryBuilder.must(new BoolQueryBuilder().must(buildPredicate(session, column.getName(), domain, column.getType())));
queryBuilder.filter(new BoolQueryBuilder().must(buildPredicate(session, column.getName(), domain, column.getType())));
}
}
}
Expand Down Expand Up @@ -108,10 +108,10 @@ private static QueryBuilder buildTermQuery(BoolQueryBuilder queryBuilder, Connec
if (!range.getLow().isLowerUnbounded()) {
switch (range.getLow().getBound()) {
case ABOVE:
rangeQueryBuilder.must(new RangeQueryBuilder(columnName).gt(getValue(session, type, range.getLow().getValue())));
rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).gt(getValue(session, type, range.getLow().getValue())));
break;
case EXACTLY:
rangeQueryBuilder.must(new RangeQueryBuilder(columnName).gte(getValue(session, type, range.getLow().getValue())));
rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).gte(getValue(session, type, range.getLow().getValue())));
break;
case BELOW:
throw new IllegalArgumentException("Low marker should never use BELOW bound");
Expand All @@ -122,10 +122,10 @@ private static QueryBuilder buildTermQuery(BoolQueryBuilder queryBuilder, Connec
if (!range.getHigh().isUpperUnbounded()) {
switch (range.getHigh().getBound()) {
case EXACTLY:
rangeQueryBuilder.must(new RangeQueryBuilder(columnName).lte(getValue(session, type, range.getHigh().getValue())));
rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).lte(getValue(session, type, range.getHigh().getValue())));
break;
case BELOW:
rangeQueryBuilder.must(new RangeQueryBuilder(columnName).lt(getValue(session, type, range.getHigh().getValue())));
rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).lt(getValue(session, type, range.getHigh().getValue())));
break;
case ABOVE:
throw new IllegalArgumentException("High marker should never use ABOVE bound");
Expand All @@ -136,7 +136,7 @@ private static QueryBuilder buildTermQuery(BoolQueryBuilder queryBuilder, Connec
}

if (valuesToInclude.size() == 1) {
rangeQueryBuilder.must(new TermQueryBuilder(columnName, getValue(session, type, getOnlyElement(valuesToInclude))));
rangeQueryBuilder.filter(new TermQueryBuilder(columnName, getValue(session, type, getOnlyElement(valuesToInclude))));
}
queryBuilder.should(rangeQueryBuilder);
}
Expand Down

0 comments on commit e169f88

Please sign in to comment.