2
votes

This is my elastic search query for fetching the id

curl -XGET localhost:9200/test-index2/business/_search -d'
 {
     "query" : {
         "filtered" : { 
             "query" : {
                 "match_all" : {} 
             },
             "filter" : {
                 "term" : { 
                     "_id" : "AU6LqK0WCSY7HKQGengx"
                 }
             }
         }
     }
 }'

And this is part of the response

{"contactNumber": "+1-415-392-3702", "name": "Golden Gate Hotel"}

I've got the contactNumber and name

Now my second query -> i'm querying for the above contact number using term filter

curl -XGET localhost:9200/test-index2/business/_search -d'
 {
     "query" : {
         "filtered" : { 
             "query" : {
                 "match_all" : {} 
             },
             "filter" : {
                 "term" : { 
                     "contactNumber" : "+1-415-392-3702"
                 }
             }
         }
     }
 }'

and i've got 0 hits!

I've indexed both the contactNumber and name field.

What am i doing wrong??

I should be getting the exact same record back

Edit:
Attaching the mapping for contact number

{"test-index2":{"mappings":{"business":{"properties":{"address":{"type":"string"},"contactNumber":{"type":"string","store":true},"name":{"type":"string"}}}}}}
1
What is the mapping for contactNumber? - Andrei Stefan
@AndreiStefan edited to add the mapping for contact number - wolfgang
contactNumber should have an analyzer that doesn't change its content, for example keyword or it should be not_analyzed. - Andrei Stefan

1 Answers

3
votes

term filter doesn't analyze the input text, meaning if you search for "+1-415-392-3702" then this is the exact text it's expecting to find in the index.

But, your field being just the default string this means it most probably uses the standard analyzer for analyzing it. This means that in the index you'll have these tokens: "1","3702","392","415" and not a single +1-415-392-3702.

So, you need either one of these two:

    "contactNumber": {
      "type": "string",
      "index": "not_analyzed",
      "store":true
    }

or

    "contactNumber": {
      "type": "string",
      "analyzer": "keyword",
      "store":true
    }