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

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?


Todd DeSantis


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

  1. Use the official Elasticsearch Javascript client library. For your specific use case, take a look at the msearch example.
  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)