Bony Thomas
Bony Thomas

Reputation: 13

Elastic search: Like query not working in string contains &(ampersand)?

I have a query which is not working. I think the searched value containing & (ampersand) symbol. I am talking about the following query.

GET "path"_search
{

  "from": 0,
  "size": 10000,
  "query": {
    "query_string": {
      "query": "(Department_3037.Department_3037_analyzed:*P&C*)"
    }    
  }
}

Why this query is not working and how to overcome this issue or i need like query of string containing & like (p&c) ,l&t etc... Let me know how this can be fixed.. Thank you.

Upvotes: 1

Views: 1072

Answers (1)

Richa
Richa

Reputation: 7649

Since the field you are searching in is analyzed. If the field contains the text (Department_3037.Department_3037_analyzed:*P&C*) , this will be tokenized as : Department_3037 , Department_3037_analyzed , P , C.

You can get this using :

curl -XGET "http://localhost:9200/_analyze?tokenizer=standard" -d "(Department_3037.Department_3037_analyzed:*P&C*)"

You will get the tokens as follows:

{"tokens":[{"token":"Department_3037","start_offset":1,"end_offset":16,"type":"<ALPHANUM>","position":1},

{"token":"Department_3037_analyzed","start_offset":17,"end_offset":41,"type":"<ALPHANUM>","position":2},

{"token":"P","start_offset":43,"end_offset":44,"type":"<ALPHANUM>","position":3},

{"token":"C","start_offset":45,"end_offset":46,"type":"<ALPHANUM>","position":4}]}.

If you want to retrieve the documents you will have to escape the special characters :

{
"query": {
   "query_string": {
      "default_field": "name",
      "query": "\\(Department_3037\\.Department_3037_analyzed\\:\\*P\\&C\\*\\)"
    }
   }
 }

Hope it helps.

Upvotes: 2

Related Questions