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

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.

1 Like