moleculer-elasticsearch

Elasticsearch service for Moleculer.

Usage no npm install needed!

<script type="module">
  import moleculerElasticsearch from 'https://cdn.skypack.dev/moleculer-elasticsearch';
</script>

README

Moleculer logo

moleculer-elasticsearch NPM version

Elasticsearch service for Moleculer.

Features

  • support 5.4 API
  • straightforward actions & params

Install

$ npm install moleculer-elasticsearch --save

Usage

Running Elasticsearch server for development

 $ docker run -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" docker.elastic.co/elasticsearch/elasticsearch:5.5.0

More information about Elasticsearch Docker image.

Start service

"use strict";

let { ServiceBroker } 	= require("moleculer");
let ESService 			= require("moleculer-elasticsearch");

// Create broker
let broker = new ServiceBroker({ logger: console });

// Create service
broker.createService({
    mixins: [ESService],
    settings: {
        elasticsearch: {
            host: "http://elastic:changeme@<docker-hostname>:9200"
        }
    }
});

broker.start()

    // Create a document
    .then(() => broker.call("elasticsearch.create", { 
        index: "demo", 
        type: "default", 
        id: "1", 
        body: { name: "John Doe", age: 36 }
    }))

    .delay(500)

    // Search documents
    .then(() => broker.call("elasticsearch.search", { 
        body: {
            query: {
                match: {
                    name: "john"
                }
            }
        } 
    }).then(res => console.log("Hits:", res.hits.hits)))
    
    // Remove document
    .then(() => broker.call("elasticsearch.delete", { index: "demo", type: "default", id: "1" }))

Settings

Property Type Default Description
elasticsearch Object required Elasticsearch constructor options. More options
elasticsearch.host String required Host
elasticsearch.apiVersion String required API version

Actions

bulk

Perform many index/delete operations in a single API call.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-bulk

Parameters

Property Type Default Description
index String - Default index for items which don’t provide one
type String - Default document type for items which don’t provide one
body Array required The request body, as either an array of objects or new-line delimited JSON objects

Results

Type: Object

Elasticsearch response object

create

Adds a typed JSON document in a specific index, making it searchable. If a document with the same index, type, and id already exists, an error will occur.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-create

Parameters

Property Type Default Description
index String required The name of the index
type String required The type of the document
id String required Document ID
body Object required The request body, as either JSON or a JSON serializable object.

Results

Type: Object

Elasticsearch response object

get

Get a typed JSON document from the index based on its id.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-get

Parameters

Property Type Default Description
index String required The name of the index
type String required The type of the document
id String - Document ID

Results

Type: Object

Found document

update

Update (reindex) the document with the specified unique id.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

Parameters

Property Type Default Description
index String required The name of the index
type String required The type of the document
id String required Document ID
body Object required The request body, as either JSON or a JSON serializable object.

Results

Type: Object

Elasticsearch response object

delete

Delete the document with the specified unique id.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-delete

Parameters

Property Type Default Description
index String required The name of the index
type String required The type of the document
id String required Document ID

Results

Type: Object

Elasticsearch response object

search

Return documents matching a query, aggregations/facets, highlighted snippets, suggestions, and more.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-search

Parameters

Property Type Default Description
index String, Array.<String> required A comma-separated list of index names to search; use _all or empty string to perform the operation on all indices
type String, Array.<String> required A comma-separated list of document types to search; leave empty to perform the operation on all types
q String - Query in the Lucene query string syntax.
body Object - The request body, as either JSON or a JSON serializable object.

Results

Type: Object

Elasticsearch response object

count

Get the number of documents for the cluster, index, type, or a query.

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-count

Parameters

Property Type Default Description
index String, Array.<String> required A comma-separated list of indices to restrict the results.
type String, Array.<String> required A comma-separated list of types to restrict the results.
q String - Query in the Lucene query string syntax.
body Object - The request body, as either JSON or a JSON serializable object.

Results

Type: Object

Elasticsearch response object

call

Call any Elasticsearch API

More info: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

Parameters

Property Type Default Description
api String required The name of the API
params Object required Params of request

Results

Type: Object

Elasticsearch response

Methods

Test

$ npm test

In development with watching

$ npm run ci

License

The project is available under the MIT license.

Contact

Copyright (c) 2016-2019 MoleculerJS

@moleculerjs @MoleculerJS