基于最大值或最小值的折叠结果

llmtgqce  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(195)

我有这种格式的数据。

[
    {
        lead_id: "lead_1",
        value: 34
    },
    {
        lead_id: "lead_1",
        value: 22
    },
    {
        lead_id: "lead_1",
        value: 67
    },
    {
        lead_id: "lead_2",
        value: 12
    },
    {
        lead_id: "lead_2",
        value: 9
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_3",
        value: 5
    }
]

我想根据descasc顺序中的值对销售线索进行排序。此外,我还需要破坏结果。我对排序使用了类似这样的查询。

{
    "collapse": {
        "field": "lead_id"
    },
    "sort": {
        "value": { "order": "asc" }
    }
}

但我想根据maximum值进行排序。也就是说,如果我在asc中排序,我希望结果是:

[
    {
        lead_id: "lead_2",
        value: 12
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_1",
        value: 67
    }
]

而对于下降

[
    {
        lead_id: "lead_1",
        value: 67
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_2",
        value: 12
    }
]
iqjalb3h

iqjalb3h1#

作为一项功能的CLABLE_SORT尚不可用。有一个打开的PR,因此它可能会在将来可用。

解决方法是使用INTERN_HITS

{
  "collapse": {
    "field": "lead_id.keyword",
    "inner_hits": {
      "name": "most_recent",                  
      "size": 1,                              
      "sort": [ { "value": "desc" } ]    
    }
  },
  "sort": {
    "value": {
      "order": "asc"
    }
  }
}

结果

"hits" : [
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "xAugp4MBfEI_j4sNVw3V",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_3",
          "value" : 5
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_3"
          ]
        },
        "sort" : [
          5
        ],
        "inner_hits" : {  --> group sorted values
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wwugp4MBfEI_j4sNTw1c",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_3",
                    "value" : 22
                  },
                  "sort" : [
                    22
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "wgugp4MBfEI_j4sNSA00",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_2",
          "value" : 9
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_2"
          ]
        },
        "sort" : [
          9
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wQugp4MBfEI_j4sNQg3t",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_2",
                    "value" : 12
                  },
                  "sort" : [
                    12
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "vwugp4MBfEI_j4sNNw0q",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_1",
          "value" : 22
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_1"
          ]
        },
        "sort" : [
          22
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wAugp4MBfEI_j4sNPQ3F",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_1",
                    "value" : 67
                  },
                  "sort" : [
                    67
                  ]
                }
              ]
            }
          }
        }
      }
    ]

父文档仍将按照常规排序对值进行排序。组排序值可以从内部命中中选取

相关问题