# Msearch Map request and response

**URL:** <https://discuss.elastic.co/t/msearch-map-request-and-response/126459>\
**Category:** Elasticsearch\
**Created:** [April 2, 2018, 7:16pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459 "2018-04-02T19:16:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravind/32/45644_2.png) [@Aravind](https://discuss.elastic.co/u/Aravind)\
**Post date:** [April 2, 2018, 7:16pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/1 "2018-04-02T19:16:39Z")

</div>

Hi

I'm exploring multi search capability to see if it can cater to our requirement. My requirement simply is to perform single bulk search, rather than making series of individual calls. Once we get the response payload, we would want to map the response to the individual search queries. This is required for further processing.

Example illustration

```
curl -H 'Content-Type: application/x-ndjson' -XGET 'localhost:9200/products/_msearch?pretty=true' --data-binary @"multiSearch.q" ; echo

```

**multiSearch.q: Content**  
{ "query": { "match": { "product": "Jeans" } } }  
{ "query": { "match": { "product": "Men Shoes" } } }

This is the response we get :

```
{
  "responses" : [
	{
	  "took" : 4,
	  "timed_out" : false,
	  "_shards" : {
		"total" : 5,
		"successful" : 5,
		"skipped" : 0,
		"failed" : 0
	  },
	  "hits" : {
		"total" : 1,
		"max_score" : 11.156035,
		"hits" : [
		  {
			"_index" : "product",
			"_type" : "coverage",
			"_id" : "TWlKeGIBtL1jpnPeSMhq",
			"_score" : 11.156035,
			"_source" : {
			  "product_id" : "100",
			  "name" : "Jeans",
			  "available" : "Y"
			}
		  }
		]
	  },
	  "status" : 200
	},
	{
	  "took" : 2,
	  "timed_out" : false,
	  "_shards" : {
		"total" : 5,
		"successful" : 5,
		"skipped" : 0,
		"failed" : 0
	  },
	  "hits" : {
		"total" : 1,
		"max_score" : 12.604036,
		"hits" : [
		  {
			"_index" : "product",
			"_type" : "coverage",
			"_id" : "8WZJeGIBtL1jpnPe8lR2",
			"_score" : 12.604036,
			"_source" : {
			  "product_id" : "200",
			  "name" : "Men Shoes",
			  "available" : "N"
			}
		  }
		]
	  },
	  "status" : 200
	}
  ]
}

```

My question is how to we identify the response and match it to the request.

**Option 1: Based on ordering**

One way to look at this is based on the ordering of the request-search and response. Is it guaranteed that the response will always be in the same order as that of the request. Meaning in the example above since the request had search for "Jeans" above "Men Shoes", can we assume that the response output will always be "Search-output-for-Jeans" before "Search-output-for-Men Shoes".

**Option 2: Tagging each search request**

Is it possible to search each search request, such that the tag is passed over in the response. This would help to map the search request to the responses.

Something like this : Note the string "query-name" in the request and response

{ **"query-name" : "Search1"** , "query": { "match": { "product": "Jeans" } } }  
{ **"query-name" : "Search2"** , "query": { "match": { "product": "Men Shoes" } } }

And the response carries the search name which would help us to map -

```
{
  "responses" : [
	{
	  "query-name" : "Search1",
	  "took" : 4,
	  "timed_out" : false,
	  "_shards" : {
		"total" : 5,
		"successful" : 5,
		"skipped" : 0,
		"failed" : 0
	  },
	  "hits" : {
		"total" : 1,
		"max_score" : 11.156035,
		"hits" : [
		  {
			"_index" : "product",
			"_type" : "coverage",
			"_id" : "TWlKeGIBtL1jpnPeSMhq",
			"_score" : 11.156035,
			"_source" : {
			  "product_id" : "100",
			  "name" : "Jeans",
			  "available" : "Y"
			}
		  }
		]
	  },
	  "status" : 200
	},
	{
	  "query-name" : "Search2",		
	  "took" : 2,
	  "timed_out" : false,
	  "_shards" : {
		"total" : 5,
		"successful" : 5,
		"skipped" : 0,
		"failed" : 0
	  },
	  "hits" : {
		"total" : 1,
		"max_score" : 12.604036,
		"hits" : [
		  {
			"_index" : "product",
			"_type" : "coverage",
			"_id" : "8WZJeGIBtL1jpnPe8lR2",
			"_score" : 12.604036,
			"_source" : {
			  "product_id" : "200",
			  "name" : "Men Shoes",
			  "available" : "N"
			}
		  }
		]
	  },
	  "status" : 200
	}
  ]
}

```

Apart from these, are there any other ideas ?

Thanks

---

<div class="post-metadata">

**Author:** ![Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravind/32/45644_2.png) [@Aravind](https://discuss.elastic.co/u/Aravind)\
**Post date:** [April 3, 2018, 4:08pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/2 "2018-04-03T16:08:34Z")

</div>

Appreciate any inputs on this.

---

<div class="post-metadata">

**Author:** ![Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravind/32/45644_2.png) [@Aravind](https://discuss.elastic.co/u/Aravind)\
**Post date:** [April 10, 2018, 11:12pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/3 "2018-04-10T23:12:33Z")

</div>

Once last try before I give up.

What I'm asking for is similar to what is possible with percolate where name is provided for the search.

```
GET /my-index/_search
{
    "query" : {
        "bool" : {
            "should" : [
                {
                    "percolate" : {
                        "field" : "query",
                        "document" : {
                            "message" : "bonsai tree"
                        },
                        "name": "query1" 
                    }
                },
                {
                    "percolate" : {
                        "field" : "query",
                        "document" : {
                            "message" : "tulip flower"
                        },
                        "name": "query2" 
                    }
                }
            ]
        }
    }
}

```

Documented here : [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html#\_specifying\_multiple\_percolate\_queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html#_specifying_multiple_percolate_queries)

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 11, 2018, 1:41pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/4 "2018-04-11T13:41:45Z")

</div>

[The documentation for the multi search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html) specifies that: "_The response returns a responses array, which includes the search response and status code for each search request **matching its order in the original multi search request.** _"

So yes, going back to your original question: the ordering in the response is guaranteed to be the same as in the request.

As far as I know there is no way to name the individual queries in the request.

---

<div class="post-metadata">

**Author:** ![Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravind/32/45644_2.png) [@Aravind](https://discuss.elastic.co/u/Aravind)\
**Post date:** [April 12, 2018, 4:28pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/5 "2018-04-12T16:28:14Z")

</div>

Thanks Abdon.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 13, 2018, 7:31am UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/6 "2018-04-13T07:31:09Z")

</div>

I just figured out that it is actually possible to name queries, if you would still wish to do so, using [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html). Each filter and query can accept a `_name` that is returned with the search results as `matched_queries`. For example, your `_msearch` would look like this:

```auto
GET /_msearch
{}
{"query":{"match":{"product":{"_name":"Query for jeans","query":"Jeans"}}}}
{}
{"query":{"match":{"product":{"_name":"Query for shoes","query":"Men Shoes"}}}}

```

---

<div class="post-metadata">

**Author:** ![Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aravind/32/45644_2.png) [@Aravind](https://discuss.elastic.co/u/Aravind)\
**Post date:** [April 13, 2018, 10:51pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/7 "2018-04-13T22:51:26Z")

</div>

Oh this is really great. Thanks Abdon.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2018, 10:51pm UTC](https://discuss.elastic.co/t/msearch-map-request-and-response/126459/8 "2018-05-11T22:51:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
