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

[Backport 2.x] Remove ApproximateIndexOrDocValuesQuery #16291

Merged
merged 1 commit into from
Oct 14, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Remove identity-related feature flagged code from the RestController ([#15430](https://github.com/opensearch-project/OpenSearch/pull/15430))
- Remove Identity FeatureFlag ([#16024](https://github.com/opensearch-project/OpenSearch/pull/16024))
- Ensure RestHandler.Wrapper delegates all implementations to the wrapped handler ([#16154](https://github.com/opensearch-project/OpenSearch/pull/16154))
- Code cleanup: Remove ApproximateIndexOrDocValuesQuery ([#16273](https://github.com/opensearch-project/OpenSearch/pull/16273))


### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximatePointRangeQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.opensearch.search.lookup.SearchLookup;

import java.io.IOException;
Expand Down Expand Up @@ -112,21 +112,6 @@
: LEGACY_DEFAULT_DATE_TIME_FORMATTER;
}

public static Query getDefaultQuery(Query pointRangeQuery, Query dvQuery, String name, long l, long u) {
return FeatureFlags.isEnabled(FeatureFlags.APPROXIMATE_POINT_RANGE_QUERY_SETTING)
? new ApproximateIndexOrDocValuesQuery(
pointRangeQuery,
new ApproximatePointRangeQuery(name, pack(new long[] { l }).bytes, pack(new long[] { u }).bytes, new long[] { l }.length) {
@Override
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
dvQuery
)
: new IndexOrDocValuesQuery(pointRangeQuery, dvQuery);
}

/**
* Resolution of the date time
*
Expand Down Expand Up @@ -487,22 +472,42 @@
}
DateMathParser parser = forcedDateParser == null ? dateMathParser : forcedDateParser;
return dateRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, parser, context, resolution, (l, u) -> {
Query pointRangeQuery = isSearchable() ? LongPoint.newRangeQuery(name(), l, u) : null;
Query dvQuery = hasDocValues() ? SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u) : null;
if (isSearchable() && hasDocValues()) {
Query query = getDefaultQuery(pointRangeQuery, dvQuery, name(), l, u);
if (context.indexSortedOnField(name())) {
query = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, query);
if (isSearchable()) {
Query pointRangeQuery = LongPoint.newRangeQuery(name(), l, u);
Query query;
if (dvQuery != null) {
query = new IndexOrDocValuesQuery(pointRangeQuery, dvQuery);
if (context.indexSortedOnField(name())) {
query = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, query);

Check warning on line 482 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L482

Added line #L482 was not covered by tests
}
} else {
query = pointRangeQuery;

Check warning on line 485 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L485

Added line #L485 was not covered by tests
}
if (FeatureFlags.isEnabled(FeatureFlags.APPROXIMATE_POINT_RANGE_QUERY_SETTING)) {
return new ApproximateScoreQuery(

Check warning on line 488 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L488

Added line #L488 was not covered by tests
query,
new ApproximatePointRangeQuery(
name(),
pack(new long[] { l }).bytes,
pack(new long[] { u }).bytes,
new long[] { l }.length
) {

Check warning on line 495 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L491-L495

Added lines #L491 - L495 were not covered by tests
@Override
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));

Check warning on line 498 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L498

Added line #L498 was not covered by tests
}
}
);
}
return query;
}
if (hasDocValues()) {
if (context.indexSortedOnField(name())) {
dvQuery = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, dvQuery);
}
return dvQuery;

// Not searchable. Must have doc values.
if (context.indexSortedOnField(name())) {
dvQuery = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, dvQuery);

Check warning on line 508 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L508

Added line #L508 was not covered by tests
}
return pointRangeQuery;
return dvQuery;

Check warning on line 510 in server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java#L510

Added line #L510 was not covered by tests
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.opensearch.common.lucene.search.function.FunctionScoreQuery;
import org.opensearch.index.mapper.DateFieldMapper;
import org.opensearch.index.query.DateRangeIncludingNowQuery;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.opensearch.search.internal.SearchContext;

import java.io.IOException;
Expand Down Expand Up @@ -55,7 +55,7 @@ private Helper() {}
queryWrappers.put(FunctionScoreQuery.class, q -> ((FunctionScoreQuery) q).getSubQuery());
queryWrappers.put(DateRangeIncludingNowQuery.class, q -> ((DateRangeIncludingNowQuery) q).getQuery());
queryWrappers.put(IndexOrDocValuesQuery.class, q -> ((IndexOrDocValuesQuery) q).getIndexQuery());
queryWrappers.put(ApproximateIndexOrDocValuesQuery.class, q -> ((ApproximateIndexOrDocValuesQuery) q).getOriginalQuery());
queryWrappers.put(ApproximateScoreQuery.class, q -> ((ApproximateScoreQuery) q).getOriginalQuery());
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,6 @@ public boolean canApproximate(SearchContext context) {
if (context.aggregations() != null) {
return false;
}
if (!(context.query() instanceof ApproximateIndexOrDocValuesQuery)) {
return false;
}
// size 0 could be set for caching
if (context.from() + context.size() == 0) {
this.setSize(10_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
* Entry-point for the approximation framework.
* This class is heavily inspired by {@link org.apache.lucene.search.IndexOrDocValuesQuery}. It acts as a wrapper that consumer two queries, a regular query and an approximate version of the same. By default, it executes the regular query and returns {@link Weight#scorer} for the original query. At run-time, depending on certain constraints, we can re-write the {@code Weight} to use the approximate weight instead.
*/
public class ApproximateScoreQuery extends Query {
public final class ApproximateScoreQuery extends Query {

private final Query originalQuery;
private final ApproximateQuery approximationQuery;

protected Query resolvedQuery;
Query resolvedQuery;

public ApproximateScoreQuery(Query originalQuery, ApproximateQuery approximationQuery) {
this.originalQuery = originalQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery;
import org.apache.lucene.search.Query;
Expand All @@ -65,8 +66,8 @@
import org.opensearch.index.query.DateRangeIncludingNowQuery;
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximatePointRangeQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.joda.time.DateTimeZone;

import java.io.IOException;
Expand Down Expand Up @@ -213,8 +214,11 @@ public void testTermQuery() {
MappedFieldType ft = new DateFieldType("field");
String date = "2015-10-12T14:10:55";
long instant = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date)).toInstant().toEpochMilli();
Query expected = new ApproximateIndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant, instant + 999),
Query expected = new ApproximateScoreQuery(
new IndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant, instant + 999),
SortedNumericDocValuesField.newSlowRangeQuery("field", instant, instant + 999)
),
new ApproximatePointRangeQuery(
"field",
pack(new long[] { instant }).bytes,
Expand All @@ -225,8 +229,7 @@ public void testTermQuery() {
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
SortedNumericDocValuesField.newSlowRangeQuery("field", instant, instant + 999)
}
);
assumeThat(
"Using Approximate Range Query as default",
Expand Down Expand Up @@ -279,8 +282,11 @@ public void testRangeQuery() throws IOException {
String date2 = "2016-04-28T11:33:52";
long instant1 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date1)).toInstant().toEpochMilli();
long instant2 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date2)).toInstant().toEpochMilli() + 999;
Query expected = new ApproximateIndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
Query expected = new ApproximateScoreQuery(
new IndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
),
new ApproximatePointRangeQuery(
"field",
pack(new long[] { instant1 }).bytes,
Expand All @@ -291,8 +297,7 @@ public void testRangeQuery() throws IOException {
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
}
);
assumeThat(
"Using Approximate Range Query as default",
Expand All @@ -307,8 +312,11 @@ protected String toString(int dimension, byte[] value) {
instant1 = nowInMillis;
instant2 = instant1 + 100;
expected = new DateRangeIncludingNowQuery(
new ApproximateIndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
new ApproximateScoreQuery(
new IndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
),
new ApproximatePointRangeQuery(
"field",
pack(new long[] { instant1 }).bytes,
Expand All @@ -319,8 +327,7 @@ protected String toString(int dimension, byte[] value) {
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
}
)
);
assumeThat(
Expand Down Expand Up @@ -389,8 +396,8 @@ public void testRangeQueryWithIndexSort() {
"field",
instant1,
instant2,
new ApproximateIndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
new ApproximateScoreQuery(
new IndexOrDocValuesQuery(LongPoint.newRangeQuery("field", instant1, instant2), dvQuery),
new ApproximatePointRangeQuery(
"field",
pack(new long[] { instant1 }).bytes,
Expand All @@ -401,8 +408,7 @@ public void testRangeQueryWithIndexSort() {
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
dvQuery
}
)
);
assumeThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.query.QueryStringQueryBuilder;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximatePointRangeQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -191,11 +191,14 @@ public void testDateRangeQuery() throws Exception {
is(true)
);
assertEquals(
new ApproximateIndexOrDocValuesQuery(
LongPoint.newRangeQuery(
DATE_FIELD_NAME,
parser.parse(lowerBoundExact, () -> 0).toEpochMilli(),
parser.parse(upperBoundExact, () -> 0).toEpochMilli()
new ApproximateScoreQuery(
new IndexOrDocValuesQuery(
LongPoint.newRangeQuery(
DATE_FIELD_NAME,
parser.parse(lowerBoundExact, () -> 0).toEpochMilli(),
parser.parse(upperBoundExact, () -> 0).toEpochMilli()
),
controlDv
),
new ApproximatePointRangeQuery(
DATE_FIELD_NAME,
Expand All @@ -207,8 +210,7 @@ public void testDateRangeQuery() throws Exception {
protected String toString(int dimension, byte[] value) {
return Long.toString(LongPoint.decodeDimension(value, 0));
}
},
controlDv
}
),
queryOnDateField
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import org.opensearch.index.mapper.RangeFieldMapper.RangeFieldType;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.query.QueryShardException;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.opensearch.test.IndexSettingsModule;
import org.joda.time.DateTime;
import org.junit.Before;
Expand Down Expand Up @@ -293,7 +293,7 @@ public void testDateRangeQueryUsingMappingFormatLegacy() {
);
assertEquals(
"field:[1465975790000 TO 1466062190999]",
((IndexOrDocValuesQuery) ((ApproximateIndexOrDocValuesQuery) queryOnDateField).getOriginalQuery()).getIndexQuery().toString()
((IndexOrDocValuesQuery) ((ApproximateScoreQuery) queryOnDateField).getOriginalQuery()).getIndexQuery().toString()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.apache.lucene.search.TermQuery;
import org.opensearch.core.common.ParsingException;
import org.opensearch.index.search.MatchQuery.ZeroTermsQuery;
import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery;
import org.opensearch.search.approximate.ApproximateScoreQuery;
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -131,7 +131,7 @@ protected void doAssertLuceneQuery(MatchPhraseQueryBuilder queryBuilder, Query q
.or(instanceOf(PointRangeQuery.class))
.or(instanceOf(IndexOrDocValuesQuery.class))
.or(instanceOf(MatchNoDocsQuery.class))
.or(instanceOf(ApproximateIndexOrDocValuesQuery.class))
.or(instanceOf(ApproximateScoreQuery.class))
);
}

Expand Down
Loading
Loading