【问题标题】:How to do elasticsearch aggregation together with sort and find duplicate values如何进行弹性搜索聚合以及排序和查找重复值
【发布时间】:2021-10-26 21:46:45
【问题描述】:

我想查找重复值,如果有重复值,那么我会根据上次更新进行排序,所以我采用的是最新的,我该如何进行聚合?我已经尝试过这种聚合。

我尝试向源添加排序但它仍然不起作用,我尝试了几种方法但它仍然失败有时它会出现 1 但只有旧数据,有时顺序是最新的正确但出现 2数据

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "BILLING_TYPE_CD": "Service Bundle"
          }
        },
        {
          "match": {
            "ID": "xxxx"
          }
        },
        {
          "exists": {
            "field": "LI_MILESTONE"
          }
        },
        {
          "exists": {
            "field": "LI_SID"
          }
        },
        {
          "query_string": {
            "default_field": "LI_SID",
            "query": "*xxxx*"
          }
        }
      ],
      "must_not": {
        "bool": {
          "must": [
            {
              "query_string": {
                "default_field": "LI_PRODUCT_NAME",
                "query": "*Network*"
              }
            },
            {
              "terms": {
                "LI_MILESTONE.keyword": [
                  "Abandoned",
                  "Cancelled"
                ]
              }
            },
            {
              "terms": {
                "ORDER_STATUS.keyword": [
                  "Abandoned",
                  "Cancelled",
                  "Drop In Progress"
                ]
              }
            },
            {
              "term": {
                "STATUS.keyword": ""
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "TGL_CREATED": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "list_products": {
      "composite": {
        "size": 50000,
        "sources": [
          {
            "LI_SID": {
              "terms": {
                "field": "LI_SID.keyword",
                "order": "desc"
              }
            }
          }
        ]
      },
      "aggs": {
        "totalService": {
          "terms": {
            "field": "LI_SID.keyword",
            "size": 50000,
            "order": {
              "_term": "asc"
            }
          }
        },
        "bucket_sort": {
          "bucket_sort": {
            "from": 0,
            "size": 10
          }
        },
        "includes_source": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": [
                "LAST_UPDATE",
                "xxxxx",
                "xxxxx",
                "xxxxx",
                "xxx"
              ]
            }
          }
        }
      }
    },
    "term_product": {
      "terms": {
        "field": "LI_SID.keyword",
        "size": 50000
      }
    }
  }
}

【问题讨论】:

  • 能否分享一些示例索引数据和预期的搜索结果?
  • @ESCoder 此示例索引数据和预期结果。 pastebin.com/raw/QyL9AkQ2 在这个示例中它有两个数据,具有相同的 LI_SID 和不同的日期时间,如何从这个重复的 LI_SID 中获取一个并从 LAST_UPDATE 获取最新值

标签: node.js elasticsearch elastic-stack


【解决方案1】:

像这样?

{
  "aggs": {
    "LI_SID": {
      "terms": {
        "field": "LI_SID.keyword",
        "size": 10
      },
      "aggs": {
        "hit": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "LAST_UPDATE": "desc"
              }
            ]
          }
        }
      }
    }
  },
  "size": 0
}

您需要使用aggregations 响应而不是hits

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多