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

dot-elastic

v1.0.3

Published

Create objects using advanced dot syntax

Downloads

4

Readme

dot-elastic

Build Status npm version

Dot object syntax for creating elasticsearch queries

Install with npm or Yarn:

# via npm
$ npm install dot-elastic

# via yarn (automatically saves the package to your `dependencies` in package.json)
$ yarn add dot-elastic

Usage

import dot from 'dot-elastic';
dot({ 'a.b.c': 1, 'a.b.d': 2 });
{ "a": { "b": { "c": 1, "d": 2 } } }

In elasticsearch we often add on stuff to arrays (bool queries and similar). So it's important we have an easy syntax for this, we solve this duplicate key problem by using .u on the default import or by using the named export u. This is the biggest change from most other dot notation libs.

import dot, { u } from 'dot-elastic';

log(dot({
  [dot.u`a[].b`]: 1,
  [dot.u`a[].b`]: 2,
}));

log(dot({
  [u`a[]`]: 1,
  [u`a[]`]: 2,
}));
{ "a": [ { "b": 1 }, { "b": 2 } ] }
{ "a": [ 1, 2 ] }

Example queries shamelessly copied from Tim Ojo's article "23 Useful Elasticsearch Example Queries"

dot({
  'query.term.publisher': 'manning',
  '_source': [ 'title', 'publish_date', 'publisher' ],
  'sort[].publish_date.order': 'desc',
});
{
    "query": {
        "term": {
            "publisher": "manning"
        }
    },
    "_source": [
        "title",
        "publish_date",
        "publisher"
    ],
    "sort": [
        {
            "publish_date": {
                "order": "desc"
            }
        }
    ]
}
dot({
  [dot.u`query.bool.must.bool.should[].match.title`]: 'Elasticsearch',
  [dot.u`query.bool.must.bool.should[].match.title`]: 'Solr',
  'query.bool.must_not.match.authors': 'radu gheorge',
});
{
    "query": {
        "bool": {
            "must": {
                "bool": {
                    "should": [
                        { "match": { "title": "Elasticsearch" } },
                        { "match": { "title": "Solr" } }
                    ]
                }
            },
            "must_not": {
                "match": { "authors": "radu gheorge" }
            }
        }
    }
}

When dealing with multiple keys on the same level it sometimes makes it cleaner to use the second syntax:

dot({
  'query.function_score.query.multi_match.query': 'search engine',
  'query.function_score.query.multi_match.fields': [ 'title', 'summary' ],
  'query.function_score.field_value_factor.field': 'num_reviews',
  'query.function_score.field_value_factor.modifier': 'log1p',
  'query.function_score.field_value_factor.factor': 2,
  '_source': [ 'title', 'summary', 'publish_date', 'num_reviews' ],
});

dot({
  'query.function_score.query.multi_match': {
    query: 'search engine',
    fields: [ 'title', 'summary' ],
  },
  'query.function_score.field_value_factor': {
    field: 'num_reviews',
    modifier: 'log1p',
    factor: 2,
  },
  '_source': [ 'title', 'summary', 'publish_date', 'num_reviews' ],
});
{
    "query": {
        "function_score": {
            "query": {
                "multi_match": {
                    "query": "search engine",
                    "fields": [ "title", "summary" ]
                }
            },
            "field_value_factor": {
                "field": "num_reviews",
                "modifier": "log1p",
                "factor": 2
            }
        }
    },
    "_source": [ "title", "summary", "publish_date", "num_reviews" ]
}

Create shortcuts inside objects for prettier formatting (new in 1.0.3). This example is copied from test.ts.

const obj = {};

const shortcut = dot.ln('query.bool.must[].bool', obj);
dot({ 'should[].term.aid': 10 }, shortcut);
dot({ 'should[].term.bid': 20 }, shortcut);
{
    "query": {
        "bool": {
            "must": [ {
                "bool": {
                    "should": [ {
                        "term": {
                            "aid": 10
                        }
                    }, {
                        "term": {
                            "bid": 20
                        }
                    } ]
                }
            } ]
        }
    }
}

Contribute!

This code is not optimized at all right now but it's not really slow either but any improvements are very welcome!