elastic-search-builder working with elasticsearch.js
See more use case in the docs as well as in the tests.
(option | body | build)
:
see below.
Usage
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
host: 'localhost:9200'
});
const esb = require('elastic-search-builder');
const searchparams = esb()
.option()
.indices(['2016.01.01'])
.body()
.query({
match: {
message: 'hello world'
}
})
.aggs()
.build();
client.search(searchparams).then(body => {
console.log(body)
})
Add custom option
(Object)
option body.
(indices | type)
:
see below
//build option
esb().option({
index: 'logs',
type: '2016.01.01'
}).build()
Add custom body
(Object)
option body.
(query | aggs | fields | size | from)
:
see below.
esb().body({
query: {
match: {
message: 'hello'
}
}
})
.build()
Build with options and body.
Object
:
search option.
Add index field to option.
(boolean)
Whether specified concrete indices should be ignored when unavailable (missing or closed)
(boolean)
Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes _all string or when no indices have been specified)
esb()
.options()
.indices(['2016.01.01'], true, true)
.build()
esb()
.options()
.indices('2016.01.01,2016.02.02', false, false)
.build()
Add type field to option
(...any)
esb()
.options()
.type(['company', 'school'], 'employee', 'student')
.build()
Add query clause to query body
(Object
= {}
)
any query clause
bool
:
see below.
//build body
esb()
.body()
.query({
match: {
message: 'hello'
}
})
.build()
//result:
{
body: {
query: {
match: {
message: 'hello'
}
}
}
}
Add aggregation to aggs body
(Object
= {}
)
any aggregation body
(appendAggs | subAggs | forkAggs | mergeAggs)
:
see below.
esb()
.body()
.aggs({
message: {
terms: {
field: 'message'
}
}
})
.build();
//result:
{
body: {
aggs: {
message: {
terms: {
field: 'message'
}
}
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-field-caps.html
(...any)
esb()
.body()
.fields('rating', 'count')
.build()
//result:
{
body: {
fields: ['rating', 'count']
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html
(number
= 0
)
esb()
.size(10)
.build()
//result:
{
body: {
size: 10
}
}
(string)
(Any)
esb()
.body()
.rawOption('_source', ['obj1.*', 'obj2.*'])
.build();
// result:
{
body: {
_source: ['obj1.*', 'obj2.*']
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html
(number
= 0
)
esb()
.from(1)
.build()
//result:
{
body: {
from: 1
}
}
Add an aggregation clause to the aggs body.
esb()
.body()
.aggs()
.appendAggs('message', 'terms', {
field: 'message'
})
.build();
Add aggregation to sub aggs body.
(Object
= {}
)
esb()
.body()
.aggs()
.appendAggs('message', 'terms', {
field: 'message'
})
.subAggs()
.appendAggs('name', 'terms', {
field: 'name'
})
.build();
//result
{
"body": {
"aggs": {
"message": {
"terms": {
"field": "message"
},
"aggs": {
"name": {
"terms": {
"field": "name"
}
}
}
}
}
}
}
fork from current aggs node
esb()
.body()
.aggs()
.appendAggs('by_gender', 'terms', {
"field": "gender"
})
.subAggs()
.forkAggs()
.appendAggs('by_city', 'terms', {
"field": "city"
})
.subAggs()
.appendAggs('all_name', 'terms', {
"field": "name"
})
.mergeAggs()
.appendAggs('by_language', 'terms', {
"field": "language"
})
.subAggs()
.appendAggs('all_name', 'terms', {
"field": "name"
})
.build()
//result:
{
"aggs": {
"by_gender": {
"terms": {
"field": "gender"
},
"aggs": {
"by_city": {
"terms": {
"field": "city"
},
"aggs": {
"all_name": {
"terms": {
"field": "name"
}
}
}
},
"by_language": {
"terms": {
"field": "language"
},
"aggs": {
"all_name": {
"terms": {
"field": "name"
}
}
}
}
}
}
}
}
Add bool clause to query body.
(Object)
bool clause
(boolMust | boolNot | boolShould | boolFilter)
:
see below.
esb()
.body()
.query()
.bool({
must: {
term : { user : 'kimchy' }
},
must_not: {
range: {
age: {
gte: 10,
lte: 20
}
}
}
})
.build()
//result:
{
"body": {
"query": {
"bool": {
"must": {
"term": { "user": "kimchy"}
},
"must_not": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
}
The clause (query) must appear in matching documents and will contribute to the score.
(...any)
esb()
.body()
.query()
.bool()
.boolMust({
term : { user : 'kimchy' }
})
.build()
//result
{
"body": {
"query": {
"bool": {
"must": [
{
"term" : {
"user" : 'kimchy'
}
}
]
}
}
}
}
The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.
(...any)
esb()
.body()
.query()
.bool()
.boolNot({
term : { user : 'kimchy' }
})
.build()
//result
{
"body": {
"query": {
"bool": {
"must_not": [
{
"term" : {
"user" : 'kimchy'
}
}
]
}
}
}
}
The clause (query) should appear in the matching document. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.
(...any)
esb()
.body()
.query()
.bool()
.boolShould({
term : { user : 'kimchy' }
})
.build()
//result
{
"body": {
"query": {
"bool": {
"should": [
{
"term" : {
"user" : 'kimchy'
}
}
]
}
}
}
}
The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
(...any)
esb()
.body()
.query()
.bool()
.boolFilter({
term : { user : 'kimchy' }
})
.build()
//result
{
"body": {
"query": {
"bool": {
"filter": [
{
"term" : {
"user" : 'kimchy'
}
}
]
}
}
}
}