Get started with the serverless Node.js client
editGet started with the serverless Node.js client
editThis client is for use with Elasticsearch Serverless only. See also the Elasticsearch clients.
This page guides you through the installation process of the Node.js client for Elasticsearch Serverless, shows you how to initialize the client, and how to perform basic Elasticsearch operations with it.
Requirements
edit- Node.js 16 or higher installed on your system.
Installation
editUsing the command line
editYou can install the Node.js client with the following commands:
npm install @elastic/elasticsearch-serverless
Initialize the client
editInitialize the client using your API key and Elasticsearch endpoint:
const { Client } = require('@elastic/elasticsearch-serverless') const client = new Client({ node: 'https://', // serverless project URL auth: { apiKey: 'your_api_key' }, // project API key })
To get API keys for the URL for a project, see Get started.
Using the API
editAfter you’ve initialized the client, you can start ingesting documents.
You can use the bulk
API for this.
This API enables you to index, update, and delete several documents in one request.
Creating an index and ingesting documents
editYou can call the bulk
helper API with a list of documents and a handler for
what action to perform on each document.
The following is an example of bulk indexing some classic books into the books
index:
// First we build our data: const body = [ {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470}, {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}, {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}, {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}, {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}, {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} ] // Then we send the data using the bulk API helper: const result = await client.helpers.bulk({ datasource: body, onDocument (doc) { // instructs the bulk indexer to add each item in `body` to the books index // you can optionally inspect each `doc` object to alter what action is performed per document return { index: { _index: 'books' } } } })
Getting documents
editYou can get documents by using the following code:
await client.get({ index: 'books', id: 'a_document_id', })
Searching
editNow that some data is available, you can search your documents using the search
API:
const result = await client.search({ index: 'books', query: { match: { author: 'ray bradbury' } } }) console.log(result.hits.hits)
Updating a document
editYou can call the update
API to update a document:
await client.update({ index: 'books', id: 'a_document_id', doc: { author: 'S.E. Hinton', new_field: 'new value' } })
Deleting a document
editYou can call the delete
API to delete a document:
await client.delete({ index: 'books', id: 'a_document_id', })
Deleting an index
editawait client.indices.delete({ index: 'books' })
TypeScript
editThe Node.js client is implemented in TypeScript. IDEs that support
TypeScript-based autocompletion should automatically find and load the
appropriate declaration files in the package’s lib
directory.
The source TypeScript can also be
viewed on GitHub.