【问题标题】:How to sort the result set based on the minimum value of a nested field in elasticsearch?如何根据elasticsearch中嵌套字段的最小值对结果集进行排序?
【发布时间】:2021-01-17 02:17:45
【问题描述】:

我想根据“priceWithPartialDiscount”的最小值对结果集进行排序,“priceWithPartialDiscount”是嵌套字段中的一个字段。

考虑嵌套字段:

"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
}
],

我写了排序的代码:

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

,我期待

"sort": [100.0]

但令人惊讶的是我看到了:

"sort": [200.0]

我做错了吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    删除filter 内的第二级嵌套,因为path 已在父级的嵌套上下文中定义:

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

    我认为第二级嵌套查询以一种不明显的方式弄乱了您的排序值。

    【讨论】:

    • ?? 不错。嘿——不要脸的插件——我正在写一本弹性搜索手册,我在其中更详细地讨论了嵌套字段的用法。如果您能在 2 分钟内找到 let me know 具体您想了解的更多信息,我将不胜感激。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多