Elasticsearch Escaping special characters in regexp

Viewed 825

i want to search in elasticsearch using regexp but i must to escape caractere speciaux in this phrase " http://10.10.100.100/test.txt " I tried :

GET indexname/_search
{
  "query": {
    "nested": {
      "path": "test",
      "query": {
        "bool": {
          "must": [
            { 
              "regexp": { 
                "test.search1": "http://10.10.100.100.*" 
              }
            },
            { 
              "match": { 
                "test.search2": "url" 
              }
            }
          ]
        }
      }
    }
  }
}

not working.

when i replace "http://10.10.100.100." with "http." it's working so i need to escape ":" and "//"

how to do that with elasticsearch

1 Answers

Elastic search has reserved few special characters. They are,

+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

Inorder to use those characters in the query, you need to escape them with \. In your case you should query with this string

"test.search1": "http\:\/\/10.10.100.100.*"

Instead of this

"test.search1": "http://10.10.100.100.*" 

For more information you can refer the docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters

Related