NestedSortBuilder usage example for elasticsearch 6.3.2

2.8k views Asked by At

I have to sort on a field that is one level nested using the elastic java high level rest client api. I could find this answer

Elasticsearch nested sorting

The problem is that the answer uses SortBuilder for sorting nested fields using the following code :

SortBuilder sb = SortBuilders.fieldSort("authorList.lastName")
    .order(SortOrder.ASC)
    .setNestedPath("authorList")
    .setNestedFilter(matchFirst);

However it seems that nestedPath and NestedFilter have been deprecated in 6.3.2 elastic (deprication info)and a new NestedSortBuilder has been introduced. However I was not able to build a query using it. Can anyone please explain how to use it or perhaps point me to an example where it has been used?

1

There are 1 answers

0
humbleCoder On BEST ANSWER

Actually was easy. This works for me :

SortBuilders.fieldSort("bulkOrders.expiryDate").order(SortOrder.asc).setNestedSort(new NestedSortBuilder("bulkOrders"));

This is equivalent to :

"sort": [
        {
          "bulkOrders.expiryDate": {
            "order": "asc",
            "nested": {
              "path": "bulkOrders"
            }
          }
        }
      ]