java Elasticsearch聚合查询
我在elasticsearch中存储了一组文档,它们看起来像这样:
{
"id": "12312312",
"timestamp": "2015-11-01T00:00:00.000",
"unit": {
"id": "123456",
"name": "unit-4"
},
"samples": [
{
"value": 244.05435180062133,
"aggregation": "M",
"type": {
"name": "SomeName1",
"display": "Some name 1"
}
},
{
"value": 251.19450064653438,
"aggregation": "I",
"type": {
"name": "SomeName2",
"display": "Some name 2"
}
},
...
]
}
我想对它运行一个聚合查询,它将返回属性samples.value
的每个存储桶的unit.id
计数,
查询应该基于samples.type.name
和samples.aggregation
。我制作了这样的东西:
{
"query": {
"bool": {
"must": [{
"range": {
"timestamp": {
"gte": "2015-11-01T00:00:00.000",
"lte": "2015-11-30T23:59:59.999",
"format": "date_hour_minute_second_fraction"
}
}
}, {
"nested": {
"path": "samples",
"query": {
"bool": {
"must": [{
"match": {
"samples.type.name": "SomeName1"
}
}]
}
}
}
}]
}
},
"aggs": {
"0": {
"nested": {
"path": "samples"
},
"aggs": {
"1": {
"histogram": {
"field": "samples.value",
"interval": 10
}
}
}
}
}
}
我在询问http://localhost:9200/dc/sample/_search?search_type=count&pretty
。但这会返回samples数组中嵌套文档的计数。
但我需要计算每个桶的不同unit.id
你们能帮帮我吗
编辑:添加了映射
{
"dc" : {
"mappings" : {
"sample" : {
"unit" : {
"properties" : {
"name" : {
"type" : "string"
}}},
"samples" : {
"type" : "nested",
"properties" : {
"aggregation" : {
"type" : "string"
},
"type" : {
"properties" : {
"display" : {
"type" : "string"
},
"name" : {
"type" : "string"
}
}
},
"value" : {
"type" : "double"
}
}
},
"timestamp" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
}}}}}
}
编辑 我会试着换个说法。。。我想得到“直方图样本值”定义的每个桶的单位数。这意味着这些计数的总和应该是单元的总数。为了测试它,我编写了一个查询,它只过滤一个单元(许多具有不同样本值的文档)——除了一个“直方图样本值”存储桶外,所有存储桶都应该包含count=0,一个存储桶应该包含count=1
共 (0) 个答案