marcg
marcg

Reputation: 658

How can I include query string in elastic search response

front end is sending async calls to Elastic server. To match the responses, I would like to add the query string in the elastic response json. Does Elastic search have any option to include the query string in the response ?

Upvotes: 2

Views: 937

Answers (2)

jaspreet chahal
jaspreet chahal

Reputation: 9099

You can use named queries. You can assign a name to each query which will appear in the result

Query:

{
  "query": {
    "query_string": {
      "query": "this OR thus",
      "_name":"query1"
    }
  }
}

Result:

"hits" : [
      {
        "_index" : "index50",
        "_type" : "_doc",
        "_id" : "VDBsK3IBpnSikKlzkKY3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "this"
        },
        "matched_queries" : [
          "query1"    ---> name passed in query
        ]
      }
    ]

To search in url:

localhost:9200/_search?pretty&source={"query": {"query_string": {
      "query": "this OR thus", "_name":"query1"} }}
&source_content_type=application/json

Upvotes: 2

nilleb
nilleb

Reputation: 1012

The documentation https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html doesn’t mention it anyway.

I think that such an option does NOT exist since several types of query exist and for most of them there is more than a query string.

Upvotes: 0

Related Questions