示例文档:
{
"id": "62655",
"attributes": [
{
"name": "genre",
"value": "comedy"
},
{
"name": "year",
"value": "2016"
}
]
}
{
"id": "62656",
"attributes": [
{
"name": "genre",
"value": "horror"
},
{
"name": "year",
"value": "2016"
}
]
}
{
"id": "62657",
"attributes": [
{
"name": "language",
"value": "english"
},
{
"name": "year",
"value": "2015"
}
]
}
预期输出:
{
"hits" : {
"total": 3,
"hits": []
},
"aggregations": {
"attribCount": {
"language": 1,
"genre": 2,
"year": 3
},
"attribVals": {
"language": {
"english": 1
},
"genre": {
"comedy": 1,
"horror": 1
},
"year": {
"2016": 2,
"2015": 1
}
}
}
}
我的查询:
我可以使用以下查询获得">attribCount"聚合。但我不知道如何获得每个属性值的计数。
{
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
},
"aggs": {
"attribCount": {
"terms": {
"field": "attributes.name",
"size": 0
}
}
},
"size": 0
}
当我使用attributes.value进行聚合时,它会给出总计数。但我需要将它列在预期输出中给定的名称值下。
正如您所说,属性字段是嵌套的。试试这个,这将工作
{
"size": 0,
"aggs": {
"count": {
"nested": {
"path": "attributes"
},
"aggs": {
"attribCount": {
"terms": {
"field": "attributes.name"
}
},
"attribVal": {
"terms": {
"field": "attributes.name"
},
"aggs": {
"attribval2": {
"terms": {
"field": "attributes.value"
}
}
}
}
}
}
}
}