- Elastic Cloud Serverless
- Elasticsearch
- Elastic Observability
- Get started
- Observability overview
- Elastic Observability Serverless billing dimensions
- Create an Observability project
- Quickstart: Monitor hosts with Elastic Agent
- Quickstart: Monitor your Kubernetes cluster with Elastic Agent
- Quickstart: Monitor hosts with OpenTelemetry
- Quickstart: Unified Kubernetes Observability with Elastic Distributions of OpenTelemetry (EDOT)
- Quickstart: Collect data with AWS Firehose
- Get started with dashboards
- Applications and services
- Application performance monitoring (APM)
- Get started with traces and APM
- Learn about data types
- Collect application data
- View and analyze data
- Act on data
- Use APM securely
- Reduce storage
- Managed intake service event API
- Troubleshooting
- Synthetic monitoring
- Get started
- Scripting browser monitors
- Configure lightweight monitors
- Manage monitors
- Work with params and secrets
- Analyze monitor data
- Monitor resources on private networks
- Use the CLI
- Configure a Synthetics project
- Multifactor Authentication for browser monitors
- Configure Synthetics settings
- Grant users access to secured resources
- Manage data retention
- Scale and architect a deployment
- Synthetics Encryption and Security
- Troubleshooting
- Application performance monitoring (APM)
- Infrastructure and hosts
- Logs
- Inventory
- Incident management
- Data set quality
- Observability AI Assistant
- Machine learning
- Reference
- Get started
- Elastic Security
- Elastic Security overview
- Security billing dimensions
- Create a Security project
- Elastic Security requirements
- Elastic Security UI
- AI for Security
- Ingest data
- Configure endpoint protection with Elastic Defend
- Manage Elastic Defend
- Endpoints
- Policies
- Trusted applications
- Event filters
- Host isolation exceptions
- Blocklist
- Optimize Elastic Defend
- Event capture and Elastic Defend
- Endpoint protection rules
- Identify antivirus software on your hosts
- Allowlist Elastic Endpoint in third-party antivirus apps
- Elastic Endpoint self-protection features
- Elastic Endpoint command reference
- Endpoint response actions
- Cloud Security
- Explore your data
- Dashboards
- Detection engine overview
- Rules
- Alerts
- Advanced Entity Analytics
- Investigation tools
- Asset management
- Manage settings
- Troubleshooting
- Manage your project
- Changelog
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.
On this page