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

ginlibs-es-query-dls

v1.1.0

Published

generate ES query dls

Downloads

10

Readme

ginlibs-es-query-dls

NPM version NPM Weekly Downloads License

生成 es 查询 dls 的工具类

安装

npm i ginlibs-es-query-dls --save

使用例子

import EsQueryDls from 'ginlibs-es-query-dls'

const dls = new EsQueryDls().must
  .term({
    name: {
      value: 'apple',
      keyword: true,
    },
  })
  .toQuery('fruit_es', 'fruit_type')

console.log(dls)

/**
{
  "index": "fruit_es",
  "type": "fruit_type",
  "body": {
    "query": {
      "bool": {
        "must": [
          {
            "term": {
              "name.keyword": "apple"
            }
          }
        ]
      }
    }
  }
}
**/

目录

API

  • api 都有带有一个上层条件,意义如下
    • must: 对应 es 的 must 条件,对应 逻辑
    • filter: 对应 es 的 filter 条件,对应 逻辑,和 must 不同,filter 不进行评分,查询性能更好
    • not: 对应 es 的 must_not 条件,对应 逻辑
    • should: 对应 es 的 must 条件,对应 逻辑

term api

  • .must.term(data)
  • .filter.term(data)
  • .not.term(data)
  • .should.term(data)

对应的 es 的 term 条件,表示全匹配

参数:data

  • Type: Object
  • Desc:
    • data 的 键名 是要筛选的字段名,键值是要查询的 字段值
    • 键值 也可以是一个对象,{value: 'any', keyword: true}, value 是查询的 字段值, keyword 表示是否制定为 keyword 类型

例子

new EsQueryDls().not.term({
  name: {
    value: 'apple',
    keyword: true,
  },
})

生成的 dls:

{
  "must_not": [
    {
      "term": {
        "name.keyword": "apple"
      }
    }
  ]
}

like api

  • .must.like(data)
  • .filter.like(data)
  • .not.like(data)
  • .should.like(data)

对应的 es 的 wildcard 条件,表示模糊匹配

参数:data

  • Type: Object
  • Desc:
    • data 的 键名 是要筛选的字段名,键值是要查询的 字段值

例子

new EsQueryDls().must.like({
  name: {
    value: 'apple',
  },
})

生成的 dls:

{
  "must": [
    {
      "wildcard": {
        "name.keyword": "*apple*"
      }
    }
  ]
}

range api

  • .must.range(data)
  • .filter.range(data)
  • .not.range(data)
  • .should.range(data)

对应的 es 的 range 条件,表示范围匹配

参数:data

  • Type: Object
  • Desc:
    • data 的 键名 是要筛选的字段名,键值是对应的范围信息 rangeInfo
    • rangeInfo
      • Type: Object
      • Desc: 键名只能是 gte, lte, lt, gt, 键值是具体范围值

例子

new EsQueryDls().must.like({
  count: {
    gt: 0,
    lt: 10,
  },
})

生成的 dls:

{
  "must": [
    {
      "range": {
        "count": {
          "gt": 0,
          "lt": 10
        }
      }
    }
  ]
}

Test Report

Tests are using jest, to run the tests use:

$ npm run test

report detail

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |   75.51 |    22.62 |   52.38 |   76.84 |
 index.ts |   53.19 |     12.5 |   28.57 |   55.56 | 35-64,71,96-114
 leaf.ts  |   96.08 |    83.33 |     100 |      96 | 59,97
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        3.418 s
Ran all test suites.

License (MIT)