Skip to content

Commit b01438a

Browse files
authored
Re-enable parallel collection for field sorted top hits (#125916)
With #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 #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 61d92d6 commit b01438a

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
@@ -741,4 +741,11 @@ public FieldSortBuilder rewrite(QueryRewriteContext ctx) throws IOException {
741741
}
742742
return new FieldSortBuilder(this).setNestedSort(rewrite);
743743
}
744+
745+
@Override
746+
public boolean supportsParallelCollection() {
747+
// Disable parallel collection for sort by field.
748+
// It is supported but not optimized on the Lucene side to share info across collectors, and can cause regressions.
749+
return false;
750+
}
744751
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -721,4 +721,11 @@ public GeoDistanceSortBuilder rewrite(QueryRewriteContext ctx) throws IOExceptio
721721
}
722722
return new GeoDistanceSortBuilder(this).setNestedSort(rewrite);
723723
}
724+
725+
@Override
726+
public boolean supportsParallelCollection() {
727+
// Disable parallel collection for sort by field.
728+
// It is supported but not optimized on the Lucene side to share info across collectors, and can cause regressions.
729+
return false;
730+
}
724731
}

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

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

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

-5
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,4 @@ public ScriptSortBuilder rewrite(QueryRewriteContext ctx) throws IOException {
502502
}
503503
return new ScriptSortBuilder(this).setNestedSort(rewrite);
504504
}
505-
506-
@Override
507-
public boolean supportsParallelCollection() {
508-
return true;
509-
}
510505
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,6 @@ public String toString() {
285285
}
286286

287287
public boolean supportsParallelCollection() {
288-
return false;
288+
return true;
289289
}
290290
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ public void testSupportsParallelCollection() {
10071007
{
10081008
SearchSourceBuilder searchSourceBuilder = newSearchSourceBuilder.get();
10091009
searchSourceBuilder.aggregation(new TopHitsAggregationBuilder("tophits").sort(SortBuilders.fieldSort("field")));
1010-
assertFalse(searchSourceBuilder.supportsParallelCollection(fieldCardinality));
1010+
assertTrue(searchSourceBuilder.supportsParallelCollection(fieldCardinality));
10111011
}
10121012
{
10131013
SearchSourceBuilder searchSourceBuilder = newSearchSourceBuilder.get();

0 commit comments

Comments
 (0)