To search something in Elasticsearch, the request looks like this:
The query definition is sent as JSON in the body of the request. Here’s an example search body:
This script shows how we might build the search body for the previous request.
In the script, we’re building the query line-by-line and making use of ES2015’s shorthand property names to link it all together.
I usually find I work backwards from the es.search() statement. First are the params – defining the index and options for the request. Then onto the request body, which itself unravels into its constituent parts.
This technique can be used in Node JS or modern browsers – wherever ES2015 is supported!