|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.search.approximate; |
| 10 | + |
| 11 | +import org.apache.lucene.search.IndexSearcher; |
| 12 | +import org.apache.lucene.search.Query; |
| 13 | +import org.apache.lucene.search.QueryVisitor; |
| 14 | +import org.opensearch.index.mapper.MappedFieldType; |
| 15 | +import org.opensearch.search.internal.SearchContext; |
| 16 | +import org.opensearch.search.sort.FieldSortBuilder; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +/** |
| 22 | + * Replaces match-all query with a less expensive query if possible. |
| 23 | + * <p> |
| 24 | + * Currently, will rewrite to a bounded range query over the high/low end of a field if a primary sort is specified |
| 25 | + * on that field. |
| 26 | + */ |
| 27 | +public class ApproximateMatchAllQuery extends ApproximateQuery { |
| 28 | + private ApproximateQuery approximation = null; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected boolean canApproximate(SearchContext context) { |
| 32 | + approximation = null; |
| 33 | + if (context == null) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + if (context.aggregations() != null) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + if (context.request() != null && context.request().source() != null && context.innerHits().getInnerHits().isEmpty()) { |
| 41 | + FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source()); |
| 42 | + if (primarySortField != null |
| 43 | + && primarySortField.missing() == null |
| 44 | + && !primarySortField.fieldName().equals(FieldSortBuilder.DOC_FIELD_NAME) |
| 45 | + && !primarySortField.fieldName().equals(FieldSortBuilder.ID_FIELD_NAME)) { |
| 46 | + MappedFieldType mappedFieldType = context.getQueryShardContext().fieldMapper(primarySortField.fieldName()); |
| 47 | + if (mappedFieldType == null) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + Query rangeQuery = mappedFieldType.rangeQuery(null, null, false, false, null, null, null, context.getQueryShardContext()); |
| 51 | + if (rangeQuery instanceof ApproximateScoreQuery approximateScoreQuery) { |
| 52 | + approximateScoreQuery.setContext(context); |
| 53 | + if (approximateScoreQuery.resolvedQuery instanceof ApproximateQuery) { |
| 54 | + approximation = (ApproximateQuery) approximateScoreQuery.resolvedQuery; |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String toString(String field) { |
| 65 | + return "Approximate(*:*)"; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void visit(QueryVisitor visitor) { |
| 70 | + visitor.visitLeaf(this); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean equals(Object o) { |
| 76 | + if (sameClassAs(o)) { |
| 77 | + ApproximateMatchAllQuery other = (ApproximateMatchAllQuery) o; |
| 78 | + return Objects.equals(approximation, other.approximation); |
| 79 | + } |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int hashCode() { |
| 85 | + return classHash(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Query rewrite(IndexSearcher indexSearcher) throws IOException { |
| 90 | + if (approximation == null) { |
| 91 | + throw new IllegalStateException("rewrite called without setting context or query could not be approximated"); |
| 92 | + } |
| 93 | + return approximation.rewrite(indexSearcher); |
| 94 | + } |
| 95 | +} |
0 commit comments