dot-elastic
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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!

Package Sidebar

Install

npm i dot-elastic

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

12.5 kB

Total Files

4

Last publish

Collaborators

  • snorkypie