简体   繁体   中英

How to sort the result set based on the minimum value of a nested field in elasticsearch?

I would like to sort the result set on the basis of the minimum value of "priceWithPartialDiscount", which is a field inside a nested field.

Consider the nested field:

"children": [
{
    "partialDiscountPercent": 0.0,
    "productId": 497071,
    "price": 200.0,
    "priceWithPartialDiscount": 200.0,
    "partialDiscountAmount": 0.0
},
{
    "partialDiscountPercent": 0.0,
    "productId": 497072,
    "price": 100.0,
    "priceWithPartialDiscount": 100.0,
    "partialDiscountAmount": 0.0
}
],

I wrote the code for sort:

"sort":[{
   "children.priceWithPartialDiscount":{
      "missing":"_last",
      "mode":"min",
      "nested":{
         "filter":{
            "nested":{
               "path":"children",
               "query":{
                  "range":{
                     "children.price":{
                        "gte":0.0
                     }
                  }
               }
            }
         },
         "path":"children"
      },
      "order":"asc"
   }
}]

, and I expect

"sort": [100.0]

But surprisingly I see:

"sort": [200.0]

Did I make a mistake?

Remove the 2nd level nested-ness inside of the filter because the path is already defined in the parent's nested context:

{
  "sort": [
    {
      "children.priceWithPartialDiscount": {
        "missing":"_last",
        "mode": "min",
        "nested": {
          "filter": {
            "range": {
              "children.price": {
                "gte": 0
              }
            }
          },
          "path": "children"
        },
        "order": "asc"
      }
    }
  ]
}

I think that 2nd level nested query was messing up your sort values in a non-obvious way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM