简体   繁体   English

Elasticsearch - 计算有多少记录具有特定字段值,用于多个字段

[英]Elasticsearch - Count how many records have specific field values, for multiple fields

I have an elasticsearch index containing documents like this:我有一个包含如下文档的 elasticsearch 索引:

{"id": 1, "red": true, "green": true, "blue": true }
{"id": 2, "red": false, "green": false, "blue": true }
{"id": 3, "red": false, "green": true }
{"id": 4, "red": true, "green": true, "blue": false }

For each of the color attributes, I want to count how many true I have.对于每个颜色属性,我想计算我有多少真实。 New colors may appear anytime.新的 colors 随时可能出现。 Essentially, I need that output, in some form or another:本质上,我需要某种形式的 output:

red: 2
green: 3
blue: 2

How can I get that in one query, ideally with a DSL or SQL query?我怎样才能在一个查询中得到它,最好是使用 DSL 或 SQL 查询?

Bonus points if I can turn that into a data transform / rollup per day.如果我可以将其转化为每天的数据转换/汇总,则可以加分。

you can use Bucket aggregations您可以使用 Bucket 聚合

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

smth like that:像这样:

GET /my-index-000001/_search获取 /my-index-000001/_search

{
"aggs": {
    "red": {
      "terms": {
        "field": "red"
      }
    },
    "green": {
      "terms": {
        "field": "green"
      }
    },
    "blue":{ 
      "terms": {
        "field": "green"
      }
    }
  }
}

It can also be done with SQL, with something like this:也可以使用 SQL 来完成,如下所示:

select sum(red::int) as red
     , sum(green::int) as green
     , sum(blue::int) as blue
from my_index

Not entirely sure how the NULLs are handled in this case (not tested), but it works at least with non-null values.不完全确定在这种情况下如何处理 NULL(未测试),但它至少适用于非空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch 如何计算索引中所有文档中多个字段的特定值? - Elasticsearch how to count specific values from multiple fields across all documents in index? 如何在 Elasticsearch 中使用 multi_match 查询传递多个值以在多个记录中搜索多个字段 - How to pass multiple values to search in multiple fields over multiple records using multi_match query in Elasticsearch elasticsearch 获取唯一值,并且值计入多个字段 - elasticsearch get unique values, and the values count on multiple fields Elasticsearch。 获取特定索引字段中的值计数 - Elasticsearch. Get count of values in a field of specific index 如何在elasticsearch中过滤多个字段和值? - How to filter with multiple fields and values in elasticsearch? ElasticSearch计算按字段分组的多个字段 - ElasticSearch count multiple fields grouped by 通过多个字段聚合并在ElasticSearch中计数 - Aggregate by multiple fields and count in ElasticSearch 如何计算嵌套字段内的字段数? - 弹性搜索 - How to count number of fields inside nested field? - Elasticsearch 计算 Elasticsearch 中有多少文档具有某个属性或缺少该属性 - Count how many documents have an attribute or are missing that attribute in Elasticsearch 如何根据 Elasticsearch 中的特定字段获取非空值的计数 - how to get count of not-null value based on specific field in Elasticsearch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM