Ayush Kumar
Ayush Kumar

Reputation: 45

Not able to write the wildcard query for ElasticSearch?

I am facing an issue using the wildcard. Whenever I try to find a data using wildcard, it is returning an error

Data Set looks like the following in ES:

   "hits": {
      "total": 1000,
      "max_score": 1,
      "hits": [
         {
            "_index": "accounts",
            "_type": "data",
            "_id": "25",
            "_score": 1,
            "_source": {
               "account_number": 25,
               "balance": 40540,
               "firstname": "Virginia",
               "lastname": "Ayala",
               "age": 39,
               "gender": "F",
               "address": "171 Putnam Avenue",
               "employer": "Filodyne",
               "email": "[email protected]",
               "city": "Nicholson",
               "state": "PA"
            }
         }

And I use the below query:

GET /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "match":{
            "wildcard":{"lastname" : "Aya*" }
            }
          }      
        }
      }
    }
    }
}

But it's throwing the following error:

{
   "error": {
      "root_cause": [
         {
            "type": "query_parsing_exception",
            "reason": "[match] query does not support [lastname]",
            "index": "accounts",
            "line": 9,
            "col": 25
         }

I have tried without the match instance using only the wildcard in the query , but still I cannot get the data. The search here is successful but the data is not returned.

Without Match Query:

GET /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "wildcard":{"lastname" : "Aya*" }
          }      
        }
      }
    }
    }
}

Please help me understand how I should device the query. I have to use this query string in JAVA API as well. So, any advice on that front would also be very helpful.

Upvotes: 3

Views: 5177

Answers (2)

tyler.frankenstein
tyler.frankenstein

Reputation: 2344

This worked for me:

GET /_search
{
  "query": {
    "wildcard": {
      "lastname": "aya*"
    }
  }
}

I had wasted many hours until I read this online:

...if you search a not_analyzed field and use wildcards, you MUST NOT include any capital letters in your query...

The whole time it was not working because I was searching for e.g. Aya*. Soon as I converted it to lower case aya* it worked.

Upvotes: 10

Val
Val

Reputation: 217254

I'm guessing your lastname field is probably analyzed and thus the token that was indexed is ayala and not Ayala. So since wildcard queries are not analyzed you need to specify the search term in lowercase:

Try this:

POST /_search
{
    "query":{       
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" :
          {
            "wildcard":{"lastname" : "aya*" }
          }      
        }
      }
    }
    }
}

Upvotes: 1

Related Questions