I'm trying to use total hit count with the bucket_script aggregation as sub aggregation of terms aggregation. Any suggestions are welcome.
I also tried value_count and terms aggregation on the package id field. But it also didn't work.
Mapping:
PUT /packages/_mapping/_doc
{
"properties": {
"items": {
"type": "nested"
}
}
}
Example document:
{
"id": 1,
"name": "Lorem",
"items": [
{
"infos": {
"available": true
},
"item": {
"id": 1,
"meta": false,
"name": "Ipsum"
}
},
{
"infos": {
"available": false
},
"item": {
"id": 2,
"meta": false,
"name": "Ipsum 2"
}
},
{
"infos": {
"available": false
},
"item": {
"id": 3,
"meta": false,
"name": "Ipsum 3"
}
}
]
}
My aggregation query:
GET /packages/_search
{
"query": { "match_all": {} },
"size": 0,
"aggs": {
"items": {
"nested": {
"path": "items"
},
"aggs": {
"non_meta": {
"filter": {
"term": {
"items.item.meta": false
}
},
"aggs": {
"item": {
"terms": {
"field": "items.item.id"
},
"aggs": {
"total_count": { <--------------------- I want to get total document count here, but I can only reach `doc_count` for docs that includes items
"reverse_nested": {}
},
"available_count": {
"sum": {
"script": {
"source": "doc['items.infos.available'].value == true ? 1 : 0"
}
}
},
"penetration": {
"bucket_script": {
"buckets_path": {
"available": "available_count",
"total": "total_count>_count"
},
"script": "params.available / params.total * 100"
}
}
}
}
}
}
}
}
}
}