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] Fixed inefficient Stream API call chains ending with count() (#15386) #16361

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -83,6 +83,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix multi-search with template doesn't return status code ([#16265](https:/opensearch-project/OpenSearch/pull/16265))
- [Streaming Indexing] Fix intermittent 'The bulk request must be terminated by a newline [\n]' failures [#16337](https:/opensearch-project/OpenSearch/pull/16337))
- Fix wrong default value when setting `index.number_of_routing_shards` to null on index creation ([#16331](https:/opensearch-project/OpenSearch/pull/16331))
- Fix inefficient Stream API call chains ending with count() ([#15386](https:/opensearch-project/OpenSearch/pull/15386))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,19 +510,25 @@ static Result selectBestResult(Result result1, Result result2) {
}

private static int minTermLength(Set<QueryExtraction> extractions) {
// In case there are only range extractions, then we return Integer.MIN_VALUE,
// so that selectBestExtraction(...) we are likely to prefer the extractions that contains at least a single extraction
if (extractions.stream().filter(queryExtraction -> queryExtraction.term != null).count() == 0
&& extractions.stream().filter(queryExtraction -> queryExtraction.range != null).count() > 0) {
return Integer.MIN_VALUE;
}

boolean hasTerm = false;
boolean hasRange = false;
int min = Integer.MAX_VALUE;

for (QueryExtraction qt : extractions) {
if (qt.term != null) {
hasTerm = true;
min = Math.min(min, qt.bytes().length);
}
if (qt.range != null) {
hasRange = true;
}
}

// If there are no terms but there are ranges, return Integer.MIN_VALUE
if (!hasTerm && hasRange) {
return Integer.MIN_VALUE;
}

return min;
}

Expand Down
Loading