Search code examples
elasticsearchkibana

Why does elastic search wildcard query return no results?


Query #1 in Kibana returns results, however Query #2 returns no results. I search for only "bob" and get results, but when searching for "bob smith", no results, even though "Bob Smith" exists in the index. Any reason why?

Query #1: returns results

GET people/_search
{
"query": {
    "wildcard" : {
        "name" : "*bob*"
    }
}

}


Results:

{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
 },
 "hits" : {
   "total" : {
     "value" : 23,
    "relation" : "eq"
   },
   "max_score" : 1.0,
  "hits" : [
  {
    "_index" : "people",
    "_type" : "_doc",
    "_id" : "xxxxx",
    "_score" : 1.0,
    "_source" : {
      "name" : "Bob Smith",
     
      ...

Query #2: returns nothing.. why(?)

GET people/_search
{
 "query": {
    "wildcard" : {
        "name" : "*bob* *smith*"
    }
 }

 }

results...nothing

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
  "total" : 2,
  "successful" : 2,
  "skipped" : 0,
  "failed" : 0
  },
   "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
     "max_score" : null,
     "hits" : [ ]
   }
 }

Solution

  • Look like the reason of the empty result is your index mapping. If you use "text" type field, you actually search in the inverted index, mean you search in the token "bob" and token "smith" (standard analyzer) and not in the "Bob Smith". If you want to search in "Bob Smith" as one token, you need to use "keyword" type (maybe with lowercase normalizer, if you want to use not key sensetive search)

    For example:

    PUT test
    {
      "settings": {
        "analysis": {
          "normalizer": {
            "lowercase_normalizer": {
              "type": "custom",
              "char_filter": [],
              "filter": [
                "lowercase",
                "asciifolding"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "lowercase_normalizer"
          }
        }
      }
    }
    
    PUT test/_doc/1
    {
      "name" : "Bob Smith"
    }
    
    GET test/_search
    {
      "query": {
        "wildcard": {
          "name": "*bob* *Smith*"
        }
      }
    }