【问题标题】:SQL DISTINCT Pair of Values in ElasticsearchElasticsearch 中的 SQL DISTINCT 值对
【发布时间】:2021-06-01 06:24:11
【问题描述】:

想问一下如何在Elasticsearch中使用SQL DISTINCT Pair of Values。如下所示,

SELECT distinct username, city from country_table

我可以只针对一个字段运行以下查询。在 aggs json 中,我只能看到“用户名”字段。

但我需要 DISTINCT 用户名,城市对。

{
  "_source": ["username","city"]
  "aggs": {
     "unique_ids": 
        {
           "terms": { "fields": "username" }
        }
  }
}

我该如何解决这个问题?

感谢回复

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用multi_terms aggregation

    {
      "size": 0,
      "aggs": {
        "unique_pair": {
          "multi_terms": {
            "terms": [
              {
                "field": "username"
              },
              {
                "field": "city"
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

    • 值得一提的是,这仅从7.12开始提供
    • 是的,这就是答案,但我可以问一个问题吗?是否可以将结果发送到另一个索引?我的意思是另一个索引中的结果(用户名,城市)?
    • @kndr 是的,你可以这样做using transforms
    • 我已经尝试了答案。那运行完美。但是,如果我在转换 api“聚合”中使用这个“聚合”。我得到“不支持的聚合类型 [multi_terms]”@Val
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多