Elastic search query not resulting as expected

Viewed 38

Having elastic search query as below -

{
  "query": {
    "bool": {
      "must": {
        "query": "*Hanry Oliver 45354*",
        "fields": [
          "firstName",
          "lastName",
          "customerNo"
        ],
        "default_operator": "AND"
      }
    }
  }
}

This results in one record which is correct. But if uses -

   "query":{
      "bool":{
         "must":{
            "query":"*Han Ol 453*",
            "fields":[
               "firstName",
               "lastName",
               "customerNo"
            ],
            "default_operator":"AND"
         }
      }
   }
}

It results in no records, whereas it should match "Hanry Oliver 45354". If and operator is removed, it results in all records having match han or ol or 453 whereas looking for query result having and- han and Ol and 453

Could someone help what query should be used in this case ?

1 Answers

When you are using "query":"*Han Ol 453*", then the query string searches for *Han AND OlAND 453*

  1. *Han --> will get no result
  2. Ol --> will get no result
  3. 453* --> will get the result

Due to 453* you are getting your result with the OR operator and not with the AND operator.


You need to modify your search query as

    {
  "query": {
    "query_string": {
      "query": "Han* Ol* 453*",     // note this
      "fields": [
        "firstName",
        "lastName",
        "customerNo"
      ],
      "default_operator": "and",
      "analyze_wildcard":true
    }
  }
}

Search Result will be

"hits": [
      {
        "_index": "67140182",
        "_type": "_doc",
        "_id": "1",
        "_score": 3.0,
        "_source": {
          "firstName": "Hanry",
          "lastName": "Oliver",
          "customerNo": "45354"
        }
      }
    ]
Related