2

Elasticsearch sort not working on nested field. It's showing mixed of ascending and descending values. Like 40, 30, 50 . It's not showing in ascending order like this: 30, 40, 50

Query:

"sort": [
    {
      "sellerInfoES.offerPrice": {
        "order": "asc",
        "ignore_unmapped": true,
        "missing": "_last"
      }
    }
  ]
1
  • 1
    Please show the data which causes the unexpected result in your post as well. Commented Jul 13, 2016 at 21:10

1 Answer 1

4

Sorting on a nested field is hard, the problem is that you can have multiple nested docs so you have to decide which document to pick. One way is to have the minimum value of a certain field in the nested docs and sort on that value. You can do this using the mode property. You also need the nested_path property.

It should be something like this:

   "sort" : [
       {
          "sellerInfoES.offerPrice" : {
             "mode" :  "avg",
             "order" : "asc",
             "nested_path" : "sellerInfoES"
          }
       }
    ]

More info can be found in the elasticsearch reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_nested_sorting_example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.