wolfgang
wolfgang

Reputation: 7819

Elastic search term filter is not working

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"}}}}}}

Upvotes: 2

Views: 2572

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52366

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
    }

Upvotes: 5

Related Questions