What is @elastic/elasticsearch?
@elastic/elasticsearch is the official Node.js client for Elasticsearch. It allows developers to interact with Elasticsearch clusters, perform CRUD operations, search, and manage indices, among other functionalities.
What are @elastic/elasticsearch's main functionalities?
Connecting to Elasticsearch
This code demonstrates how to create a new client instance to connect to an Elasticsearch cluster running on localhost.
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
Indexing Documents
This code sample shows how to index a document into an Elasticsearch index named 'my-index'.
async function run() {
await client.index({
index: 'my-index',
id: '1',
body: {
title: 'Test Document',
content: 'This is a test document.'
}
});
}
run().catch(console.log);
Searching Documents
This code demonstrates how to search for documents in the 'my-index' index that match the term 'Test' in the title field.
async function run() {
const { body } = await client.search({
index: 'my-index',
body: {
query: {
match: { title: 'Test' }
}
}
});
console.log(body.hits.hits);
}
run().catch(console.log);
Managing Indices
This code sample shows how to create a new index named 'my-new-index' in Elasticsearch.
async function run() {
await client.indices.create({
index: 'my-new-index'
});
}
run().catch(console.log);
Other packages similar to @elastic/elasticsearch
elasticsearch
The 'elasticsearch' package is an older, community-maintained client for Elasticsearch. It provides similar functionalities but is not officially maintained by Elastic. It may lack some of the newer features and optimizations present in @elastic/elasticsearch.
searchkit
Searchkit is a toolkit for building search UIs with Elasticsearch. It provides higher-level abstractions and components for building search interfaces, making it easier to integrate Elasticsearch into front-end applications. However, it may not offer the same low-level control as @elastic/elasticsearch.
@elastic/elasticsearch
Note: In the past months we have worked on the new Elasticsearch Node.js client, and if you want you can already try it by following the instructions below, while if you're going to use the legacy one or report an issue, please check out elastic/elasticsearch-js-legacy.
The official Node.js client for Elasticsearch.
Features
- One-to-one mapping with REST API.
- Generalized, pluggable architecture.
- Configurable, automatic discovery of cluster nodes.
- Persistent, Keep-Alive connections.
- Load balancing (with pluggable selection strategy) across all available nodes.
- TypeScript support out of the box.
Install
npm install @elastic/elasticsearch
Compatibility
The minimum supported version of Node.js is v8
.
The library is compatible with all Elasticsearch versions since 5.x, but you should use the same major version of the Elasticsearch instance that you are using.
# Elasticsearch 7.x
@elastic/elasticsearch@7
# Elasticsearch 6.x
@elastic/elasticsearch@6
# Elasticsearch 5.x
@elastic/elasticsearch@5
Install multiple versions
If you are using multiple versions of Elasticsearch, you need to use multiple versions of the client. In the past, install multiple versions of the same package was not possible, but with npm v6.9
, you can do that via aliasing.
The command you must run to install different version of the client is:
npm install <alias>@npm:@elastic/elasticsearch@<version>
So for example if you need to install 7.x
and 6.x
, you will run
npm install es6@npm:@elastic/elasticsearch@6
npm install es7@npm:@elastic/elasticsearch@7
And your package.json
will look like the following:
"dependencies": {
"es6": "npm:@elastic/elasticsearch@^6.7.0",
"es7": "npm:@elastic/elasticsearch@^7.0.0"
}
And finally, you will require the packages from your code by using the alias you have defined.
const { Client: Client6 } = require('es6')
const { Client: Client7 } = require('es7')
const client6 = new Client6({ node: 'http://localhost:9200' })
const client7 = new Client7({ node: 'http://localhost:9201' })
client6.info(console.log)
client7.info(console.log)
Finally, if you want to install the client for the next version of Elasticsearch (the one that lives in Elasticsearch’s master branch), you can use the following command:
npm install esmaster@github:elastic/elasticsearch-js
Usage
You can find the full documentation in our docs website.
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
const result = await client.search({
index: 'my-index',
body: { foo: 'bar' }
})
client.search({
index: 'my-index',
body: { foo: 'bar' }
}, (err, result) => {
if (err) console.log(err)
})
The returned value of every API call is formed as follows:
{
body: object | boolean
statusCode: number
headers: object
warnings: [string]
meta: object
}
Client options
The client is designed to be easily configured as you see fit for your needs, following you can see all the possible options that you can use to configure it.
{
node: string | string[];
nodes: string | string[];
Connection: typeof Connection;
ConnectionPool: typeof ConnectionPool;
Transport: typeof Transport;
Serializer: typeof Serializer;
maxRetries: number;
requestTimeout: number;
pingTimeout: number;
sniffInterval: number;
sniffOnStart: boolean;
sniffEndpoint: string;
sniffOnConnectionFault: boolean;
resurrectStrategy: 'ping' | 'optimistic' | 'none';
suggestCompression: boolean;
compression: 'gzip';
ssl: http.SecureContextOptions;
agent: http.AgentOptions;
nodeFilter: nodeFilterFn;
nodeSelector: nodeSelectorFn | string;
generateRequestId: generateRequestIdFn;
name: string;
}
Request specific options
If needed you can pass request specific options in a second object:
const result = await client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
ignore: [404],
maxRetries: 3
})
The supported request specific options are:
{
ignore: [number],
requestTimeout: number,
maxRetries: number,
asStream: boolean,
compression: string,
headers: object,
querystring: object
context: object
id: any
}
License
This software is licensed under the Apache 2 license.