escape special character in elasticsearch query

13,007

You get the error because there is no need to escape the '@' character.
"query": "@as" should work.
You should check your mappings as well, if your fields are not marked as not_analyzed(or don't have keyword analyzer) you won't see any search results - standard analyzer removes characters like '@' when indexing a document.
UPDATE
query_string uses _all field by default, so you have to configure this field in the way similar to this example:

PUT index 
{
   "mappings":{
      "book":{
         "_all":{
            "type":"string",
            "index":"analyzed",
            "analyzer":"whitespace"
         },
         "properties":{
            "name":{
               "type":"string",
               "index":"not_analyzed"
            }
         }
      }
   }
}

PUT /index/book/1
{
"name" : "@foo bar"
}

GET index/_search
{
  "size": 20,
  "query": {
    "query_string": {
      "query": "@foo",
      "analyzer": "keyword"
    }
  }
}
Share:
13,007
Tamizharasan
Author by

Tamizharasan

Updated on June 14, 2022

Comments

  • Tamizharasan
    Tamizharasan almost 2 years

    My question is simple, I can't use @ in the search query. Finally, I found that I can escape the special characters using the backslash.

    GET index/_search
    {
      "size": 20,
      "query": {
        "query_string": {
          "query": "\@as",
          "analyzer": "keyword"
        }
      }
    }
    

    But when I try to do that I got the following error Unrecognized character escape '@' (code 64)\n at. And when I try without @ symbol i got the results without @ symbol like

    1. As missed as ever
    2. As. GUI
    3. As if And so on. can any one suggest how can I achieve the previous query can be executed as per my expectation?

    I am using

    • Elasticsearch 2.4.1
    • Ubuntu 14.04
    • Kibana 4.6.1