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

simple-elastic-query

v0.1.2

Published

Lightweight query builder for ElasticSearch

Downloads

1

Readme

elastic-query Build Status codecov

Lightweight query builder for ElasticSearch

Install

$ npm install elastic-query

Usage

const elasticQuery = require('elastic-query');

elasticQuery
	.term('user.name', 'Foobaruser')
	.build();

API

Currently, this query builder is only compatible with ElasticSearch 6.x.

This query builder covers most of the basics of the Query DSL and one compound query:

  • match_all [query] (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html)
  • match_none [query] (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html)
  • All the Full text queries
  • All the Term level queries
  • The bool Compound query

Another important note is when you chain multiple queries, only the last query in the chain will be returned using build(). If you use buildQueryArray() then the query builder will return an array of Query objects.

Summary:

  • build() creates a leaf query clause.
  • buildQueryArray() creates a collection of queries that can be used in a compound query clause like bool.

Build operations

elasticQuery.build()

This will create a leaf query of the last query operation performed on the query builder.

elasticQuery.buildQueryArray()

This will return an array of queries which can be used in compound queries.

Full Text Queries

elasticQuery.matchAll()

elasticQuery.matchNone()

elasticQuery.match(field, value, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.matchPhrase(field, value, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.matchPhrasePrefix(field, value, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.multiMatch(fields, value, [options])

field

Type: string[]

Fields you want to match.

value

Type: string

Value you want to match with the provided fields.

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.commonTerms(field, value, cutOff, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

cutoff

Type: number

Terms are allocated to the high or low frequency groups based on the cutoff_frequency, which can be specified as an absolute frequency (>=1) or as a relative frequency (0.0 .. 1.0). source

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.queryString(field, value, [options])

field

Type: string or string[]

Field(s) you want to match.

value

Type: string

Value you want to match with the provided field(s).

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.simpleQueryString(fields, value, [options])

fields

Type: string[]

Fields you want to match.

value

Type: string

Value you want to match with the provided fields.

options

Type: Object

See the documentation of ElasticSearch for all the additional properties.

Term level Queries

elasticQuery.term(fields, value)

fields

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.terms(field, values)

field

Type: string

Field you want to match.

values

Type: string[] | Object

Value you want to match with the provided field(s). Either an array of values you want to match the field with or options for terms lookup mechanism.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.termsSet(field, [options])

field

Type: string

Field you want to match.

options

Type: Object

Additional query options.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.range(field, values)

field

Type: string

Field you want to match.

values

Type: string[] | Object

Value you want to match with the provided field.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.exists(field)

fields

Type: string

Field you want to check if it exists.

elasticQuery.prefix(field, value, [boost])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

boost

Type: number

Value you want to match boost your query with.

elasticQuery.wildcard(field, value, [boost])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

boost

Type: number

Value you want to match boost your query with.

elasticQuery.regexp(field, value, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

options

Type: Object

Additional query options.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.fuzzy(field, value, [options])

field

Type: string

Field you want to match.

value

Type: string

Value you want to match with the provided field.

options

Type: Object

Additional query options.

See the documentation of ElasticSearch for all the additional properties.

elasticQuery.type(type)

type

Type: string

Filters documents matching the provided document / mapping type.

elasticQuery.ids(values, [type])

values

Type: string or string[]

Filters documents that only have the provided values as ids.

type

Type: string

Filters documents matching the provided document / mapping type.

Compound Queries

elasticQuery.bool(options)

options

Type: Object

Boolean search options

options.must

Type: Object[]

must clause, must be an array of queries.

options.filter

Type: Object[]

filter clause, must be an array of queries.

options.should

Type: Object[]

should clause, must be an array of queries.

options.must_not

Type: Object[]

must_not clause, must be an array of queries.

options.boost

Type: number

Percentage of boost to boost the query with.

options.minimum_should_match

Type: number

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. source

License

MIT © Simon Jang