What is elasticsearch?
The elasticsearch npm package is a client library for interacting with Elasticsearch, a distributed search and analytics engine. This package allows developers to perform a variety of operations such as indexing documents, searching, and managing indices within an Elasticsearch cluster.
What are elasticsearch's main functionalities?
Indexing Documents
This feature allows you to index documents into an Elasticsearch index. The code sample demonstrates how to create a client, connect to an Elasticsearch node, and index a document into 'my-index'.
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
async function run() {
await client.index({
index: 'my-index',
document: {
title: 'Test Document',
content: 'This is a test document.'
}
});
console.log('Document indexed');
}
run().catch(console.log);
Searching Documents
This feature allows you to search for documents within an Elasticsearch index. The code sample demonstrates how to search for documents in 'my-index' where the title matches 'Test'.
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
async function run() {
const result = await client.search({
index: 'my-index',
query: {
match: { title: 'Test' }
}
});
console.log(result.hits.hits);
}
run().catch(console.log);
Managing Indices
This feature allows you to manage indices in Elasticsearch. The code sample demonstrates how to create a new index called 'my-new-index'.
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
async function run() {
await client.indices.create({
index: 'my-new-index'
});
console.log('Index created');
}
run().catch(console.log);
Other packages similar to elasticsearch
mongoose
Mongoose is an ODM (Object Data Modeling) library for MongoDB and Node.js. It provides a schema-based solution to model your application data. While it is similar in that it interacts with a database, it is specific to MongoDB and offers features like schema validation and middleware, which are not provided by the elasticsearch package.
redis
Redis is a fast, open-source, in-memory key-value data store. The redis npm package allows you to interact with a Redis database. While it offers some overlapping functionalities like data storage and retrieval, it is designed for different use cases such as caching and real-time analytics, unlike Elasticsearch which is optimized for full-text search and complex queries.
couchbase
Couchbase is a distributed NoSQL cloud database. The couchbase npm package allows you to interact with a Couchbase server. It offers similar functionalities like indexing and querying documents but is designed for high-performance applications requiring low-latency data access, whereas Elasticsearch is optimized for search and analytics.
node-elasticsearch
This is a Node.js module for the elasticsearch REST API.
Usage
var elasticsearch = require('elasticsearch');
var es = elasticsearch({index: 'kitteh'});
es.query({query: {field: {field1: 'hai'}}}, function(err, results) {
console.log(results);
});
API
Unless otherwise stated, all callbacks are called with cb(err, res)
, with res
being the parsed JSON response from elasticsearch.
elasticsearch(opts)
Shortcut for elasticsearch.createClient(opts)
.
elasticsearch.createClient(opts)
Returns a new client object.
Options:
index
: The name of the index to act upon.host
: The hostname of the elasticsearch server (defaults to localhost)port
: The port of the elasticsearch server (defaults to 9200)
We'll call the returned client es
.
es.status(opts, cb)
Get the status of the index. Maps to GET /index/_status.
es.add(opts, doc, cb)
Add a document to the index. Maps to PUT /index/type/id or POST /index/type.
Options:
id
: Optional ID for the document. A UUID will be chosen by elasticsearch if no ID is specified.type
: Optional type for the document (default: doc
).refresh
: Set this to true to refresh the index after add.
es.delete(opts, cb)
Delete a document or documents from the index. Maps to DELETE /index/type/id.
Options:
id
: Optional ID for the document. All documents of this type will be deleted if no ID is specified.type
: Optional type for the document (default: doc
).refresh
: Set this to true to refresh the index after delete.
es.get(opts, cb)
Get a document from the index. Maps to GET /index/type/id.
Options:
id
: ID for the document.type
: Optional type for the document (default: doc
).
es.query(opts, query, cb)
Query the index. Maps to POST /index/_search.
es.count(opts, query, cb)
Get the count of a query result set. Maps to POST /index/_count.
es.queryAll(opts, query, cb)
Query all indexes. Maps to POST /_search.
es.putRiver(opts, river, cb)
Put a new or updated river. Maps to PUT /_river/name/_meta.
Options:
name
: Name for the river.
es.getRiver(opts, name, cb)
Get a river. Maps to GET /_river/name/_meta.
es.deleteRiver(opts, name, cb)
Delete a river. Maps to DELETE /_river/name/.
es.putMapping(opts, config, cb)
Put a new or updated mapping. Maps to PUT /index/name/_mapping.
name
is defined by config.name
and mapping
by config.mapping
.
es.getMapping(opts, type, cb)
Get a mapping. Maps to GET /index/type/_mapping.
If type is an array its values will be joined.
es.deleteMapping(opts, config, cb)
Delete a mapping. Maps to DELETE /index/name/_mapping.
name
is defined by config.name
.
new elasticsearch.Index(opts)
Returns a new index object.
Options:
name
: The name of the index to act upon.
We'll call the returned index index
.
index.status(opts, cb)
Get the status of the index. Maps to GET /index/_status.
index.refresh(opts, cb)
Refresh the index. Maps to POST /index/_refresh.
index.create(opts, config, cb)
Create the index. Maps to PUT /index/ or POST /index/ depending on the existence of config.mappings
.
index.delete(cb)
Delete the index. Maps to DELETE /index.
new elasticsearch.Cluster()
Returns a new cluster object.
We'll call the returned cluster cluster
.
cluster.status(opts, cb)
Get the status of the cluster. Maps to GET /_status.
cluster.deleteIndices(opts, cb)
Delete all indices in this cluster. Maps to multiple calls to DELETE /index/.
cluster.health(opts, cb)
Get health of the cluster. Maps to GET _cluster/health.
Options map to query parameters.
Testing
npm install
npm test
Requirements
- Node.js
- elasticsearch
- The need for search
License
MIT