npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

elasticsearch-query

v1.0.6

Published

elasticsearch-query generate queries for elasticsearch by simplify params

Readme

elasticsearch-query

elasticsearch-query easily generate complex elasticsearch queries (last version for ELS 5.X)

Installation

npm install elasticsearch-query

Simple queries : mustQuery, orQuery, mustNotQuery

var elsQuery = require('elasticsearch-query');
var elsGeneratorQueries = new elsQuery();

var type = null;

var mustQuery = {
     'createdBy': 'kimchy'
};

/*
** method@generate(type, query, queryAggregations, match_type, callback);
**
**	type (string) type on your elasticsearch index (not necessary could be null)
** 	query (object) that you want to transform
**	queryAggregations (object) elasticsearch query if you want an aggregation more specific
**	match_type (object) you can use {term: true}, or {match: true}, {match_phrase_prefix: true}, ... 
** 	callback (function) return err & queryELS
*/
elsGeneratorQueries.generate(type, mustQuery, null, {term: true}, function(err, queryELS) {
	console.log('mustQuery ->', JSON.stringify(queryELS));
});

==> queryELS
  {
    "query": {
      "bool":{
        "must":[
          {
            "term":{
              "createdBy":"kimchy"
            }
          }
        ],
        "must_not":[],
        "should":[],
        "filter":[]
      }
    }
  }
  <==
  
  // I want documents createdBy kimchy or postedBy boubaks
  var orQuery = {
  	'|createdBy': 'kimchy',
  	'|postedBy': 'boubaks'
  };
  elsGeneratorQueries.generate(type, orQuery, null, {term: true}, function(err, queryELS) {
  	console.log('orQuery ->', JSON.stringify(queryELS));
  });
  
  
  // I want documents not createdBy kimchy & not postedBy boubaks
  var mustNotQuery = {
  	'!createdBy': 'kimchy',
  	'!postedBy': 'boubaks'
  }
  elsGeneratorQueries.generate(type, mustNotQuery, null, {term: true}, function(err, queryELS) {
  	console.log('mustNotQuery ->', JSON.stringify(queryELS));
  });
  

Range queries : $gt, $gte, $in, $lt, $lte, $ne & $nin

  // I want documents where "age" is between 18 and 60 both included
  var rangeQuery = {
  	'$lte': { 'age': 60 },
  	'$gte': { 'age': 18 }
  };
  
  elsGeneratorQueries.generate(type, rangeQuery, null, {term: true}, function(err, queryELS) {
  	console.log('rangeQuery ->', JSON.stringify(queryELS));
  });
  ==> queryELS
  {  
    "query":{
      "bool":{  
        "must":[  
          {  
            "range":{  
              "age":{  
                "lte":60,
                "gte":18
              }
            }
          }
        ],
        "must_not":[],
        "should":[],
        "filter": []
      }
    }
  <==
  

Complexes queries ($facet, $exist, $not_exist)

  // I want the username aggregation and count between VERIFIED people where the field AGE exist and between 18-60 years old, not include boubaks
  var complexQuery = {
  	'verified': true,
  	'$exist': 'age',
  	'$lte': { 'age': 60 },
  	'$gte': { 'age': 18 },
  	'!username': 'boubaks',
  	'$facet': 'username',
  	'$count': true
  };
  
  elsGeneratorQueries.generate(type, complexQuery, null, {term: true}, function(err, queryELS) {
  	console.log('complexQuery ->', JSON.stringify(queryELS));
  });
  
  ==> queryELS
  {  
    "query":{  
      "bool":{  
        "must":[  
          {  
            "term":{  
              "verified":true
            }
          },
          {  
            "range":{  
              "age":{  
                  "lte":60,
                  "gte":18
              }
            }
          },
          {  
            "exists":{  
              "field":"age"
            }
          }
        ],
        "must_not":[  
          {  
            "term":{  
              "username":"boubaks"
            }
          }
        ],
        "should":[],
        "filter": []
      }
    },
    "aggregations":{  
      "aggs":{
          "terms":{
            "field":"username",
            "size":10
          }
      }
    }
  }
  <==
  

elsQuery object

You can add an handle function to a specific field setted

  // addHandle(param, defaultValue, fcn);
  // deleteHandle(index[, all (boolean)]);
  // getHandles(); // console.log by default (will be removed)

  /*
  ** @query: {$test: "elasticsearch"}
  ** @queryELS: the query generate for elasticsearch
  ** callback: function(err, queryELS)
  */ 
  var handleTest = function (query, queryELS, callback) {
    var error = null;
    if (query.$test) {
      // Do your stuff
    }
    callback(error, queryELS);
  };
  
  elsQueryGenerator.addHandle('$test', null, handleTest);

Notes

If you want to do specific action or normal query on $page, $limit, $sort, $skip, $handle you have to add handle function