Skip to content

Commit bee5e60

Browse files
Backport of 18189 PR
Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent ea48ab0 commit bee5e60

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55

66
## [Unreleased 3.0.x]
77
### Added
8+
Improve sort-query performance by retaining the default `totalHitsThreshold` for approximated `match_all` queries ([#18189](https://github.com/opensearch-project/OpenSearch/pull/18189))
89

910
### Changed
1011

server/src/internalClusterTest/java/org/opensearch/search/sort/FieldSortIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ public void testSortMissingNumbersMinMax() throws Exception {
11451145
.get();
11461146
assertNoFailures(searchResponse);
11471147

1148-
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(2L));
1148+
assertThat(searchResponse.getHits().getTotalHits().value(), equalTo(3L));
11491149
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
11501150
// The order here could be unstable (depends on document order) since missing == field value
11511151
assertThat(searchResponse.getHits().getAt(1).getId(), is(oneOf("3", "2")));

server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,12 @@ public boolean canApproximate(SearchContext context) {
444444
if (context.trackTotalHitsUpTo() == SearchContext.TRACK_TOTAL_HITS_ACCURATE) {
445445
return false;
446446
}
447+
447448
// size 0 could be set for caching
448449
if (context.from() + context.size() == 0) {
449450
this.setSize(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO);
450451
} else {
451-
this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo() + 1));
452+
this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo()));
452453
}
453454
if (context.request() != null && context.request().source() != null) {
454455
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source());

server/src/main/java/org/opensearch/search/approximate/ApproximateScoreQuery.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.search.IndexSearcher;
1313
import org.apache.lucene.search.Query;
1414
import org.apache.lucene.search.QueryVisitor;
15+
import org.apache.lucene.search.ScoreMode;
1516
import org.apache.lucene.search.Weight;
1617
import org.opensearch.search.internal.SearchContext;
1718

@@ -47,7 +48,11 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException {
4748
// Default to the original query. This suggests that we were not called from ContextIndexSearcher.
4849
return originalQuery.rewrite(indexSearcher);
4950
}
50-
return resolvedQuery.rewrite(indexSearcher);
51+
Query rewritten = resolvedQuery.rewrite(indexSearcher);
52+
if (rewritten != resolvedQuery) {
53+
resolvedQuery = rewritten;
54+
}
55+
return this;
5156
}
5257

5358
public void setContext(SearchContext context) {
@@ -78,6 +83,15 @@ public boolean equals(Object o) {
7883
return true;
7984
}
8085

86+
@Override
87+
public Weight createWeight(IndexSearcher indexSearcher, ScoreMode scoreMode, float boost) throws IOException {
88+
if (resolvedQuery == null) {
89+
// Default to the original query.
90+
return originalQuery.createWeight(indexSearcher, scoreMode, boost);
91+
}
92+
return resolvedQuery.createWeight(indexSearcher, scoreMode, boost);
93+
}
94+
8195
@Override
8296
public int hashCode() {
8397
int h = classHash();

server/src/main/java/org/opensearch/search/query/TopDocsCollectorContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.opensearch.common.util.CachedSupplier;
7575
import org.opensearch.index.search.OpenSearchToParentBlockJoinQuery;
7676
import org.opensearch.search.DocValueFormat;
77+
import org.opensearch.search.approximate.ApproximateScoreQuery;
7778
import org.opensearch.search.collapse.CollapseContext;
7879
import org.opensearch.search.internal.ScrollContext;
7980
import org.opensearch.search.internal.SearchContext;
@@ -724,6 +725,8 @@ static int shortcutTotalHitCount(IndexReader reader, Query query) throws IOExcep
724725
query = ((ConstantScoreQuery) query).getQuery();
725726
} else if (query instanceof BoostQuery) {
726727
query = ((BoostQuery) query).getQuery();
728+
} else if (query instanceof ApproximateScoreQuery) {
729+
query = ((ApproximateScoreQuery) query).getOriginalQuery();
727730
} else {
728731
break;
729732
}

server/src/test/java/org/opensearch/index/mapper/DateFieldTypeTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,20 @@ public void testRangeQuery() throws IOException {
287287
String date2 = "2016-04-28T11:33:52";
288288
long instant1 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date1)).toInstant().toEpochMilli();
289289
long instant2 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date2)).toInstant().toEpochMilli() + 999;
290-
Query expected = new ApproximatePointRangeQuery(
290+
ApproximatePointRangeQuery approximatePointRangeQuery = new ApproximatePointRangeQuery(
291291
"field",
292292
pack(new long[] { instant1 }).bytes,
293293
pack(new long[] { instant2 }).bytes,
294294
new long[] { instant1 }.length,
295295
ApproximatePointRangeQuery.LONG_FORMAT
296296
);
297+
Query expected = new ApproximateScoreQuery(
298+
new IndexOrDocValuesQuery(
299+
LongPoint.newRangeQuery("field", instant1, instant2),
300+
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
301+
),
302+
approximatePointRangeQuery
303+
);
297304
Query rangeQuery = ft.rangeQuery(date1, date2, true, true, null, null, null, context);
298305
assertTrue(rangeQuery instanceof ApproximateScoreQuery);
299306
((ApproximateScoreQuery) rangeQuery).setContext(new TestSearchContext(context));

0 commit comments

Comments
 (0)