Skip to content

Skip approximation when track_total_hits is set to true #18017

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

Merged
merged 3 commits into from
Apr 24, 2025
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 @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed
- Change the default max header size from 8KB to 16KB. ([#18024](https://github.com/opensearch-project/OpenSearch/pull/18024))
- Avoid invalid retries in multiple replicas when querying [#17370](https://github.com/opensearch-project/OpenSearch/pull/17370)
- Skip approximation when `track_total_hits` is set to `true` [#18017](https://github.com/opensearch-project/OpenSearch/pull/18017)

### Dependencies
- Bump `com.google.code.gson:gson` from 2.12.1 to 2.13.0 ([#17923](https://github.com/opensearch-project/OpenSearch/pull/17923))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected boolean canApproximate(SearchContext context) {
if (context.aggregations() != null) {
return false;
}
// Exclude approximation when "track_total_hits": true
if (context.trackTotalHitsUpTo() == SearchContext.TRACK_TOTAL_HITS_ACCURATE) {
return false;
}

if (context.request() != null && context.request().source() != null && context.innerHits().getInnerHits().isEmpty()) {
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ public boolean canApproximate(SearchContext context) {
if (context.aggregations() != null) {
return false;
}
// Exclude approximation when "track_total_hits": true
if (context.trackTotalHitsUpTo() == SearchContext.TRACK_TOTAL_HITS_ACCURATE) {
return false;
}

// size 0 could be set for caching
if (context.from() + context.size() == 0) {
this.setSize(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.opensearch.search.aggregations.AggregatorFactories;
import org.opensearch.search.aggregations.SearchContextAggregations;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.internal.ShardSearchRequest;
import org.opensearch.search.sort.FieldSortBuilder;
import org.opensearch.search.sort.SortOrder;
Expand Down Expand Up @@ -105,4 +106,60 @@ public ShardSearchRequest request() {
assertThrows(IllegalStateException.class, () -> approximateMatchAllQuery.rewrite(null));
}

public void testCannotApproximateWithTrackTotalHits() {
ApproximateMatchAllQuery approximateMatchAllQuery = new ApproximateMatchAllQuery();

ShardSearchRequest[] shardSearchRequest = new ShardSearchRequest[1];

MapperService mockMapper = mock(MapperService.class);
String sortfield = "myfield";
MappedFieldType myFieldType = new NumberFieldMapper.NumberFieldType(sortfield, NumberFieldMapper.NumberType.LONG);
when(mockMapper.fieldType(sortfield)).thenReturn(myFieldType);

Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.build();
IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
QueryShardContext queryShardContext = new QueryShardContext(
0,
new IndexSettings(indexMetadata, settings),
BigArrays.NON_RECYCLING_INSTANCE,
null,
null,
mockMapper,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
TestSearchContext searchContext = new TestSearchContext(queryShardContext) {
@Override
public ShardSearchRequest request() {
return shardSearchRequest[0];
}
};

SearchSourceBuilder source = new SearchSourceBuilder();
shardSearchRequest[0] = new ShardSearchRequest(null, System.currentTimeMillis(), null);
shardSearchRequest[0].source(source);
source.sort(sortfield, SortOrder.ASC);

assertTrue(approximateMatchAllQuery.canApproximate(searchContext));

searchContext.trackTotalHitsUpTo(SearchContext.TRACK_TOTAL_HITS_ACCURATE);
assertFalse("Should not approximate when track_total_hits is accurate", approximateMatchAllQuery.canApproximate(searchContext));

searchContext.trackTotalHitsUpTo(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO);
assertTrue("Should approximate when track_total_hits is not accurate", approximateMatchAllQuery.canApproximate(searchContext));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static java.util.Arrays.asList;
import static org.apache.lucene.document.LongPoint.pack;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ApproximatePointRangeQueryTests extends OpenSearchTestCase {

Expand Down Expand Up @@ -372,4 +373,23 @@ public boolean canApproximate(SearchContext context) {
SearchContext searchContext = mock(SearchContext.class);
assertTrue(queryCanApproximate.canApproximate(searchContext));
}

public void testCannotApproximateWithTrackTotalHits() {
ApproximatePointRangeQuery query = new ApproximatePointRangeQuery(
"point",
pack(0).bytes,
pack(20).bytes,
1,
ApproximatePointRangeQuery.LONG_FORMAT
);
SearchContext mockContext = mock(SearchContext.class);
when(mockContext.trackTotalHitsUpTo()).thenReturn(SearchContext.TRACK_TOTAL_HITS_ACCURATE);
assertFalse(query.canApproximate(mockContext));
when(mockContext.trackTotalHitsUpTo()).thenReturn(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO);
when(mockContext.aggregations()).thenReturn(null);
when(mockContext.from()).thenReturn(0);
when(mockContext.size()).thenReturn(10);
when(mockContext.request()).thenReturn(null);
assertTrue(query.canApproximate(mockContext));
}
}
Loading