![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
elastic-builder
Advanced tools
Readme
A Node.js implementation of the Elasticsearch DSL for use with the official elasticsearch javascript client with builder syntax.
Check out the API reference documentation.
elastic-builder
was built for 5.x query DSL. However, the library should be usable with
2.x as well. For older versions of the DSL, you can try
elastic.js
or bodybuilder
npm install elastic-builder --save
const bob = require('elastic-builder');
const requestBody = bob.requestBodySearch()
.query(bob.matchQuery('message', 'this is a test'));
// OR
const requestBody = new bob.RequestBodySearch()
.query(new bob.MatchQuery('message', 'this is a test'));
// bob.prettyPrint(requestBody)
// {
// "query": {
// "match": {
// "message": "this is a test"
// }
// }
// }
// requestBody.toJSON()
// {
// "query": {
// "match": {
// "message": "this is a test"
// }
// }
// }
For each class, MyClass
, a utility function myClass
has been provided which
contructs the object for us without the need for new
keyword.
Try it out on the command line using the node REPL:
# Start the repl
node ./node_modules/elastic-builder/repl.js
# The builder is available in the context variable bob
elastic-builder > bob.prettyPrint(bob.requestBodySearch().query(bob.matchQuery('message', 'this is a test')));
Elasticsearch only provides a low level client for making requests.
elastic.js
was a relatively popular library for building the request search body.
However, this project is not being maintained nor is the fork.
There were several changes
in the 5.0 release which make the older libraries unusable.
This library is a port of elastic.js
to es6 with elasticsearch 5.3 compatibility.
API reference can be accessed here - http://elastic-builder.js.org/docs.
API documentation was generated using documentation.js It is being hosted with help from this awesome project - https://github.com/js-org/dns.js.org
Documentation is a WIP. See roadmap.
// Bool query
const requestBody = bob.requestBodySearch()
.query(
bob.boolQuery()
.must(bob.matchQuery('last_name', 'smith'))
.filter(bob.rangeQuery('age').gt(30))
)
// requestBody.toJSON()
// {
// "query" : {
// "bool" : {
// "must" : {
// "match" : {
// "last_name" : "smith"
// }
// },
// "filter" : {
// "range" : {
// "age" : { "gt" : 30 }
// }
// }
// }
// }
// }
// Multi Match Query
const requestBody = bob.requestBodySearch()
.query(
bob.multiMatchQuery(['title', 'body'], 'Quick brown fox')
.type('best_fields')
.tieBreaker(0.3)
.minimumShouldMatch('30%')
);
// requestBody.toJSON()
// {
// "multi_match": {
// "query": "Quick brown fox",
// "type": "best_fields",
// "fields": [ "title", "body" ],
// "tie_breaker": 0.3,
// "minimum_should_match": "30%"
// }
// }
// Aggregation
const requestBody = bob.requestBodySearch()
.size(0)
.agg(bob.termsAggregation('popular_colors', 'color'));
// requestBody.toJSON()
// {
// "size" : 0,
// "aggs" : {
// "popular_colors" : {
// "terms" : {
// "field" : "color"
// }
// }
// }
// }
// Nested Aggregation
const requestBody = bob.requestBodySearch()
.size(0)
.agg(
bob.termsAggregation('colors', 'color')
.agg(bob.avgAggregation('avg_price', 'price'))
.agg(bob.termsAggregation('make', 'make'))
);
// requestBody.toJSON()
// {
// "size" : 0,
// "aggs": {
// "colors": {
// "terms": {
// "field": "color"
// },
// "aggs": {
// "avg_price": {
// "avg": {
// "field": "price"
// }
// },
// "make": {
// "terms": {
// "field": "make"
// }
// }
// }
// }
// }
// }
// Sort
const requestBody = bob.requestBodySearch()
.query(
bob.boolQuery()
.filter(bob.termQuery('message', 'test'))
)
.sort(bob.sort('timestamp', 'desc'))
.sorts([
bob.sort('channel', 'desc'),
bob.sort('categories', 'desc'),
// The order defaults to desc when sorting on the _score,
// and defaults to asc when sorting on anything else.
bob.sort('content'),
bob.sort('price').order('desc').mode('avg')
]);
// requestBody.toJSON()
// {
// "query": {
// "bool": {
// "filter": {
// "term": { "message": "test" }
// }
// }
// },
// "sort": [
// { "timestamp": { "order": "desc" } },
// { "channel": { "order": "desc" } },
// { "categories": { "order": "desc" } },
// "content",
// { "price": { "order": "desc", "mode": "avg" } }
// ]
// }
// From / size
const requestBody = bob.requestBodySearch()
.query(bob.matchAllQuery())
.size(5)
.from(10);
// requestBody.toJSON()
// {
// "query": {
// "match_all": {}
// },
// "size": 5,
// "from": 10
// }
elastic-builder
provides lightweight validation where ever possible:
$ node repl.js
elastic-builder > bob.multiMatchQuery().field('title').field('body').query('Quick brown fox').type('bwst_fields')
See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
Got 'type' - bwst_fields
Error: The 'type' parameter should belong to Set {
'best_fields',
'most_fields',
'cross_fields',
'phrase',
'phrase_prefix' }
at MultiMatchQuery.type (E:\Projects\repos\elastic-builder\lib\queries\full-text-queries\multi-match-query.js:134:23)
at repl:1:77
at ContextifyScript.Script.runInContext (vm.js:35:29)
at REPLServer.defaultEval (repl.js:342:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:538:10)
at emitOne (events.js:96:13)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:239:10)
Tests are being added. See roadmap.
For running whatever tests have been added:
npm test
elastic-builder
is heavily inspired by elastic.js
and the fork by Erwan Pigneul.
bodybuilder for documentation style, build setup, demo page.
MIT
FAQs
A JavaScript implementation of the elasticsearch Query DSL
The npm package elastic-builder receives a total of 74,611 weekly downloads. As such, elastic-builder popularity was classified as popular.
We found that elastic-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.