Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

elastic-search-builder

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elastic-search-builder

An elasticsearch search option builder.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Elasticsearch Builder

Build Status Coverage Status

This lib working with elasticsearch.js, flatten search params and query bodies.

Installation

npm install elastic-search-builder --save

Documentation

Usage

/* in ES 5 */
const elasticsearch = require('elasticsearch');
const esb = require('elastic-search-builder');
/* in ES6 */
import elasticsearch from 'elasticsearch';
import esb from 'elastic-search-builder';
/* new elasticsearch client */
const client = new elasticsearch.Client({
  host: 'localhost:9200'
});
/* build search params by elastic-search-builder */
const searchParams = esb()
   .option()
   .indices(['2016.01.01'])
   .body()
   .query({
      match: {
         dialogs: 'hello world'
      }
   }).build();
client.search(searchParams).then(body => {
   console.log(body);
})

Bool Query

esb()
  .body()
  .query()
  .bool()
  .boolMust({
    "term" : { "user" : "kimchy" }
  }, {
    "term" : { "user" : "blob" }
  })
  .boolNot([{
    "term": { "user": "john" }
  },{
    "term": { "user": "belly" }
  }])
  .build();

// {
//   body: {
//      query: {
//         bool: {
//            must: [
//               {
//                  "term" : { "user" : "kimchy" }
//               },
//               {
//                  "term" : { "user" : "blob" }
//               }
//            ],
//            must_not: [
//               {
//                  "term": { "user": "john" }
//               },
//               {
//                  "term": { "user": "belly" }
//               }
//            ]
//         }
//      }
//   }
// }

Aggregation

basic usage

esb()
  .body()
  .aggs()
  .appendAggs('all_name', 'terms', {
    "field": "name"
  })
  .subAggs()
  .appendAggs('all_gender', 'terms', {
      "field": "gender"
   })
   .subAggs()
   .appendAggs('all_city', 'terms', {
      "field": "city"
   })
   .build()

// {
//   "aggs": {
//     "all_name": {
//       "terms": {
//         "field": "name"
//       },
//       "aggs": {
//         "all_gender": {
//           "terms": {
//             "field": "gender"
//           },
//           "aggs": {
//               "all_city": {
//                  "terms": {
//                       "field": "city"
//                   }
//                }
//            }
//         }
//       }
//     }
//   }
// }

advanced usage

build nested aggragation without callback

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()

// {
//   "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"
//                         }
//                     }
//                   }
//             }
//           }
//       }
//   }
}

Development

debug with browser
npm run dev
run test
npm run test
build documentation
npm run build:docs
Manully publish to npm
npm login
npm publish

Keywords

FAQs

Package last updated on 05 Feb 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc