Skip to content

Commit 64d95b9

Browse files
dk2kdbwiddis
authored andcommitted
Fixed inefficient Stream API call chains ending with count() (opensearch-project#15386)
* Fixed inefficient Stream API call chains ending with count() Signed-off-by: Dmitry Kryukov <[email protected]> * Refactored method minTermLength() as per @sandeshkr419's advice Signed-off-by: Dmitry Kryukov <[email protected]> * Added a line in CHANGELOG.md Signed-off-by: Dmitry Kryukov <[email protected]> --------- Signed-off-by: Dmitry Kryukov <[email protected]>
1 parent df6c12b commit 64d95b9

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8383
- Fix multi-search with template doesn't return status code ([#16265](https://github.com/opensearch-project/OpenSearch/pull/16265))
8484
- [Streaming Indexing] Fix intermittent 'The bulk request must be terminated by a newline [\n]' failures [#16337](https://github.com/opensearch-project/OpenSearch/pull/16337))
8585
- 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))
86+
- Fix inefficient Stream API call chains ending with count() ([#15386](https://github.com/opensearch-project/OpenSearch/pull/15386))
8687

8788
### Security
8889

modules/percolator/src/main/java/org/opensearch/percolator/QueryAnalyzer.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,25 @@ static Result selectBestResult(Result result1, Result result2) {
510510
}
511511

512512
private static int minTermLength(Set<QueryExtraction> extractions) {
513-
// In case there are only range extractions, then we return Integer.MIN_VALUE,
514-
// so that selectBestExtraction(...) we are likely to prefer the extractions that contains at least a single extraction
515-
if (extractions.stream().filter(queryExtraction -> queryExtraction.term != null).count() == 0
516-
&& extractions.stream().filter(queryExtraction -> queryExtraction.range != null).count() > 0) {
517-
return Integer.MIN_VALUE;
518-
}
519-
513+
boolean hasTerm = false;
514+
boolean hasRange = false;
520515
int min = Integer.MAX_VALUE;
516+
521517
for (QueryExtraction qt : extractions) {
522518
if (qt.term != null) {
519+
hasTerm = true;
523520
min = Math.min(min, qt.bytes().length);
524521
}
522+
if (qt.range != null) {
523+
hasRange = true;
524+
}
525525
}
526+
527+
// If there are no terms but there are ranges, return Integer.MIN_VALUE
528+
if (!hasTerm && hasRange) {
529+
return Integer.MIN_VALUE;
530+
}
531+
526532
return min;
527533
}
528534

0 commit comments

Comments
 (0)