Skip to content

Commit 2b1c26b

Browse files
committed
Re-enable parallel collection for field sorted top hits (elastic#125916)
With elastic#123610 we disabled parallel collection for field and script sorted top hits, aligning its behaviour with that of top level search. This was mainly to work around a bug in script sorting that did not support inter-segment concurrency. The bug with script sort has been fixed with elastic#123757 and concurrency re-enabled for it. While sort by field is not optimized for search concurrency, top hits benefits from it and disabling concurrency for sort by field in top hits has caused performance regressions in our nightly benchmarks. This commit re-enables concurrency for top hits with sort by field is used. This introduces back a discrepancy between top level search and top hits, in that concurrency is applied for top hits despite sort by field normally disables it. The key difference is the context where sorting is applied, and the fact that concurrency is disabled only for performance reasons on top level searches and not for functional reasons.
1 parent 4936a11 commit 2b1c26b

File tree

8 files changed

+21
-27
lines changed

8 files changed

+21
-27
lines changed

docs/changelog/125916.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 125916
2+
summary: Re-enable parallel collection for field sorted top hits
3+
area: Search
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/search/aggregations/metrics/TopHitsAggregationBuilder.java

-15
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.util.Objects;
5050
import java.util.Optional;
5151
import java.util.Set;
52-
import java.util.function.ToLongFunction;
5352

5453
public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHitsAggregationBuilder> {
5554
public static final String NAME = "top_hits";
@@ -823,18 +822,4 @@ public String getType() {
823822
public TransportVersion getMinimalSupportedVersion() {
824823
return TransportVersions.ZERO;
825824
}
826-
827-
@Override
828-
public boolean supportsParallelCollection(ToLongFunction<String> fieldCardinalityResolver) {
829-
if (sorts != null) {
830-
// the implicit sorting is by _score, which supports parallel collection
831-
for (SortBuilder<?> sortBuilder : sorts) {
832-
if (sortBuilder.supportsParallelCollection() == false) {
833-
return false;
834-
}
835-
}
836-
}
837-
838-
return super.supportsParallelCollection(fieldCardinalityResolver);
839-
}
840825
}

server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java

+7
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,11 @@ public FieldSortBuilder rewrite(QueryRewriteContext ctx) throws IOException {
749749
}
750750
return new FieldSortBuilder(this).setNestedSort(rewrite);
751751
}
752+
753+
@Override
754+
public boolean supportsParallelCollection() {
755+
// Disable parallel collection for sort by field.
756+
// It is supported but not optimized on the Lucene side to share info across collectors, and can cause regressions.
757+
return false;
758+
}
752759
}

server/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java

+7
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,11 @@ public GeoDistanceSortBuilder rewrite(QueryRewriteContext ctx) throws IOExceptio
746746
}
747747
return new GeoDistanceSortBuilder(this).setNestedSort(rewrite);
748748
}
749+
750+
@Override
751+
public boolean supportsParallelCollection() {
752+
// Disable parallel collection for sort by field.
753+
// It is supported but not optimized on the Lucene side to share info across collectors, and can cause regressions.
754+
return false;
755+
}
749756
}

server/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java

-5
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,4 @@ public TransportVersion getMinimalSupportedVersion() {
173173
public ScoreSortBuilder rewrite(QueryRewriteContext ctx) {
174174
return this;
175175
}
176-
177-
@Override
178-
public boolean supportsParallelCollection() {
179-
return true;
180-
}
181176
}

server/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java

-5
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,4 @@ public ScriptSortBuilder rewrite(QueryRewriteContext ctx) throws IOException {
511511
}
512512
return new ScriptSortBuilder(this).setNestedSort(rewrite);
513513
}
514-
515-
@Override
516-
public boolean supportsParallelCollection() {
517-
return true;
518-
}
519514
}

server/src/main/java/org/elasticsearch/search/sort/SortBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,6 @@ public String toString() {
290290
}
291291

292292
public boolean supportsParallelCollection() {
293-
return false;
293+
return true;
294294
}
295295
}

server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ public void testSupportsParallelCollection() {
10301030
{
10311031
SearchSourceBuilder searchSourceBuilder = newSearchSourceBuilder.get();
10321032
searchSourceBuilder.aggregation(new TopHitsAggregationBuilder("tophits").sort(SortBuilders.fieldSort("field")));
1033-
assertFalse(searchSourceBuilder.supportsParallelCollection(fieldCardinality));
1033+
assertTrue(searchSourceBuilder.supportsParallelCollection(fieldCardinality));
10341034
}
10351035
{
10361036
SearchSourceBuilder searchSourceBuilder = newSearchSourceBuilder.get();

0 commit comments

Comments
 (0)