Query_string query with asterisk and escaped char not working

Hi everyone!

Could you please help me on how to use query_string with * and escaped characters?

For example:

Mapping:

{
  "index": "test_user_name",
  "mappings": {
    "properties": {
      "userId": {
        "type": "keyword"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

Record that is stored in index:

{"userId": "1", "name": "AliceV!ABC"}

Query:

{
    "query": {
        "query_string": {
            "query": "*Alice* AND *V\\!*",
            "default_field": "name"
        }
    }
}

Expected result: stored record is returned.
Actual result: record is not returned.

Note: without special chars everything works correct.

Following query (without escaped char returns expected result):

{
    "query": {
        "query_string": {
            "query": "*Alice* AND *V*",
            "default_field": "name"
        }
    }
}

Thanks in advance, Nick.

Hi @Nick_Lipnyagov

When you do not define the analyzer, elastic defaults to "standard" and this analyzer removes the characters like "!".

You can use the whitespace analyzer and get results with the query you are trying to run.

{
  "mappings": {
    "properties": {
      "userId": {
        "type": "keyword"
      },
      "name": {
        "analyzer": "whitespace",
        "type": "text"
      }
    }
  }
}
1 Like

Thanks, now it's much clearer how it works!