I need an aggregation query to get a bucket with all my root folders. All documents in my elasticsearch have a field named path where I store an array with the paths where the document is located ( e.g. path=[1.3., 1.2.4, 5., 11] ).
If I use the normal terms aggregation
"terms": {
"field": "path.keyword"
}
I unfortunately get all unique paths:
"buckets" : [
{
"key" : "1.3."
"doc_count" : 6
},
{
"key" : "11."
"doc_count" : 3
},
{
"key" : "5."
"doc_count" : 3
},
{
"key" : "1.2.4."
"doc_count" : 1
}
]
I've tried to solve it using a painless script
"terms": {
"script": "doc['path.keyword'].value.substring(0, doc['path.keyword'].value.indexOf('.') )"
}
but then I only get the last elements of my path array
"buckets" : [
{
"key" : "1",
"doc_count" : 7
},
{
"key" : "11",
"doc_count" : 3
}
]
how do I only get the root folders?