# Elastic Search- Fetch Distinct Tags

**URL:** <https://discuss.elastic.co/t/elastic-search-fetch-distinct-tags/21961>\
**Category:** Elasticsearch\
**Created:** [February 3, 2015, 4:22pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-distinct-tags/21961 "2015-02-03T16:22:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ritesh\_Kumar\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ritesh_kumar_gupta/32/939_2.png) [@Ritesh\_Kumar\_Gupta](https://discuss.elastic.co/u/Ritesh_Kumar_Gupta)\
**Post date:** [February 3, 2015, 4:22pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-distinct-tags/21961/1 "2015-02-03T16:22:33Z")

</div>

I have document of following format:

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

{  
\_id:"2",  
tags: ["orange","guava", "mango shakes", "apple pie", "grammar"]  
}

{

\_id:"3",  
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  
example).

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": [  
"tags"  
],  
"query": "g\*",  
"fuzziness":0  
}  
}  
]  
}  
},  
"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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/9219c044-195b-4e0e-ba21-f9f7c0bc5ebd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:35am UTC](https://discuss.elastic.co/t/elastic-search-fetch-distinct-tags/21961/2 "2017-07-06T00:35:08Z")

</div>

Closed on July 6, 2017, 12:35am UTC


