esBuilder

elastic-search-builder working with elasticsearch.js

See more use case in the docs as well as in the tests.

esBuilder
Returns
(option | body | build): see below.
Example
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)
})

option

Add custom option

option(content: Object): (indices | type)
Parameters
content (Object) option body.
Returns
(indices | type): see below
Example
//build option
esb().option({
  index: 'logs',
  type: '2016.01.01'
}).build()

body

Add custom body

body(content: Object): (query | aggs | fields | size | from)
Parameters
content (Object) option body.
Returns
(query | aggs | fields | size | from): see below.
Example
esb().body({
   query: {
      match: {
         message: 'hello'
      }
   }   
})
.build()

build

Build with options and body.

build(): Object
Returns
Object: search option.

indices

Add index field to option.

indices(indices: (Array | string), ignoreUnavailable: boolean, allowNoIndices: boolean)
Parameters
indices ((Array | string) = []) multi index
ignoreUnavailable (boolean) Whether specified concrete indices should be ignored when unavailable (missing or closed)
allowNoIndices (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)
Example
esb()
 .options()
 .indices(['2016.01.01'], true, true)
 .build()
esb()
 .options()
 .indices('2016.01.01,2016.02.02', false, false)
 .build()

type

Add type field to option

type(args: ...any)
Parameters
args (...any)
Example
esb()
 .options()
 .type(['company', 'school'], 'employee', 'student')
 .build()

query

Add query clause to query body

query(queryBody: Object): bool
Parameters
queryBody (Object = {}) any query clause
Returns
bool: see below.
Example
//build body
esb()
 .body()
 .query({
     match: {
         message: 'hello'
     }
 })
 .build()
//result:
{
    body: {
         query: {
             match: {
                 message: 'hello'
             }
        }   
    }
}

aggs

Add aggregation to aggs body

aggs(aggsBody: Object): (appendAggs | subAggs | forkAggs | mergeAggs)
Parameters
aggsBody (Object = {}) any aggregation body
Returns
(appendAggs | subAggs | forkAggs | mergeAggs): see below.
Example
esb()
 .body()
 .aggs({
     message: {
         terms: {
             field: 'message'
         }
     }
 })
 .build();
//result:
{
     body: {
         aggs: {
             message: {
                 terms: {
                     field: 'message'
                 }
             }
         }
     }
}

fields

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-field-caps.html

fields(args: ...any)
Parameters
args (...any)
Example
esb()
 .body()
 .fields('rating', 'count')
 .build()
//result:
{
     body: {
         fields: ['rating', 'count']
     }
}

size

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

size(size: number)
Parameters
size (number = 0)
Example
esb()
 .size(10)
 .build()
//result:
{
     body: {
         size: 10
     }
}

rawOption

rawOption(key: string, value: Any)
Parameters
key (string)
value (Any)
Example
esb()
 .body()
 .rawOption('_source', ['obj1.*', 'obj2.*'])
 .build();
// result:
{
     body: {
         _source: ['obj1.*', 'obj2.*']
     }
}

from

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

from(from: number)
Parameters
from (number = 0)
Example
esb()
 .from(1)
 .build()
//result:
{
     body: {
         from: 1
     }
}

appendAggs

Add an aggregation clause to the aggs body.

appendAggs(name: string, type: string, body: Object)
Parameters
name (string) Name of the aggregation.
type (string) Name of the aggregation type, such as 'sum' or 'terms' .
body (Object) Options to include in the aggregation.
Example
esb()
 .body()
 .aggs()
 .appendAggs('message', 'terms', {
     field: 'message'
 })
 .build();

subAggs

Add aggregation to sub aggs body.

subAggs(aggsBody: Object)
Parameters
aggsBody (Object = {})
Example
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"
                     }
                 }
             }
         }
     }
 }
}

forkAggs

fork from current aggs node

forkAggs()
Example
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"
                          }
                      }
                  }
              }
          }
      }
  }
}

mergeAggs

mergeAggs()

bool

Add bool clause to query body.

bool(boolBody: Object): (boolMust | boolNot | boolShould | boolFilter)
Parameters
boolBody (Object) bool clause
Returns
(boolMust | boolNot | boolShould | boolFilter): see below.
Example
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
                         }
                     }
                 }
             }
         }
     }
}

boolMust

The clause (query) must appear in matching documents and will contribute to the score.

boolMust(args: ...any)
Parameters
args (...any)
Example
esb()
 .body()
 .query()
 .bool()
 .boolMust({
     term : { user : 'kimchy' }
 })
 .build()

//result
{
 "body": {
     "query": {
         "bool": {
             "must": [
                 {
                     "term" : { 
                         "user" : 'kimchy' 
                     }
                 }
             ]
         }
     }
 }
}

boolNot

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.

boolNot(args: ...any)
Parameters
args (...any)
Example
esb()
 .body()
 .query()
 .bool()
 .boolNot({
     term : { user : 'kimchy' }
 })
 .build()

//result
{
 "body": {
     "query": {
         "bool": {
             "must_not": [
                 {
                     "term" : { 
                         "user" : 'kimchy' 
                     }
                 }
             ]
         }
     }
 }
}

boolShould

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.

boolShould(args: ...any)
Parameters
args (...any)
Example
esb()
 .body()
 .query()
 .bool()
 .boolShould({
     term : { user : 'kimchy' }
 })
 .build()

//result
{
 "body": {
     "query": {
         "bool": {
             "should": [
                 {
                     "term" : { 
                         "user" : 'kimchy' 
                     }
                 }
             ]
         }
     }
 }
}

boolFilter

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.

boolFilter(args: ...any)
Parameters
args (...any)
Example
esb()
 .body()
 .query()
 .bool()
 .boolFilter({
     term : { user : 'kimchy' }
 })
 .build()

//result
{
 "body": {
     "query": {
         "bool": {
             "filter": [
                 {
                     "term" : { 
                         "user" : 'kimchy' 
                     }
                 }
             ]
         }
     }
 }
}