ElasticQuery
ElasticQuery is an easy to use query builder for the rather verbose ElasticSearch DSL. It covers a large portion of the ES DSL, but does not currently have full coverage — pull requests welcome!
Installation
yarn
yarn add @shnhrrsn/elasticquery
npm
npm install --save @shnhrrsn/elasticquery
Documentation
TODO! In the mean time, check out some examples below, the tests and source code.
Examples
Create a Connection
const { ElasticSearch } = require('@shnhrrsn/elasticquery')
const search = new ElasticSearch({
host: 'localhost:9200'
index: 'locations'
})
Conditions
const results = await search.query('doc').where('country', 'US')
const results = await search.query('doc').whereNot('country', 'US')
const results = await search.query('doc').whereIn('country', [ 'US', 'CA' ])
const query = search.query('doc')
query.distance('location', '5mi', '40.782420', '-73.965600')
query.orDistance('location', '10mi', '40.782420', '-73.965600')
const query = search.query('doc')
query.range('established', '1700', '1800')
Aggregating
await search.query('doc').aggregate(agg => agg.terms('field'))
search.query('doc').aggregate(agg => {
agg.terms('field', term => {
term.aggregate(query => {
query.sum('nested.sum1', sum => sum.as('sum1'))
query.sum('sum2')
})
})
})
search.query('doc').aggregate(aggregate => {
aggregate.filter(filter => {
filter.where('abc', 123)
})
aggregate.dateHistogram('field', histogram => {
histogram.interval('day')
histogram.timezone('-05:00')
histogram.bounds({ min: 'now-30d/d', max: 'now/d' })
histogram.minDocCount(0)
histogram.size(10)
})
})
License
ElasticQuery
was created by Shaun Harrison and is made available under the MIT license.