# How can I search for documents that contain only the given tag X?

**URL:** <https://discuss.elastic.co/t/how-can-i-search-for-documents-that-contain-only-the-given-tag-x/10968>\
**Category:** Elasticsearch\
**Created:** [March 1, 2013, 4:52pm UTC](https://discuss.elastic.co/t/how-can-i-search-for-documents-that-contain-only-the-given-tag-x/10968 "2013-03-01T16:52:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [March 1, 2013, 4:52pm UTC](https://discuss.elastic.co/t/how-can-i-search-for-documents-that-contain-only-the-given-tag-x/10968/1 "2013-03-01T16:52:07Z")

</div>

Given these 3 example records:

{ "tag" : ["cool", "fun"] }  
{ "tag" : ["cool", "happy"] }  
{ "tag" : ["cool"] }

If I search for records with tag "cool", I will get all 3 of these.  
Is there a way for me to get the record that only has the tag "cool" and  
nothing else?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 2, 2013, 3:24am UTC](https://discuss.elastic.co/t/how-can-i-search-for-documents-that-contain-only-the-given-tag-x/10968/2 "2013-03-02T03:24:19Z")

</div>

You can index the number of tags as a separate field and while you are  
searching require that all tags match your query and the number of tags in  
the record is equal to the number of tags in the query. So, assuming that  
tag\_num contains the number of tags in the record, your query for "cool"  
can be expressed as "tag:cool AND tag\_num:1"

On Friday, March 1, 2013 11:52:07 AM UTC-5, Mike wrote:

> Given these 3 example records:
> 
> { "tag" : ["cool", "fun"] }  
> { "tag" : ["cool", "happy"] }  
> { "tag" : ["cool"] }
> 
> If I search for records with tag "cool", I will get all 3 of these.  
> Is there a way for me to get the record that only has the tag "cool" and  
> nothing else?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:48am UTC](https://discuss.elastic.co/t/how-can-i-search-for-documents-that-contain-only-the-given-tag-x/10968/3 "2017-07-06T02:48:45Z")

</div>


