1.0.0-beta.1 • Published 2 years ago

es-query-pro v1.0.0-beta.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

es-query-pro

NPM Link Build Status Code Coverage ISC License

Simple and powerful ElasticSearch query builder

Installation

npm install es-query-pro

Function reference

Basic

FunctionMatch TypeExample
term(fieldOrFields, valueOrValues, anyOrAll)Exact field valueterm('created_by', 123)
matchBoostedPhrase(fields, terms, options)Full-textmatchBoostedPhrase(['title'], 'AT&T Wireless')
match(field, valueOrValues)Full-textmatch('title', 'Market research')
exists(fieldOrFields)Exact fieldexists('deleted_at')
range(field, op, value)Exact field valuerange('age', 'between', [18, 35])

Advanced

FunctionMatch TypeExample
multiTerm(fields, value, anyOrAll)Exact field valuemultiTerm(['created_by','modified_by'], 123)
matchPhrase(field, phraseOrPhrases)Full-textmatchPhrase('title', 'Little Red Riding Hood')
matchPhrasePrefix(fieldOrFields, phraseOrPhrases)Full-textmatchPhrasePrefix('title', 'Little Red R')
multiMatch(fields, valueOrValues, anyOrAll)Full-textmultiMatch(['title','body'], ['phone','mobile'], 'ALL')
multiMatchWithPhrase(fields, valueOrValues, options)Full-textmultiMatchWithPhrase(['title','body'], ['phone','mobile'])
queryString(fieldOrFields, query)Lucene expressionqueryString('body', '(tech AND support) OR (service desk)')

boostedPhrase??

Negative conditions

FunctionMatch TypeExample
notTerm(field, valueOrValues)Exact field valuenotTerm('status', [2,3,4])
notMultiTerm(fields, value)Exact field valuenotMultiTerm(['border','outline'], 'blue')
notExists(fieldOrFields)Exact fieldnotExists('category')
notMatch(field, valueOrValues)Full-textnotMatch('title', 'Market research')
notMultiMatch(fields, valueOrValues)Full-textnotMultiMatch(['title','body'], 'Research')
notRange(field, op, value)Exact field valuenotRange('age', 'between', [36, 44])

Sorting and pagination

FunctionExample
limit(limitTo)limit(10)
page(pageNo)page(2)
sort(field)sort('-created_at')
sortByRandom(trueOrFalse)sortByRandom(true)

Subqueries

FunctionPurpose
should(subquery)Where subquery matches
shouldNot(subquery)Where subquery does not match
shouldAny(subqueries)Where any subquery matches

Special functions

FunctionPurpose
fields(fieldNames)Set which fields to return. Default is ['*'] which returns all fields
useHighlighter(definition)Ask for excerpts of text near the matching terms
includeFacets(forFields, limit)Include counts for each field
decayFunctionScore(definition)Give match weight based on a bell-curve
aggregateTerm(field, limit, exclusions)A count of field values (like GROUP BY in SQL)
dateHistogram(dateField, intervalName, timezone)Aggregate matches by time periods
clear(field)Clear page, limit, must, mustNot, aggs, fields, highlighter, or functionScore

Inspection

FunctionPurpose
toKibana()Get a string suitable for running in Kibana
getFields()Get the list of fields that will be returned
getBody()Get the structure of the query body
getOptions()Get the size, from, sort
getQuery()Return the fields, body and options to query

Examples

const EsQueryBuilder = require('es-query-pro');

const query = new EsQueryBuilder();
query.term('author', 15);
query.matchBoostedPhrase(['fulltext_*'], 'Cold pressed juice');
query.range('created_at', '>=', '2021-01-01');
query.sort('created_at', 'desc');
query.limit(25);
query.page(2);
const textProcessor = new TextProcessor();
textProcessor.setArrayJoiner('ψ');
textProcessor.registerPattern(
	{ find: /([a-z])&([a-z0-9])/gi, replace: '$1ε$2' },
	{ find: /([a-z])ε([a-z0-9])/gi, replace: '$1&$2' }
);
textProcessor.registerField(/^fulltext_/);
QueryBuilder.registerProcessor(textProcessor);

Unit Tests and Code Coverage

Powered by jest

npm test
npm run coverage

Contributing

Contributions are welcome. Please open a GitHub ticket for bugs or feature requests. Please make a pull request for any fixes or new code you'd like to be incorporated.

License

Open Source under the ISC License.