Elastic Search wildcard search with spaces

user1530318 picture user1530318 · May 8, 2015 · Viewed 10.8k times · Source

I have the following query. I'm trying to find values of 'hello world', but it returns zero results. However, when value = 'hello*', it does give me that expected result. Any idea how I can change my query to give me that hello world result? I've tried *hello world*, but for some reason it just won't search anything with spaces.

I think it has something to do with the spaces as when I try to search "* *", it gives me no results. But I know I have many values in there with spaces. Any ideas would help!

 {
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "variant": [
                "collection"
              ]
            }
          }
        ]
      },
      "query": {
        "wildcard": {
          "name": {
            "value": "hello world"
          }
        }
      }
    }
  }
}

Answer

Prabin Meitei picture Prabin Meitei · May 8, 2015

What is the mapping you have used for your field name? If you have not defined any mapping or you have just defined the type as string (without any analyzer) then the field will be analyzed using the standard analyzer. This will create tokens as "hello" and "world" separately. This means wildcard query will work for something like *ell* or *wor* but not with spaces.

You have to change your mapping to have the field "name" as not_analyzed then wildcard searches with space will work.

A word of caution: Wildcard searches are heavy. If you want to do partial matching search (equivalent of %like%) You can use ngram token filter in your analyzer and do term search. It will take care of matching partial string and have better performance too.