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

@coedl/elastic-query-builder

v0.3.0

Published

A library to help construct elastic search queries

Downloads

3

Readme

Elastic Query Builder

Inspired by https://elastic-builder.js.org/docs/. I couldn't get it to work in the browser but the ideas are very cool so I thought I'd use this opportunity to setup up a typescript library that supports both CommonJS (cjs) and ES (mjs) modules.

API documentation is available at https://coedl.github.io/elastic-query-builder/

Developing the library

In a terminal:

> npm run develop

This sets up typescript in watch mode to compile the code.

In another terminal:

> npm run tests:watch

This gets Jest going in watch mode to run the tests. With regard to Jest - support for ES modules support is experimental ({https://jestjs.io/docs/ecmascript-modules(https://jestjs.io/docs/ecmascript-modules)}) so note that the tests use cjs imports from the cjs bundles in dist.

Folder setup

Adding a query or aggregation

Say you needed an interval query as defined in QueryDSL: Full Text Queries:

  • Add src/queries/full-text-queries/interval-query.ts with code to return the query fragment
  • Add a test for it in src/queries/full-text-queries/interval-query.spec.js (note the JS suffix - see note above re: jest and es modules)
  • Register the query in src/queries/index.ts

Publishing a new version

  • Build the distributables (dist): > npm run build
  • Check it in: > git add dist docs && git commit
  • Bump the version: npm version [major | minor | patch ] as required
  • Publish: npm publish

Usage - tutorial

The base building block is the Query class. Instantiate this to start building a query:

import { Query } from '@coedl/elastic-query-builder'

let query = new Query({})

Once you have a query object you can append a query clause to it:

import { termQuery } from '@coedl/elastic-query-builder/queries'

query = query.append(termQuery({}))

Or if you have a complex set of queries append a BoolQuery:

import { BoolQuery } from '@coedl/elastic-query-builder'
import { termQuery, matchQuery, rangeQuery } from '@coedl/elastic-query-builder/queries'

query = query.append(
    new BoolQuery()
        .must( [termQuery()] )
        .should( [matchQuery(), rangeQuery({}) ])

Multi level compound queries are supported:

query = query.append(
    new BoolQuery()
        .must( [
            termQuery(),
            new BoolQuery()
                .should( [termQuery, rangeQuery] )
        ] )
        .should( [matchQuery(), rangeQuery({}) ])

Notice we're always storing the return query.

When you're ready to search get the JSON representation of the query and search away:

query = query.toJSON()

// this is however you execute a query against elastic: see helpers.ts for the method let result =
await execute({ index, query });

Aggregations are added in the same way:

import { termsAggregation } from '@coed/elastic-query-builder/aggregations'

query = query.aggregation(termsAggregation({}))

Or many aggregations:

import { termsAggregation, cardinalityAggregation } from '@coed/elastic-query-builder/aggregations'

query = query.aggregation([ termsAggregation({}), cardinalityAggregation({}) ])

And then execute:

query = query.toJSON()

// this is however you execute a query against elastic: see helpers.ts for the method let result
await execute({ index, query });