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 🙏

© 2024 – Pkg Stats / Ryan Hefner

esql

v0.0.4

Published

Humane query language for Elasticsearch

Downloads

7

Readme

ESQL (Elasticsearch Query Language)

Elasticsearch is powerful, so is its Query DSL. But Elasticsearch Query DSL's power comes at a cost: complexity. Even the simplest queries can be verbose and difficult to write. ESQL simplifies the construction of Query DSL by compiling queries written in an SQL-like language to Elasticsearch DSL. By only supporting essential features of the Query DSL, ESQL queries can be kept very simple.

The output of ESQL can be used directly as the search argument of elasticsearch-js. However, you may pick different portions should you use another mechanism to connect to Elasticsearch. You can also augment the output however you like. Therefore, you are not locked in to only the features supported by ESQL.

ESQL can be used in both Node and browser environments.

Features

  • Scope: specify indices, types and options
  • Filter: term, terms, range
  • Query: match, multi_match, range
  • Filter/query group: must, should, must_not
  • Sort: sort, asc, desc
  • Data types: boolean, number, string/date, array, null
  • Options can be specified at each level of granularity
  • Query parameterization and precompilation
  • More to come...

Note: this is an early release of ESQL, expect the language itself and possibly the API to change. Oh yes, and bugs too. Bug reports and pull requests are very welcome.

Getting started

Install ESQL from NPM or Bower

npm install --save esql
bower install --save esql

Import esql object in Node

var esql = require('esql')

Import esql object in browser (after referencing browser/esql.min.js)

var esql = window.esql

Build DSL query

var query = 'ESQL QUERY HERE'
var dsl = esql(query)

Parameterize queries

var dsl = esql('match name = $1, age = $2', name, age)

Precompile queries

var fn = esql.prepare('match name = $1, age = $2')
var dsl = fn(name, age)

Consume DSL query with elasticsearch-js

var client = new es.Client({...})
client.search(dsl).then(callback, errback)

Example

var dsl = esql(
  'from org / documents with ("from": 20, size: 10) \
   filter expired == false, level == 3..5 \
   match name = "foo" (boost: 2), description = "foo bar" (operator: "and") with (minimum_should_match: 1) \
   sort name asc, description')

The resulting dsl object is:

{
  "body": {
    "query": {
      "filtered": {
        "filter": {
          "bool": {
            "must": [
              {
                "term": {
                  "expired": false
                }
              },
              {
                "range": {
                  "level": {
                    "gte": 3,
                    "lte": 5
                  }
                }
              }
            ]
          }
        },
        "query": {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "match": {
                  "name": {
                    "boost": 2,
                    "query": "foo"
                  }
                }
              },
              {
                "match": {
                  "description": {
                    "operator": "and",
                    "query": "foo bar"
                  }
                }
              }
            ]
          }
        }
      }
    },
    "sort": [
      {
        "name": {
          "order": "asc"
        }
      },
      {
        "description": {
          "order": null
        }
      }
    ]
  },
  "index": "org",
  "type": "documents",
  "from": 20,
  "size": 10
}

The dsl object can be fed directly to elasticsearch-js. Or you can just use its body property as POST data for your own Elasticsearch query mechanism.

Syntax Reference

Basics

ESQL is case insensitive. Spaces and newlines are skipped so you can have as many of them. All clauses are optional although if specified, they must follow this order: from, filter, query, sort.

Filter/query groups are made possible with these mappings:

  • = is mapped to should
  • == is mapped to must
  • != is mapped to must_not

Range filters and queries are supported with range syntax:

  • from..to => from from to to inclusively
  • from...to => from from to to exclusively
  • Either from or to can be optional

Options can be specified for each filter, match, sort condition or the entire group. Option names and values are not type-checked or validated in anyway whatsoever. This makes the language simple and flexible but requires you to learn about the available options.

FROM clause

Use the from clause to specify indices, types and general query options.

Example 1: index only

from index1

Example 2: index and type

from index1 / type1

Example 3: multiple indices and types (including wildcard match)

from [index1, index2] / [type1, type2, moretype*]

Example 4: query options, note that from option needs escaping

from index / type with ('from': 10, size: 100)

FILTER clause

Use the filter clause to create filters.

Example 1: term search

filter tags = 'foo'

Example 2: terms search

filter tags = ['foo', 'bar']

Example 3: multiple filters

filter tags = 'foo', expired = false

MATCH clause

Use the match clause to create queries.

Example 1: single match

match name = 'foo' // => match

Example 2: multi-match

match [name, description] = 'foo' // => multi_match

Example 3: multiple matches with options

match name = 'foo' (boost: 2), description = 'foo bar' (minimum_should_match: 1)

SORT clause

Use the sort clause to specify sort fields and directions

Example

sort name asc, age desc