Fix TopHitsAggregationBuilder adding duplicate _score sort clauses (#42179) (#42343)

When using High Level Rest Client Java API to produce search query, using AggregationBuilders.topHits("th").sort("_score", SortOrder.DESC)
caused query to contain duplicate sort clauses.
This commit is contained in:
Ignacio Vera 2019-05-22 14:02:52 +02:00 committed by GitHub
parent eb1639c5fc
commit 3a20ff7e86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -237,8 +237,9 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
} }
if (name.equals(ScoreSortBuilder.NAME)) { if (name.equals(ScoreSortBuilder.NAME)) {
sort(SortBuilders.scoreSort().order(order)); sort(SortBuilders.scoreSort().order(order));
} else {
sort(SortBuilders.fieldSort(name).order(order));
} }
sort(SortBuilders.fieldSort(name).order(order));
return this; return this;
} }
@ -254,8 +255,9 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder<TopHit
} }
if (name.equals(ScoreSortBuilder.NAME)) { if (name.equals(ScoreSortBuilder.NAME)) {
sort(SortBuilders.scoreSort()); sort(SortBuilders.scoreSort());
} else {
sort(SortBuilders.fieldSort(name));
} }
sort(SortBuilders.fieldSort(name));
return this; return this;
} }

View File

@ -49,7 +49,6 @@ import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.TopHits;
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
@ -207,4 +206,10 @@ public class TopHitsAggregatorTests extends AggregatorTestCase {
reader.close(); reader.close();
directory.close(); directory.close();
} }
public void testSortByScore() throws Exception {
// just check that it does not fail with exceptions
testCase(new MatchAllDocsQuery(), topHits("_name").sort("_score", SortOrder.DESC));
testCase(new MatchAllDocsQuery(), topHits("_name").sort("_score"));
}
} }