-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Approximate match all with sort with a bounded range query
If we run a match-all query and sort results by a numeric/timestamp field, then we can replace the match-all with a filter over the top/bottom 10,000 (or whatever `track_total_hits` is set to) values from the sort field. Signed-off-by: Michael Froh <[email protected]>
- Loading branch information
Showing
4 changed files
with
126 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
server/src/main/java/org/opensearch/search/approximate/ApproximateMatchAllQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.approximate; | ||
|
||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.QueryVisitor; | ||
import org.opensearch.index.mapper.MappedFieldType; | ||
import org.opensearch.search.internal.SearchContext; | ||
import org.opensearch.search.sort.FieldSortBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Replaces match-all query with a less expensive query if possible. | ||
* <p> | ||
* Currently, will rewrite to a bounded range query over the high/low end of a field if a primary sort is specified | ||
* on that field. | ||
*/ | ||
public class ApproximateMatchAllQuery extends ApproximateQuery { | ||
private ApproximateQuery approximation = null; | ||
|
||
@Override | ||
protected boolean canApproximate(SearchContext context) { | ||
if (context == null) { | ||
return false; | ||
} | ||
if (context.aggregations() != null) { | ||
return false; | ||
} | ||
|
||
if (context.request() != null && context.request().source() != null) { | ||
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source()); | ||
if (primarySortField != null && primarySortField.missing() == null) { | ||
MappedFieldType mappedFieldType = context.getQueryShardContext().fieldMapper(primarySortField.fieldName()); | ||
Query rangeQuery = mappedFieldType.rangeQuery(null, null, false, false, null, null, null, context.getQueryShardContext()); | ||
if (rangeQuery instanceof ApproximateScoreQuery) { | ||
ApproximateScoreQuery approximateScoreQuery = (ApproximateScoreQuery) rangeQuery; | ||
approximateScoreQuery.setContext(context); | ||
if (approximateScoreQuery.resolvedQuery instanceof ApproximateQuery) { | ||
approximation = (ApproximateQuery) approximateScoreQuery.resolvedQuery; | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString(String field) { | ||
return "Approximate(*:*)"; | ||
} | ||
|
||
@Override | ||
public void visit(QueryVisitor visitor) { | ||
visitor.visitLeaf(this); | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return sameClassAs(o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return classHash(); | ||
} | ||
|
||
@Override | ||
public Query rewrite(IndexSearcher indexSearcher) throws IOException { | ||
if (approximation == null) { | ||
throw new IllegalStateException("rewrite called without setting context or query could not be approximated"); | ||
} | ||
return approximation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters