0.1.1 • Published 7 years ago

@terrajs/elasticsearch-utils v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

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')