Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search with special characters in elasticsearch

I am not able to do a search in a Elasticsearch with character ending/starting with special character. like "123456!"

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "123456!"
    }
 }
}

My mappings are

{
  "mappings": {
    "passwords": { 
      "properties": {
        "date":    { "type": "date"}, 
        "password":    { "type": "string","index": "not_analyzed"}, 

      }
    }
  }
}

It is giving me error, what can I do in my search query (or in the mapping), so that special characters will be treated as part of search string ?

like image 446
Rudra Avatar asked Jun 08 '16 12:06

Rudra


2 Answers

Since your password field is not_analyzed (good!), try to do an exact match by surrounding 123456! with double quotes:

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "\"123456!\""
    }
 }
}

Another way of doing this is to set the keyword analyzer in your query_string query (but make sure to escape the ! because it's a reserved character (for the NOT operator)

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "123456\!",
        "analyzer": "keyword"
    }
 }
}
like image 58
Val Avatar answered Sep 18 '22 15:09

Val


In addition to Vals answer, one could also escape the query using the escape parameter:

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "123456!",
        "analyzer": "keyword",
        "escape": true
    }
  }
}
like image 45
krizajb Avatar answered Sep 19 '22 15:09

krizajb