# Need help constructing a query

**URL:** <https://discuss.elastic.co/t/need-help-constructing-a-query/10385>\
**Category:** Elasticsearch\
**Created:** [January 17, 2013, 7:32am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385 "2013-01-17T07:32:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [January 17, 2013, 7:32am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385/1 "2013-01-17T07:32:20Z")

</div>

Hi, I need help constructing a query on an index of people

Sample document:

{

```
 "name": "Benedict Simeon", 

 "tag": ["human", "single parent", "single"]

```

}

I need to search for people who have just the tag "single" and not "single  
parent", so I wrote a query

{

"query":

{

"query\_string":

{

"default\_field": "tag",

"query": ""single""

}

}

}

The problem now is that results contain documents with tags "single parent"  
even if the tag "single" is not present. How do I construct my query to  
return only results that contain just "single". With regex I would have  
expressed my query has "^single$", can I do this in elasticsearch?

Also, the tag field is analyzed.

Thanks

--

---

<div class="post-metadata">

**Author:** ![Rafal\_Kuc\_3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rafal_kuc_3/32/799_2.png) [@Rafal\_Kuc\_3](https://discuss.elastic.co/u/Rafal_Kuc_3)\
**Post date:** [January 17, 2013, 7:59am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385/2 "2013-01-17T07:59:14Z")

</div>

Hello!

You probably have your tag field analyzed (which is the default) behavior - in such case, the "single parent" will be broken in two tokens "single" and "parent". And that's why your query find the 'single' word even if only 'single parent' is provided. If you would like to match the exact content set the tag field to "index" : "not\_analyzed" and reindexing your data. You can also try only looking for phrases ([http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html) - phrase query), but I would suggest going for not analyzed field.

_--_

Regards,

Rafał Kuć

Sematext :: [http://sematext.com/](http://sematext.com/) _:: Solr - Lucene - Nutch - ElasticSearch_

Hi, I need help constructing a query on an index of people

Sample document:

{

```
 "name": "Benedict Simeon", 

 "tag": ["human", "single parent", "single"]

```

}

I need to search for people who have just the tag "single" and not "single parent", so I wrote a query

{

"query":

{

"query\_string":

{

"default\_field": "tag",

"query": ""single""

}

}

}

The problem now is that results contain documents with tags "single parent" even if the tag "single" is not present. How do I construct my query to return only results that contain just "single". With regex I would have expressed my query has "^single$", can I do this in elasticsearch?

Also, the tag field is analyzed.

Thanks

--

---

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [January 17, 2013, 9:55am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385/3 "2013-01-17T09:55:07Z")

</div>

I think i will go with the index: not\_analyzed field. Thank you

--

---

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [January 17, 2013, 9:57am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385/4 "2013-01-17T09:57:20Z")

</div>

I will go with the not\_analyzed field. Thank you

--

---

<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:55am UTC](https://discuss.elastic.co/t/need-help-constructing-a-query/10385/5 "2017-07-06T02:55:58Z")

</div>


