Skip to content

Commit

Permalink
Fix TopHitsAggregationBuilder adding duplicate _score sort clauses (#…
Browse files Browse the repository at this point in the history
…42179)

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.
  • Loading branch information
Hohol authored and iverase committed May 22, 2019
1 parent 5fb55f6 commit 9609497
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ public TopHitsAggregationBuilder sort(String name, SortOrder order) {
}
if (name.equals(ScoreSortBuilder.NAME)) {
sort(SortBuilders.scoreSort().order(order));
} else {
sort(SortBuilders.fieldSort(name).order(order));
}
sort(SortBuilders.fieldSort(name).order(order));
return this;
}

Expand All @@ -249,8 +250,9 @@ public TopHitsAggregationBuilder sort(String name) {
}
if (name.equals(ScoreSortBuilder.NAME)) {
sort(SortBuilders.scoreSort());
} else {
sort(SortBuilders.fieldSort(name));
}
sort(SortBuilders.fieldSort(name));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.AggregatorTestCase;
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.sort.SortOrder;

Expand Down Expand Up @@ -207,4 +206,10 @@ public void testSetScorer() throws Exception {
reader.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"));
}
}

0 comments on commit 9609497

Please sign in to comment.