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

@terrajs/elasticsearch-utils

v0.1.1

Published

Utils for elasticsearch for nodejs

Downloads

5

Readme

ElasticSearch utils for Node.js

npm version Travis Coverage license

Installation

npm install --save elasticsearch-utils

Usage

elasticsearch utils overide the collection class by adding an utils object that will expose all the elasticsearch utils methods:

Here an example without mono-elasticsearch

const elasticSearchUtils = require('elasticsearch-utils')
const { Client } = require('elasticsearch')

const client = await new Client(elasticsearchConfiguration)
const elasticSearchUtilsInstance = elasticSearchUtils(client, { log: loggerFunction })

// We can now access to elasticsearch-utils methods from .utils
const result = await elasticSearchUtilsInstance.utils.createIndex('elasticsearch-utils-index')

Here an example using mono-elasticsearch

const elasticSearchUtils = require('elasticsearch-utils')
const { client } = require('mono-elasticsearch')
const elasticSearchUtilsInstance = elasticSearchUtils(client, { log: loggerFunction })

//We can now access to elasticsearch-utils methods from .utils
const result = await elasticSearchUtilsInstance.utils.createIndex('elasticsearch-utils-index')

Methods

createIndex

createIndex(index, [settings, mappings]): Promise<void>

Create an elasticsearch index if not exist, with a specific settings and mappings

// Create the index `elasticsearch-utils-index` }
elasticSearchUtilsInstance.utils.createIndex('elasticsearch-utils-index')

// Create the index `elasticsearch-utils-index` with the specific settings and mappings
elasticSearchUtilsInstance.utils.createIndex('elasticsearch-utils-index', {
  "number_of_shards": "5"
}, {
 "type1": {
  "properties": {
   "field1": { "type": "keyword" }
   },
 }
})

createIndice

createIndice(indice = string, settings = object, mappings = object): Promise<void>

Create two indexes and one alias that pointing to one of it (default ${indice}_1) The generated indexes are ${indice}_1 and ${indice}_2

// Create an indice of the name `elasticsearch-utils`
await elasticSearchUtilsInstance.utils.createIndice('elasticsearch-utils')

// Create an indice with the name `elasticsearch-utils` and with settings and mappings
await elasticSearchUtilsInstance.utils.createIndice('elasticsearch-utils', {
  "number_of_shards": "6"
}, {
 "type1": {
  "properties": {
   "field1": { "type": "keyword" }
   },
 }
})

deleteIndice

deleteIndice(indice = string): Promise<void>

Delete a specific indice created by the function createIndice The method will delete the indexes : ${indice}_1 and ${indice}_2

// Delete the `elasticsearch-utils` indice
await elasticSearchUtilsInstance.utils.deleteIndice('elasticsearch-utils')

reindexIndice

reindexIndice(indice: string): Promise<void>

Reindex the unusedIndex from the usedIndex using the reindex function of elasticsearch

// Update unusedIndex of the indice `elasticsearch-utils`
await elasticSearchUtilsInstance.utils.reindexIndice('elasticsearch-utils')

swapIndice

swapIndice(indice: string): Promise<void>

Moving the alias to point to the unused index

// Moving the indice `elasticsearch-utils` from the used index to the unused index
// Ex: Pointing to `elasticsearch-utils_1` to `elasticsearch-utils_2`
await elasticSearchUtilsInstance.utils.swapIndice('elasticsearch-utils')

clearIndice

clearIndice(index: string, settings = object, mappings = object): Promise<void>

Clear the unused index of a specific indice with the specific settings and mappings

// Clear the indice `elasticsearch-utils`
// Ex: if current unused index is `elasticsearch-utils_2` then cleaning it.
await elasticSearchUtilsInstance.utils.clearIndice('elasticsearch-utils', {
  "number_of_shards": "4"
}, {
 "type1": {
  "properties": {
   "field1": { "type": "keyword" }
  },
 }
})

clearIndex

clearIndex(index: string, settings = object, mappings = object): Promise<void>

Clear a specific index with the specific settings and mappings

// Clear the specified index `elasticsearch-utils`
elasticSearchUtilsInstance.utils.clearIndex('elasticsearch-utils', {
  "number_of_shards": "4"
}, {
 "type1": {
  "properties": {
   "field1": { "type": "keyword" }
  },
 }
})

refreshIndex

refreshIndex(index: string): Promise<void>

Refresh a specific index. Call the refresh function of elastic search

//Refresh the `elasticsearch-index` index
elasticSearchUtilsInstance.utils.refreshIndex('elasticsearch-utils')

generateTermFilter

generateTermFilter(field = string, value = string || object): object

Generate a term filter to be use with elasticsearch search method

// Generate field1 term with value field-1 -> { term: { field1: 'field-1' }}
generateTermFilter('field1', 'field-1')
// Generate field1 terms with value ['field-1', 'field-2']
generateTermFilter('field1', ['field-1', 'field-2'])

generateExistsFilter

generateExistsFilter(field = object): object

Generate exists filter to be use with elasticsearch search method

// Generate field1 exists with value field-1 -> { exists: { field1: 'field-1' }}
generateExistsFilter({ field1: 'field-1' })

generateRangeFilter

generateRangeFilter(field = string, value = string || Array): object

Generate range bool should filter to be use with elasticsearch search method

// Generate range for age with 2 to 12 and 4 to 25
generateRangeFilter('age', ['2-12', '4-25'])

search

search(query = object => { index: string, type: string, options = { limit: ..., offset: ... }, body: object })

The search method return an elasticsearch search result from a specific index, type, body and options.

Options:

  • limit: Nb of hits to return, no limit by default
  • offset: Nb of hits to skpi, default: 0
// We search for a document that match the index `elasticsearch-utils` type `type1` with projection and limit at 1 element
const result = await elasticSearchUtilsInstance.utils.search('elasticsearch-utils', 'type1', {
  limit: 1,
  offset: 0
})

getUsedIndex

getUsedIndex(indice: string): Promise<string>

Return the used index for a specific indice

// Return the used index of `elasticsearch-utils` indice
const usedIndex = await elasticSearchUtilsInstance.utils.getUsedIndex('elasticsearch-utils')

getUnusedIndex

getUnusedIndex(indice: string): Promise<string>

Return the unused index for a specific indice

// Return the unused index of `elasticsearch-utils` indice
const unusedIndex = await elasticSearchUtilsInstance.utils.getUnusedIndex('elasticsearch-utils')