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

elasticsearch-dynamic-query

v0.4.0

Published

A simple query builder, it will helps to develop DSL query for elasticsearch

Downloads

68

Readme

Elasticsearch Dynamic Query Builder

A simple query builder, it will helps to develop DSL query for elasticsearch

Installation

You can start it from npm. Go to your terminal and run this command from your project root directory.

npm install elasticsearch-dynamic-query

After installation, import Elasticsearch Dynamic Query Builder in your file,

const { ElasticSearchDynamicQuery } = require('elasticsearch-dynamic-query');

Now you are ready to build your queries by your logical command.

Usage

Before build your query you need to develop your logical command based on your requirements. For building your logical command need to maintain a proper structure. Here is regular example,

const command = {
    fieldName: {
        type: DataTypeEnum, // ID, TEXT, NUMBER, ARRAY, DATETIME,
        conditions: {
            $eq?: any;
            $neq?: any;
            $in?: string[] | number[];
            $nin?: string[] | number[];
            $like?: any;
            $nlike?: any;
            $lt?: string | number;
            $lte?: string | number;
            $gt?: string | number;
            $gte?: string | number;
            $exists?: boolean;
            $regex?: string;
            $between?: {
                $lt?: string | number;
                $lte?: string | number;
                $gt?: string | number;
                $gte?: string | number;
            }
            $or?: {
              // Without $or, all conditional operator available under $or
            }
        }
    }
}

In your command you can pass multiple fields with type and conditions. Here is multiple accepatable data type and conditional operators.

Data type

This data type will be accept as type value in your field.

export enum DataTypeEnum {
  ID = 'ID',
  TEXT = 'TEXT',
  NUMBER = 'NUMBER',
  ARRAY = 'ARRAY',
  DATETIME = 'DATETIME',
  BOOLEAN = 'BOOLEAN',
}

Conditonal Operator

This conditional operator will be accept as conditions value in your field.

| Operator | Description | | -------- | ----------- | | $eq | Equal | | $neq | Not Equal | | $in | Included in an array | | $nin | Not included in an array | | $like | Match in text | | $nlike | Not match in text | | $lt | Less than | | $lte | Less than or equal to | | $gt | Greater than | | $gte | Greater than or equal to | | $exists | Exists field or not | | $regex | Supported regular expression | | $between | Is between | | $or | Or expression |

Initialize Elasticsearch Dynamic Query Builder:

const builder = new ElasticSearchDynamicQuery(command);

List of Query

Compound Query

Compound queries wrap other compound or leaf queries, either to combine their results and scores, to change their behaviour, or to switch from query to filter context. Currently this package support these query under Compound Query:

Bool Query:

The default query for combining multiple leaf or compound query clauses, as must, should, must_not, or filter clauses. The must and should clauses have their scores combined — the more matching clauses, the better — while the must_not and filter clauses are executed in filter context. Below added example how to use Bool Query:

const query = builder.compoundQuery().build();

In default, Compound Query Build Bool query so no need to pass any type under compoundQuery() to build dynamic query. But compoundQuery(type) can accept other type.

This .build() function will generate a validate object using logical command

{
  "bool": {
    "must": [
      {
        "match": {
          "cast": "Antti"
        }
      }
    ],
    "filter": [
      {
        "range": {
          "release_year": {
            "lt": 2020,
            "gte": 2016
          }
        }
      }
    ]
  }
}

You can pass generated query to your searching index.

Contributing

Pull requests are welcome. For any changes, please open an issue first to discuss what you would like to change.