Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestedSortBuilder usage example for elasticsearch 6.3.2

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?

like image 697
humbleCoder Avatar asked Mar 06 '23 16:03

humbleCoder


1 Answers

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"
            }
          }
        }
      ]
like image 149
humbleCoder Avatar answered Mar 15 '23 16:03

humbleCoder