Search code examples
elasticsearch

Elastic Search Wildcards With Whitespace issue


Prepared an ES query to find all records, which contain my substring (basically LIKE in SQL):

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "companyName": {
              "value": "*test exactly*"
            }
          } 
        }
      ]
    }
  }
}

It doesn't return records with companyName "Profile Test exactly" or f.e. "Subtest exactly". How can I resolve this issue?

Tried the same with regex queries, but it didn't help.


Solution

  • You can try using regexp like this

    {"regexp": {"companyName.keyword: {"value": ".*test exactly.*", "case_insensitive": True, "max_determinized_states": 10000}}}
    

    In case you have .keyword field for companyName than only that will work to search texts with whitespaces.
    If you don't have companyName.keyword field and the whitespace searching is not working, than you need to update your index mapping to get this field.

    PS: When a new field is added to index, ES automatically creates a .keyword field for it too (if you don't specify mapping for that field yourself).

    The below image is Elasticsearch Tools visualization of fields of an index

    enter image description here