You are viewing docs on Elastic's new documentation system, currently in technical preview. For all other Elastic docs, visit elastic.co/guide.

Get started with the serverless Node.js client

Set up and use the Node.js client for Elasticsearch on serverless.

This page guides you through the installation process of the Node.js client for Elasticsearch on serverless, shows you how to initialize the client, and how to perform basic Elasticsearch operations with it.

Requirements

  • Node.js 16 or higher installed on your system.

Installation

Using the command line

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

npm install @elastic/elasticsearch-serverless

Initialize the client

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

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

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

You can get documents by using the following code:

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

Searching

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

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

You can call the delete API to delete a document:

await client.delete({
  index: 'books',
  id: 'a_document_id',
})

Deleting an index

await client.indices.delete({ index: 'books' })

TypeScript

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.

On this page