# How to query for exact value?

**URL:** <https://discuss.elastic.co/t/how-to-query-for-exact-value/7319>\
**Category:** Elasticsearch\
**Created:** [April 12, 2012, 9:56pm UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319 "2012-04-12T21:56:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![benmccann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benmccann/32/420_2.png) [@benmccann](https://discuss.elastic.co/u/benmccann)\
**Post date:** [April 12, 2012, 9:56pm UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/1 "2012-04-12T21:56:31Z")

</div>

Hi,

I would like to find records in my index where emails.raw is exactly  
"benjamin.mccann@gmail.com".

I'm doing a text phrase query for "benjamin.mccann@gmail.com" and it  
returned all the results below. Is text phrase the wrong kind of query? I  
tried a term query and that returned 0 results.

{"firstName":"Benjamin","lastName":"McCann","workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"benjamin.mccann@gmail.com"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[user-sc.1332715735.deghmmmfbjpomgmchhjd-benjamin.mccann=gmail.com](http://user-sc.1332715735.deghmmmfbjpomgmchhjd-benjamin.mccann=gmail.com)@cassandra.apache.org"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=gmail.com](http://dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=gmail.com)@cassandra.apache.org"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=gmail.com](http://user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=gmail.com)@cassandra.apache.org"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[dev-allow-sc.1330720003.ggfgcibbmiiknljcbcdk-benjamin.mccann=gmail.com](http://dev-allow-sc.1330720003.ggfgcibbmiiknljcbcdk-benjamin.mccann=gmail.com)@lucene.apache.org"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[dev-allow-sc.1330630372.hgdkjmjdbhbkghbhfeee-benjamin.mccann=gmail.com](http://dev-allow-sc.1330630372.hgdkjmjdbhbkghbhfeee-benjamin.mccann=gmail.com)@lucene.apache.org"}]}  
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"[dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=gmail.com](http://dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=gmail.com)@lucene.apache.org"}]}

Any help would be greatly appreciated!

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 12, 2012, 10:12pm UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/2 "2012-04-12T22:12:31Z")

</div>

As far as I know there are at least two solutions:

1. set the index mapping of emails.raw to "not\_analyzed" and then you can  
do the 'term' query
2. I think 'query\_string' query looking for  
""benjamin.mccann@gmail.com"" should  
work.

Please correct me if I am wrong because I am new in ES 🙂

Best regards.  
Marcin

2012/4/12 Ben McCann [benjamin.j.mccann@gmail.com](mailto:benjamin.j.mccann@gmail.com)

