I'm struggling to do basic operation with some aggregations. The code below is something i imagined it could works, but it does not. What i wanted to accomplish is to divide each "value_number" which is _count per category bucket with some absolute number (it can be parameter but it does not matter ). So, is there a way to accomplish this? Thanks in advance!
`
"aggs": {
"test": {
"sampler": {
"shard_size": 1000
},
"aggs": {
"categories": {
"terms": {
"field": "category",
"size": 150
}
},
"bucketresults": {
"bucket_script": {
"buckets_path": {
"value_number": "categories>_count"
},
"script": "value_number/100"
}
}
}
}
}
`
CodePudding user response:
You can do using below query. In your current query, you need to move bucketresults
as inner aggregation for categories
. Also, when you access value_number
then you need to access using params.value_number
.
{
"aggs": {
"test": {
"sampler": {
"shard_size": 1000
},
"aggs": {
"categories": {
"terms": {
"field": "category",
"size": 150
},
"aggs": {
"bucketresults": {
"bucket_script": {
"buckets_path": {
"value_number": "_count"
},
"script": "params.value_number/100"
}
}
}
}
}
}
}
}
Below will be response:
"aggregations": {
"test": {
"doc_count": 5,
"categories": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "apple",
"doc_count": 5,
"bucketresults": {
"value": 0.05
}
},
{
"key": "iphone",
"doc_count": 4,
"bucketresults": {
"value": 0.04
}
},
{
"key": "mobile",
"doc_count": 4,
"bucketresults": {
"value": 0.04
}
}
]
}
}
}