[英]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.