szymond
szymond

Reputation: 1310

Preparing nested sorting filter using ES java library

Some time ago I had an issue with sorting results by one of values in nested documents. It turned out the problem was with my query. Instead of:

"sort": [
   {
      "children.size": {
         "order": "desc",
         "nested_filter": {
            "term": {
               "id": 1
            }
         }
      }
   }
]

I was using:

"sort": [
    {
      "children.size": {
        "order": "asc",
        "nested_filter": {
          "nested": {
            "filter": {
              "term": {
                "id": 1
              }
            },
            "path": "children"
          }
        }
      }
    }
]

Incorrect query was strongly inspired by code generated by ES library (elasticsearch-1.4.jar). Here's a snippet I've been using to prepare sorting part of the query:

FieldSortBuilder mySort = SortBuilders.fieldSort("children.size")
              .setNestedFilter(FilterBuilders.nestedFilter("children", FilterBuilders.termFilter("id", myId)))
              .sortMode("avg")
              .order(SortOrder.ASC);    

When such "sort" is added to the query, the end result is improper nested filter.

Am using the library in a wrong way? How can I easily overcome this problem?

Upvotes: 1

Views: 938

Answers (1)

szymond
szymond

Reputation: 1310

Answer to this question appeared here, but somehow got removed. Anyway, it was explained to me that I am using wrong filter inside of method setNestedFilter - I should not provide there a nested filter, but it should be a term filter. Changing that seems to fix the issue for me.

Upvotes: 2

Related Questions