【发布时间】:2015-12-16 10:11:17
【问题描述】:
我有一个存储在 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 。但这会返回样本数组中嵌套文档的计数。
但我需要计算每个桶不同的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"
}}}}}
}
编辑 我会尝试改写它......我想获得由“histogram_samples_value”定义的每个桶的单位数。这意味着这些计数的总和应该是单位总数。为了测试它,我编写了一个查询,它只过滤一个单元(许多文档具有不同的样本值)——除了一个“histogram_samples_value”桶之外的所有桶都应该包含 count=0 ,一个桶应该包含 count = 1 。
【问题讨论】:
-
你能添加你的映射吗?这会让事情变得更容易。
-
添加了映射,如有必要,我什至可以更改文档结构 - 我预计最多有 1 亿个。该索引中的文档。
-
这似乎更好。也许我误解了你的问题,但你为什么使用
histogram聚合?您的要求似乎根本不需要它。另外,您能否添加最小预期输出?
标签: java elasticsearch nosql