Nancy's Preferred Machete
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

bodybuilder

0.3.0 • Public • Published

bodybuilder

An elasticsearch query body builder. Easily build complex queries for elasticsearch with a simple, predictable api.

bodybuilder

Install

npm install bodybuilder

Usage

var Bodybuilder = require('bodybuilder');
var body = new Bodybuilder();

Queries

Use addQuery(queryType, fieldToQuery, searchTerm) to build a query.

var body = new Bodybuilder().addQuery('match', 'message', 'this is a test');
// body == {
//   query: {
//     filtered: {
//       query: {
//         match: {
//           message: 'this is a test'
//         }
//       }
//     }
//   }
// }

Filters

Use filter(filterType, fieldToFilter, searchTerm) to build a filtered query.

var body = new Bodybuilder().filter('term', 'message', 'test');
// body == {
//   query: {
//     filtered: {
//       filter: {
//         term: {
//           message: 'test'
//         }
//       }
//     }
//   }
// }

Aggregations

Use aggregation(aggType, fieldToAgg, optionalAggName), or agg, to build an aggregation query. The optional aggregation name defaults to agg_<aggType>_<fieldToAgg>.

var body = new BodyBuilder().aggregation('terms', 'user')
// body == {
//   aggregations: {
//     agg_terms_user: {
//       terms: {
//         field: 'user'
//       }
//     }
//   }
// }

Combining queries, filters, and aggregations

Multiple queries and filters are merged using the boolean query or filter (see Combining Filters).

var body = new BodyBuilder().addQuery('match', 'message', 'this is a test')
                            .filter('term', 'user', 'kimchy')
                            .filter('term', 'user', 'herald')
                            .orFilter('term', 'user', 'johnny')
                            .notFilter('term', 'user', 'cassie')
                            .aggregation('terms', 'user');
 
 // body == {
 //   query: {
 //     filtered: {
 //       query: {
 //         match: {
 //           message: 'this is a test'
 //         }
 //       },
 //       filter: {
 //         bool: {
 //           must: [
 //             {term: {user: 'kimchy'}},
 //             {term: {user: 'herald'}}
 //           ],
 //           should: [
 //             {term: {user: 'johnny'}}
 //           ],
 //           must_not: [
 //             {term: {user: 'cassie'}}
 //           ]
 //         }
 //       }
 //     },
 //     aggregations: {
 //       agg_terms_user: {
 //         terms: {
 //           field: 'user'
 //         }
 //       }
 //     }
 //   }
 // }

Sort

Set a sort direction using sort(field, direction), where direction defaults to ascending.

var body = new Bodybuilder().filter('term', 'message', 'test')
                            .sort('date');
// body == {
//   sort: {
//     date: {
//       order: 'asc'
//     }
//   },
//   query: {
//     filtered: {
//       filter: {
//         term: {
//           message: 'test'
//         }
//       }
//     }
//   }
// }

From / Size

Set from and size parameters to configure the offset and maximum hits to be returned.

var body = new Bodybuilder().filter('term', 'message', 'test')
                            .size(5)
                            .from(10);
// body == {
//   size: 5,
//   from: 10,
//   query: {
//     filtered: {
//       filter: {
//         term: {
//           message: 'test'
//         }
//       }
//     }
//   }
// }

Other Options

Set any other search request option using rawOption passing in the key-value pair to include in the body.

var body = new Bodybuilder().filter('term', 'message', 'test')
                            .rawOption('_sourceExclude', 'verybigfield');
// body == {
//   _sourceExclude: 'verybigfield',
//   query: {
//     filtered: {
//       filter: {
//         term: {
//           message: 'test'
//         }
//       }
//     }
//   }
// }

Test

Run unit tests:

npm test

install

npm i bodybuilder

Downloadsweekly downloads

16,960

version

0.3.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability