Skip to content

Commit

Permalink
Fixed inefficient Stream API call chains ending with count() (opensea…
Browse files Browse the repository at this point in the history
…rch-project#15386)

* Fixed inefficient Stream API call chains ending with count()

Signed-off-by: Dmitry Kryukov <dk2k@ya.ru>

* Refactored method minTermLength() as per @sandeshkr419's advice

Signed-off-by: Dmitry Kryukov <dk2k@ya.ru>

* Added a line in CHANGELOG.md

Signed-off-by: Dmitry Kryukov <dk2k@ya.ru>

---------

Signed-off-by: Dmitry Kryukov <dk2k@ya.ru>
  • Loading branch information
dk2k committed Oct 17, 2024
1 parent 11f8ef1 commit 8cff5f1
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix wrong default value when setting `index.number_of_routing_shards` to null on index creation ([#16331](https://github.com/opensearch-project/OpenSearch/pull/16331))
- Fix disk usage exceeds threshold cluster can't spin up issue ([#15258](https://github.com/opensearch-project/OpenSearch/pull/15258)))

- Fix inefficient Stream API call chains ending with count() ([#15386](https://github.com/opensearch-project/OpenSearch/pull/15386))

### Security

Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8cff5f1

Please sign in to comment.