Wildcard search issue with long datatype in elasticsearch

Viewed 556
   {
   "from":0,
   "query":{
      "bool":{
         "must":[
            {
               "query_string":{
                  "query":"10*",
                  "lenient":true,
                  "fields":[
                     "phoneNumber"
                  ],
                  "escape":true
               }
            }
         ]
      }
   }
}

Here the phonenumber is of type long and we want to perform wildcard search on it. I am passing the query as 10* ( "query":"10*",) Actual hits should have been 15 but I am getting 0 hits.

If I am doing the same for a field with type keyword(string) Address, with the query "query":"newyork*", I am getting the result.

Does anyone have any idea why we didn't get the hits for fields with type long?

1 Answers

It's not directly possible to perform wildcards on numeric data types. It is better to convert those integers to strings.

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "phoneNumber": {
        "type": "text"
      }
    }
  }
}

Index Data:

{
  "phoneNumber": "101"
}

Search Query:

{
  "query": {
    "bool": {
      "filter": [
        {
          "query_string": {
            "query": "10*",
            "fields": [
              "phoneNumber"
            ]
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65109764",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "phoneNumber": "101"
        }
      }
    ]

If you want to do a partial match, then you can even go with edge n-gram tokenizer

Related