# Msearch: How to build the request header/body in javascript?

**URL:** <https://discuss.elastic.co/t/msearch-how-to-build-the-request-header-body-in-javascript/42551>\
**Category:** Elasticsearch\
**Created:** [February 24, 2016, 4:43am UTC](https://discuss.elastic.co/t/msearch-how-to-build-the-request-header-body-in-javascript/42551 "2016-02-24T04:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hifitodd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hifitodd/32/8015_2.png) [@hifitodd](https://discuss.elastic.co/u/hifitodd)\
**Post date:** [February 24, 2016, 4:43am UTC](https://discuss.elastic.co/t/msearch-how-to-build-the-request-header-body-in-javascript/42551/1 "2016-02-24T04:43:06Z")

</div>

Hi there,

I'm building an ecommerce page with elasticsearch, where I will have the main product results and also a "featured products" section.

I want to control the logic over trhe "featured products" section independantly from the main results, so I was thinking that packing it all in one request with the '\_msearch' API would be the way to go.

I'm struggling with figuring out how to build my query. It seems the examples are reading input from a file instead of creating it programmatically.

How would I do it in JS? My super noob idea (which is def. not working) is this:

trying to make this query:

{}  
{"query" : {"match\_all" : {}}}

My attempt:

```
    let query = "{}\n";
    query += '{"query" : {"match_all" : {}}}\n';

```

Any ideas how I would built out the query body in Javascript?

thanks,

Todd DeSantis

> **[AudioFlea - The New Hifi Marketplace](https://audioflea.com/)**
>
> Find more, easier. List items, faster. Sell more, cheaper.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [March 1, 2016, 8:19am UTC](https://discuss.elastic.co/t/msearch-how-to-build-the-request-header-body-in-javascript/42551/2 "2016-03-01T08:19:14Z")

</div>

Hi,

I have no real experience in Javascript, but I think you could do two things:

1. Use the [official Elasticsearch Javascript client library](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html). For your specific use case, take a look at the [msearch example](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-2-2.html#api-msearch-2-2).
2. If you don't want to use it, can't you just create an array of JSON objects representing your queries? (similar to the [msearch example](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-2-2.html#api-msearch-2-2))

Daniel

---

<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:** [July 5, 2017, 11:12pm UTC](https://discuss.elastic.co/t/msearch-how-to-build-the-request-header-body-in-javascript/42551/3 "2017-07-05T23:12:38Z")

</div>


