Skip to content

Converts a QueryCraft Filter Builder object into an Elasticsearch query

License

Notifications You must be signed in to change notification settings

BeameryEdge/queryCraft-to-elasticsearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QueryCraft-To-Elasticsearch

Converts a QueryCraft Filter Builder object into the body of an Elasticsearch query.

NPM

npm version CircleCI codecov David deps Known Vulnerabilities

Installation

npm install --save 'querycraft-to-elasticsearch'

Examples

Suppose we have a collection of data that satisfies the interface

interface Contact {
    id: string
    'list': { id: string }[]
    firstName: string
    lastName: string
    email: string
    createdAt: Date
    customFields: { id: string, value: number }[]
    assignedTo?: string
}

If we want a query the describes the logic:-

    first 50 items where
        fistName is bob
        lastName is doyle OR is not set
        assignedTo is anything
        list has an item where id is item1
    sorted (in ascending order) by the value property of the customField where id is custom1
    created less than 5 days ago

We can build build it as easily as:-

import { FilterBuilder, eq, lt, neq, any, find, where } from 'querycraft'
import toElastic from 'querycraft-to-elasticsearch'
import { Client } from 'elasticsearch'

const client = new Client({ host: 'http://localhost:9200/'})
        
async function getContacts(filter: FilterBuilder){
    const result = await client.search({
        explain: true,
        index: testIndexName,
        body: toElastic(filter, fieldIdMapFn)
    })

    return  result.hits.hits.map(prop('_source')) as Contact[]
    // -> filtered list of contacts
}

const filter = new FilterBuilder()
.where('firstName', eq('bob'))
.where('list', find(where('id', eq('item1'))))
.where('lastName', any([
    eq('doyle'),
    eq(null)
]))
.where('createdAt', lt({ daysAgo: 5 }))
.where('assignedTo', neq(null))
.setSortFieldId('customFields', 'custom1', 'value')
.setSortDirection('ASC')
.setLimit(50)

getContacts(filter)
.then(console.log)
// -> filtered list of contacts

About

Converts a QueryCraft Filter Builder object into an Elasticsearch query

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published