Coder Social home page Coder Social logo

es-kit's Introduction

ES Kit

ES Kit is a "pick and mix" library that simplifies writing Elasticsearch code

ES Kit logo

Abstract

Elasticsearch's JavaScript Client provides a powerful way to call Elasticsearch APIs but its inherent flexibility can be an issue:

  • complex request and response formats can lead to verbose, duplicate or fragile application code
  • if you're new to the API, documentation or terminology, it's difficult to know what code to write

To address this, ES Kit provides a constrained "kit of parts" in the form of reusable, atomic helper functions which fit together to reliably abstract the API lifecycle, making it quick, easy (and reasonably obvious how) to write clean, trial-and-error-free client code.

Who is this library for?

Whilst primarily aimed at simplifing Elasticsearch for new users, ES Kit aims to be useful for any user:

  • New users: use the query functions and Api to get up and running quickly
  • Improving users: use the docs, query functions and helpers to learn how Elasticsearch fits together
  • Experienced users: customise the core helpers to build requests and parse responses in less overall code

New users: check the Elasticsearch 101 document for a valuable primer on how ES works differently to how you might expect and the Elasticsearch tips document on how to work with Elastic vs against it.

The kit

ES Kit comprises the following components:

Type Purpose Code example
Queries Build Elastic queries using simple functions _multiMatch('*', 'cat')
Helpers Abstract key parts of the Elastic API lifecycle $paginate(res)
Scripts Build Elastic scripts from JavaScript functions _removeFromArray('listIds', 24)
Api Simplified interaction with Elasticsearch's APIs Api.search('contacts', params)

Each of the helpers, functions or classes aims to be simple, atomic and transparent; rather than a complex or overly-abstracted monolithic framework, these small self-contained units let you go all-in on abstraction or pick and mix what suits your development style or project.

If a helper doesn't fit your use case:

  • write that bit of code yourself, and quickly move on
  • extend the Helpers with your new code and use it again

Code examples

Quick and easy

Use the query builder functions and Api class to handle configuration, loading, parsing, pagination and error handling in a single line:

return Api.search('contacts', _.should([
  _.prefix('name', 'ke')
  _.match('lists', 'sales'),
  _.multiMatch('*', 'san diego'),
]), { size: 20, from: 100 })

New users: check the Elastic tips document to understand which queries to use and when

Additionally, unlike Elasticsearch's APIs, ES Kit's Api returns simplified, consistent response data across all Search and Document API requests, making it super-easy to get started without spending time worrying if you're doing it right or reinventing the wheel:

[
  {
    "_id" : "ZRWBNH0Bk8QNffIJQWQp",
    "name" : "Kelly Hill",
    "notes": "Works in the San Diego area"
    "lists": [
    	"sales"
    ]
  },
  {
    "_id" : "lCloSH0Bs3kx_70FRmtW",
    "name" : "Kevin James",
    "office": "San Diego"
    "lists": []
  }
]

Fully customised

ES Kit is designed to be used as a kit of parts; there are helper functions to handle each stage of the request / response lifecycle.

You can create scripts on the fly or pick from ES Kit's library (making Painless relatively pain-free!):

const script = $.script(function (ctx, params) { ... }, { lists: 'sales' })

You can build request parameters passing query, script, and sort options to the native client as you would normally:

const res = client.updateByQuery({ index: 'contacts', query, script, sort })

Then paginate, extract or build docs without writing lots of call-specific code:

return $.paginate(res, options)

Next steps

Install via NPM:

npm i @davestewart/es-kit

Then, start writing code:

If you're new to Elastic, get up to speed:

es-kit's People

Contributors

davestewart avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

es-kit's Issues

Tasks for 1.0

API

Queries

Compound

Full text

Term

Helpers

  • log
  • error
  • sort
  • painless
  • script
  • query
  • bulk
  • options
  • fields
  • paginate
  • extract
  • doc
  • results

API

Index API

Index management

Search API

Core search

Document API

Single document APIs

Multi-document APIs

Testing

Fixtures

  • create
  • search
  • get
  • update
  • delete

Tests

  • Queries
  • Helpers
  • Scripts
  • API

Docs

Intro

  • main readme
  • getting started

API

  • Queries
  • Helpers
  • Scripts
  • APIs

Reference

  • Elastic 101
  • Elastic tips

Migrate to new ES TypeScript types

Currently ES Kit uses home-rolled types, but the new type definitions are much more robust!

Therefore, should look to:

  • replace the internal types with new ES types
  • ensure generics flow through from Api to elastic client i.e. client.search<Source>(params)
  • ensure generics flow through utils classes like results() and paginate()
  • use Omit utility type to remove index from SearchRequests

Investigate:

  • how single catch-all functions like search() or extract() would work
  • maybe have separate functions + one "catch-all" convenience function

Overall though, the types look very promising and will prevent reinvention of the wheel!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.