I indexed some documents on Elaticsearch and one of the fields of the documents is "users" which is an array of users. Now I try to find documents per user where the user name matches some prefix. The prefix is used to filter documents. But I want the returned buckets to be also filtered by the same prefix as well. Is there anyway to do that? Below samples explain the problem.
Create an index test1:
curl -XPUT 'http://localhost:9200/test1/'
Create 9 documents for type type1 in index test1:
i=1
while [ $i -lt 10 ] ; do
curl -XPUT "localhost:9200/test1/type1/$i" -d '{
"users" : ["ram", "hari", "balu"]
}'
i=`expr $i + 1`
done
Now run the aggregations for documents per user where user name matches the prefix "ra"
curl -XPOST 'localhost:9200/test1/type1/_search?pretty' -d '
{
"size" : 0,
"aggs": {
"prefix_match_aggr": {
"filter" : { "prefix" : {"users" : "ra"}},
"aggs" : {
"actual_aggr" : {
"terms": {
"field" : "users",
"size" : 0
}
}
}
}
}
}'
The aggregation returns the below:
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"prefix_match_aggr" : {
"doc_count" : 9,
"actual_aggr" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "balu",
"doc_count" : 9
}, {
"key" : "hari",
"doc_count" : 9
}, {
"key" : "ram",
"doc_count" : 9
} ]
}
}
}
}
As we can see, it returned buckets for all the users balu, hari and ram. Is there a way to only return the buckets matching the prefix "ra" i.e. the buckets for ram, rami, raman etc.?