Get started with the serverless Node.js client

edit

Get started with the serverless Node.js client

edit

[preview] This functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.

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
edit
Using the command line
edit

You can install the Node.js client with the following commands:

npm install @elastic/elasticsearch-serverless
Initialize the client
edit

Initialize 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 or the URL for a project, see Get started.

Using the API
edit

After 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
edit

You 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
edit

You can get documents by using the following code:

await client.get({
  index: 'books',
  id: 'a_document_id',
})
Searching
edit

Now 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
edit

You 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
edit

You can call the delete API to delete a document:

await client.delete({
  index: 'books',
  id: 'a_document_id',
})
Deleting an index
edit
await client.indices.delete({ index: 'books' })
TypeScript
edit

The 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.