Socket
Socket
Sign inDemoInstall

elasticsearch-dynamic-query

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    elasticsearch-dynamic-query

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


Version published
Weekly downloads
13
increased by225%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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.

OperatorDescription
$eqEqual
$neqNot Equal
$inIncluded in an array
$ninNot included in an array
$likeMatch in text
$nlikeNot match in text
$ltLess than
$lteLess than or equal to
$gtGreater than
$gteGreater than or equal to
$existsExists field or not
$regexSupported regular expression
$betweenIs between
$orOr expression

Initialize Elasticsearch Dynamic Query Builder:

const builder = new ElasticSearchDynamicQuery(command);
List of Query
  • Compound 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.

Keywords

FAQs

Last updated on 08 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc