在 elasticsearch 中查找不同的值,而不是不同的计数 [英] Find distinct values, not distinct counts in elasticsearch

查看:22
本文介绍了在 elasticsearch 中查找不同的值,而不是不同的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Elasticsearch 文档 建议* 他们的一段代码

*文档已修复

GET /cars/transactions/_search?search_type=count
{
  "aggs": {
    "distinct_colors": {
      "cardinality": {
        "field": "color"
      }
    }
  }
}

对应sql查询

SELECT DISTINCT(color) FROM cars

但它实际上对应于

SELECT COUNT(DISTINCT(color)) FROM cars

我不想知道我有多少不同的值,但有哪些不同的值.有人知道如何实现吗?

I don't want to know how many distinct values I have but what are the distinct values. Anyone knows how to achieve that?

推荐答案

使用 terms 聚合color 字段上.并且您需要注意如何分析您想要获取不同值的字段,这意味着您需要确保在编制索引时没有对其进行标记,否则聚合中的每个条目都将是不同的术语,属于字段内容.

Use a terms aggregation on the color field. And you need to pay attention to how that field you want to get distinct values on is analyzed, meaning you need to make sure you're not tokenizing it while indexing, otherwise every entry in the aggregation will be a different term that is part of the field content.

如果您仍然想要标记化并使用 terms 聚合,您可能需要查看该字段的 not_analyzed 索引类型,并且可能使用 多字段.

If you still want tokenization AND to use the terms aggregation you might want to look at not_analyzed type of indexing for that field, and maybe use multi fields.

汽车的术语聚合:

GET /cars/transactions/_search?search_type=count
{
  "aggs": {
    "distinct_colors": {
      "terms": {
        "field": "color",
        "size": 1000
      }
    }
  }
}

这篇关于在 elasticsearch 中查找不同的值,而不是不同的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