下面是我的弹性文档,我想在其上发出聚合查询。
{
"id": 1,
"attributes":[
{
"fieldId": 1,
"value": "Male"
},
{
"fieldId": 2,
"value": "12/11/2015"
}
}
{
"id": 2,
"attributes":[
{
"fieldId": 1,
"value": "Male"
},
{
"fieldId": 2,
"value": "11/11/2015"
}
}
结果必须如下。
[
{
"key" : "Male",
"doc_count" : 1
}
]
[
{
"key" : "12/11/2015",
"doc_count" : 1
},
{
"key" : "11/11/2015",
"doc_count" : 1
}
]
有没有办法在弹性搜索中实现这一目标。
答案 0 :(得分:3)
这是可能的。见这个例子:
我们必须将属性映射为nested
类型才能正确聚合。
PUT /test
{
"mappings": {
"sample": {
"properties": {
"id": {
"type": "integer"
},
"attributes": {
"type": "nested",
"properties": {
"fieldId": {
"type": "integer"
},
"value": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
让我们添加您给定的测试数据:
PUT /test/sample/1
{"id":1,"attributes":[{"fieldId":1,"value":"Male"},{"fieldId":2,"value":"12/11/2015"}]}
PUT /test/sample/2
{"id":2,"attributes":[{"fieldId":1,"value":"Male"},{"fieldId":2,"value":"11/11/2015"}]}
最后让我们运行这个查询:
GET /test/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"Nest": {
"nested": {
"path": "attributes"
},
"aggs": {
"fieldIds": {
"terms": {
"field": "attributes.fieldId",
"size": 0
},
"aggs": {
"values": {
"terms": {
"field": "attributes.value",
"size": 0
}
}
}
}
}
}
}
}
它会做什么?
nested
聚合,以便进入nested
个对象并正确聚合。terms
聚合为每个fieldId
创建存储桶,在您的情况下,我们会得到其中两个:1
和2
。terms
聚合,以获得相应的值。这就是输出。
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"Nest": {
"doc_count": 4,
"fieldIds": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 2,
"values": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Male",
"doc_count": 2
}
]
}
},
{
"key": 2,
"doc_count": 2,
"values": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "11/11/2015",
"doc_count": 1
},
{
"key": "12/11/2015",
"doc_count": 1
}
]
}
}
]
}
}
}
}
这并不是你要求的。但这与Elasticsearch最接近的是什么。