How to query for exact value?


(Ben McCann) #1

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@cassandra.apache.org"}]}
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=gmail.com@cassandra.apache.org"}]}
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=gmail.com@cassandra.apache.org"}]}
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"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@lucene.apache.org"}]}
{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=gmail.com@lucene.apache.org"}]}

Any help would be greatly appreciated!

Thanks,
Ben


(Marcin Dojwa) #2

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 :slight_smile:

Best regards.
Marcin

2012/4/12 Ben McCann benjamin.j.mccann@gmail.com

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@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=
gmail.com@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=
gmail.com@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"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@lucene.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=
gmail.com@lucene.apache.org"}]}

Any help would be greatly appreciated!

Thanks,
Ben


(Ben McCann-2) #3

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.comwrote:

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 :slight_smile:

Best regards.
Marcin

2012/4/12 Ben McCann benjamin.j.mccann@gmail.com

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@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-sc.1332908539.nmahbiejchkcbbagdiag-benjamin.mccann=
gmail.com@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"user-uc.1333550602.niiaiinjhppkkebphfbh-benjamin.mccann=
gmail.com@cassandra.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"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@lucene.apache.org"}]}

{"workHistoryItems":[],"educationHistoryItems":[],"emails":[{"raw":"dev-allow-sc.1330644438.bpaipceggbmijigjjegl-benjamin.mccann=
gmail.com@lucene.apache.org"}]}

Any help would be greatly appreciated!

Thanks,
Ben


(Mohit Chawla) #4

Hello,

On Fri, Apr 13, 2012 at 4:29 AM, Ben McCann 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.


(Shaun) #5

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
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
wrote:

Hello,

On Fri, Apr 13, 2012 at 4:29 AM, Ben McCann 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.


(system) #6