Find distinct values, not distinct counts in elasticsearch

前端 未结 4 1084
陌清茗
陌清茗 2020-11-30 06:38

Elasticsearch documentation suggests* that their piece of code

*documentation fixed

GET /cars/transactions/_search?search_type=         


        
4条回答
  •  隐瞒了意图╮
    2020-11-30 07:06

    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.

    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.

    Terms aggregation for cars:

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

提交回复
热议问题