在弹性搜索中找到不同的值,而不是不同的数值 [英] Find distinct values, not distinct counts in elasticsearch

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

问题描述

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?

推荐答案

使用 color 字段中的/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.htmlrel =noreferrer>术语聚合 。您需要注意如何在要分析的领域中获得不同的值,这意味着您需要确保在索引时不会对其进行标记,否则聚合中的每个条目都将是不同的术语,它是字段内容

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.

如果您仍然希望使用标记化并使用术语聚合,您可能需要查看 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
      }
    }
  }
}

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

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