> Hi,
> 
> I would like to find records in my index where emails.raw is exactly "  
> [benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)".
> 
> I'm doing a text phrase query for "[benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)" and it  
> returned all the results below. Is text phrase the wrong kind of query? I  
> tried a term query and that returned 0 results.
> 
> {"firstName":"Benjamin","lastName":"McCann","workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"  
> [benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"user-sc.1332715735.deghmmmfbjpomgmchhjd-benjamin.mccann=  
> [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=  
> [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=  
> [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330720003.ggfgcibbmiiknljcbcdk-benjamin.mccann=  
> [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330630372.hgdkjmjdbhbkghbhfeee-benjamin.mccann=  
> [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> 
> {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=  
> [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> 
> Any help would be greatly appreciated!
> 
> Thanks,  
> Ben

---

<div class="post-metadata">

**Author:** ![Ben\_McCann\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ben_mccann_2/32/1097_2.png) [@Ben\_McCann\_2](https://discuss.elastic.co/u/Ben_McCann_2)\
**Post date:** [April 12, 2012, 10:59pm UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/3 "2012-04-12T22:59:48Z")

</div>

Just tried the second suggestion and it didn't seem to work. The query  
still returned too many results:  
SearchRequest request = new SearchRequest(index)  
.types(type)  
.source(new SearchSourceBuilder()  
.query(QueryBuilders.queryString(""benjamin.mccann@gmail.com  
"").field("emails.raw")));

Thanks for the tips!

On Thu, Apr 12, 2012 at 3:12 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:

> As far as I know there are at least two solutions:
> 
> 1. set the index mapping of emails.raw to "not\_analyzed" and then you can  
> do the 'term' query
> 2. I think 'query\_string' query looking for ""benjamin.mccann@gmail.com"" should  
> work.
> 
> Please correct me if I am wrong because I am new in ES 🙂
> 
> Best regards.  
> Marcin
> 
> 2012/4/12 Ben McCann [benjamin.j.mccann@gmail.com](mailto:benjamin.j.mccann@gmail.com)
> 
> > Hi,
> > 
> > I would like to find records in my index where emails.raw is exactly "  
> > [benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)".
> > 
> > I'm doing a text phrase query for "[benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)" and it  
> > returned all the results below. Is text phrase the wrong kind of query? I  
> > tried a term query and that returned 0 results.
> > 
> > {"firstName":"Benjamin","lastName":"McCann","workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"  
> > [benjamin.mccann@gmail.com](mailto:benjamin.mccann@gmail.com)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"user-sc.1332715735.deghmmmfbjpomgmchhjd-benjamin.mccann=  
> > [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=  
> > [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=  
> > [gmail.com@cassandra.apache.org](mailto:gmail.com@cassandra.apache.org)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330720003.ggfgcibbmiiknljcbcdk-benjamin.mccann=  
> > [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330630372.hgdkjmjdbhbkghbhfeee-benjamin.mccann=  
> > [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> > 
> > {"workHistoryItems":,"educationHistoryItems":,"emails":[{"raw":"dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=  
> > [gmail.com@lucene.apache.org](mailto:gmail.com@lucene.apache.org)"}]}
> > 
> > Any help would be greatly appreciated!
> > 
> > Thanks,  
> > Ben

---

<div class="post-metadata">

**Author:** ![Mohit\_Chawla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohit_chawla/32/2243_2.png) [@Mohit\_Chawla](https://discuss.elastic.co/u/Mohit_Chawla)\
**Post date:** [April 13, 2012, 4:44am UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/4 "2012-04-13T04:44:14Z")

</div>

Hello,

On Fri, Apr 13, 2012 at 4:29 AM, Ben McCann [ben@benmccann.com](mailto:ben@benmccann.com) wrote:

> Just tried the second suggestion and it didn't seem to work. The query  
> still returned too many results:  
> SearchRequest request = new SearchRequest(index)  
> .types(type)  
> .source(new SearchSourceBuilder()
> 
> .query(QueryBuilders.queryString(""benjamin.mccann@gmail.com"").field("emails.raw")));

You do need to set that field to be not\_analyzed.

---

<div class="post-metadata">

**Author:** ![Shaun](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Shaun](https://discuss.elastic.co/u/Shaun)\
**Post date:** [April 13, 2012, 6:36am UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/5 "2012-04-13T06:36:50Z")

</div>

I had exactly the same problem with some name data I want to query as  
terms...  
the multi mapping to create the email.raw type field referred to above  
looks like this:

"Maker" : {  
"type" : "multi\_field",  
"fields" : {  
"Maker" : {"type" : "string", "index" :  
"analyzed"},  
"asterm" : {"type" : "string", "index" :  
"not\_analyzed"}  
}  
}

From what I can see we are also at the mercy of the query parser  
analyser which seems to breaks things like [benjamin.mcc...@gmail.com](mailto:benjamin.mcc...@gmail.com)  
into "benjamin mcc gmail com" on the way in when you use a term query

Shaun

On Apr 13, 5:44 am, Mohit Chawla [mohit.chawla.bin...@gmail.com](mailto:mohit.chawla.bin...@gmail.com)  
wrote:

> Hello,
> 
> On Fri, Apr 13, 2012 at 4:29 AM, Ben McCann [b...@benmccann.com](mailto:b...@benmccann.com) wrote:
> 
> > Just tried the second suggestion and it didn't seem to work. The query  
> > still returned too many results:  
> > SearchRequest request = new SearchRequest(index)  
> > .types(type)  
> > .source(new SearchSourceBuilder()
> 
> > .query(QueryBuilders.queryString(""benjamin.mcc...@gmail.com"").field("emails.raw")));
> 
> You do need to set that field to be not\_analyzed.

---

<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, 3:32am UTC](https://discuss.elastic.co/t/how-to-query-for-exact-value/7319/6 "2017-07-06T03:32:46Z")

</div>


