Two ways to construct queries in elastic-builder
masterYou can build Elasticsearch queries using two distinct patterns:
- Class Constructors: Use the
newkeyword with specific class names (e.g.,new esb.RequestBodySearch()). - Helper Methods: Use lowercase helper functions (e.g.,
esb.requestBodySearch()) which construct the objects without requiring thenewkeyword.
Both methods result in an object that can be converted to JSON via .toJSON().
// Pattern 1: Class Constructors
const requestBody = new esb.RequestBodySearch()
.query(new esb.MatchQuery('message', 'this is a test'));
// Pattern 2: Helper Methods
const requestBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'this is a test'));