Search code examples
elasticsearchkibanaelastic-stack

Forming Elastic search query with extra quotation marks


I am trying to mimic Kibana's search query via Elasticsearch's query string. For e.g. in Kibana I can search like this with the quotation marks:

"ABC" AND "CDE"

When I try to create this query string it throws a syntax error:

{
"query": {
"query_string": {
"fields": ["messages"]
"query": ""ABC" AND "CDE"" (syntax error occurs here)
}
}
}

I know I can just query it without the extra quotation marks but I need to append user's input to the query string which will contain those extra quotation marks. How can I form this query so that it can accept the quotation marks?


Solution

  • you can either escape the quotes in your query field

    {
      "query": {
        "query_string": {
          "fields": ["messages"],
          "query": "\"ABC\" AND \"CDE\""
        }
      }
    }
    

    or wrap the query value in triple quotes

    {
      "query": {
        "query_string": {
          "fields": ["messages"],
          "query": """
            "ABC" AND "CDE"
          """
        }
      }
    }
    

    (your example was also missing a comma between the fields and query field)