Elastic Search- Fetch Distinct Tags

I have document of following format:

_id :"1",
tags:["guava","apple","mango", "banana", "gulmohar"]

tags: ["orange","guava", "mango shakes", "apple pie", "grammar"]


tags: ["guava","apple","grapes", "water", "grammar","gulmohar","water-melon", "green"]

Now, I want to fetch unique tags starting with prefix p*, so that these
unique tags will be display by tag suggestors(Stackoverflow site is an

For example: Whenever user types, 'g': "guava", "gulmohar", "grammar",
"grapes" and "green"should be returned as a result. ie. the query should
returns distinct tags with prefix g*.

I tried everywhere, browse whole documentations, searched es forum, but I
didn't find any clue, much to my dismay.

I tried aggregations, but aggregations returns the distinct count for whole
words/token in tags field. It does not return the unique list of tags
starting with 'g'.

"query": {
"filtered": {
"query": {
"bool": {
"should": [
"query_string": {
"allow_leading_wildcard": false,
"fields": [
"query": "g*",
"filter": {
//some condition on other field...


"aggs": {
"distinct_tags": {
"terms": {
"field": "tags",
"size": 10

result of above: guava(3), apple(2), mango(1)

Can someone please suggest me the correct way to fetch all the distinct
tags with prefix input_prefix*?

You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9219c044-195b-4e0e-ba21-f9f7c0bc5ebd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.