moleculer-elasticsearch
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: {
node: "http://elastic:changeme@<docker-hostname>:9200",
}
}
});
broker.start()
// Create a document
.then(() => broker.call("elasticsearch.create", {
index: "demo",
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", id: "1" }))
Settings
Property | Type | Default | Description |
---|---|---|---|
elasticsearch |
Object |
required | Elasticsearch constructor options. More options |
elasticsearch.node |
String |
required | Host |
Actions
bulk
Perform many index/delete operations in a single API call.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String |
- | Default index 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.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String |
required | The name of the index |
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.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String |
required | The name of the index |
id |
String |
- | Document ID |
Results
Type: Object
Found document
update
Update (reindex) the document with the specified unique id.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String |
required | The name of the index |
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.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String |
required | The name of the index |
id |
String |
required | Document ID |
Results
Type: Object
Elasticsearch response object
search
Return documents matching a query, aggregations/facets, highlighted snippets, suggestions, and more.
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 |
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.
Parameters
Property | Type | Default | Description |
---|---|---|---|
index |
String , Array.<String>
|
required | A comma-separated list of indices 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