且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在弹性搜索中找到不同的值,而不是不同的数值

更新时间:2023-02-08 19:14:49

使用 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
      }
    }
  }
}