What is @opensearch-project/opensearch?
@opensearch-project/opensearch is an official client for OpenSearch, a community-driven, open-source search and analytics suite derived from Elasticsearch. This package allows developers to interact with OpenSearch clusters, perform CRUD operations, search, and manage indices, among other functionalities.
What are @opensearch-project/opensearch's main functionalities?
Connecting to an OpenSearch Cluster
This code demonstrates how to create a client instance to connect to an OpenSearch cluster running on localhost.
const { Client } = require('@opensearch-project/opensearch');
const client = new Client({ node: 'http://localhost:9200' });
Indexing Documents
This code sample shows how to index a document into an OpenSearch index named 'my-index'.
async function indexDocument() {
const response = await client.index({
index: 'my-index',
id: '1',
body: {
title: 'Test Document',
content: 'This is a test document.'
}
});
console.log(response);
}
indexDocument();
Searching Documents
This code demonstrates how to search for documents in an OpenSearch index using a match query.
async function searchDocuments() {
const response = await client.search({
index: 'my-index',
body: {
query: {
match: { title: 'Test' }
}
}
});
console.log(response.hits.hits);
}
searchDocuments();
Managing Indices
This code sample shows how to create a new index in OpenSearch.
async function createIndex() {
const response = await client.indices.create({
index: 'my-new-index'
});
console.log(response);
}
createIndex();
Other packages similar to @opensearch-project/opensearch
elasticsearch
The 'elasticsearch' npm package is the official client for Elasticsearch, a distributed search and analytics engine. It provides similar functionalities to @opensearch-project/opensearch, such as connecting to clusters, indexing documents, searching, and managing indices. However, it is tailored specifically for Elasticsearch clusters.
meilisearch
The 'meilisearch' npm package is a client for MeiliSearch, an open-source, easy-to-use, and fast search engine. While it offers similar functionalities like indexing and searching documents, MeiliSearch focuses on simplicity and speed, making it a good alternative for smaller projects or those requiring quick setup.
typesense
The 'typesense' npm package is a client for Typesense, an open-source, typo-tolerant search engine optimized for instant search experiences. It provides functionalities for indexing and searching documents, similar to @opensearch-project/opensearch, but is designed to offer a more user-friendly and developer-friendly experience.
OpenSearch Node.js client
Welcome!
opensearch-js is a community-driven, open source fork of elasticsearch-js licensed under the Apache v2.0 License. For more information, see opensearch.org.
Example use
The OpenSearch JavaScript client provides a safer and easier way to interact with your OpenSearch cluster. Rather than using OpenSearch from the browser and potentially exposing your data to the public, you can build an OpenSearch client that takes care of sending requests to your cluster.
The client contains a library of APIs that let you perform different operations on your cluster and return a standard response body. The example here demonstrates some basic operations like creating an index, adding documents, and searching your data.
Setup
To add the client to your project, install it with npm:
npm i @opensearch-project/opensearch
If you prefer to add the client manually or just want to examine the source code, see opensearch-js on GitHub.
Then require the client:
const { Client } = require('@opensearch-project/opensearch')
Sample code
'use strict';
var host = 'localhost';
var protocol = 'https';
var port = 9200;
var auth = 'admin:admin';
var ca_certs_path = '/full/path/to/root-ca.pem';
var { Client } = require('@opensearch-project/opensearch');
var fs = require('fs');
var client = new Client({
node: protocol + '://' + auth + '@' + host + ':' + port,
ssl: {
ca: fs.readFileSync(ca_certs_path),
}
})
async function search() {
var index_name = 'books'
var settings = {
'settings': {
'index': {
'number_of_shards': 4,
'number_of_replicas': 3
}
}
}
var response = await client.indices.create({
index: index_name,
body: settings
})
console.log('Creating index:')
console.log(response.body)
var document = {
'title': 'The Outsider',
'author': 'Stephen King',
'year': '2018',
'genre': 'Crime fiction'
}
var id = '1'
var response = await client.index({
id: id,
index: index_name,
body: document,
refresh: true
})
console.log('Adding document:')
console.log(response.body)
var query = {
'query': {
'match': {
'title': {
'query': 'The Outsider'
}
}
}
}
var response = await client.search({
index: index_name,
body: query
})
console.log('Search results:')
console.log(response.body.hits)
var response = await client.delete({
index: index_name,
id: id
})
console.log('Deleting document:')
console.log(response.body)
var response = await client.indices.delete({
index: index_name
})
console.log('Deleting index:')
console.log(response.body)
}
search().catch(console.log)
Project Resources
Code of Conduct
This project has adopted the Amazon Open Source Code of Conduct. For more information see the Code of Conduct FAQ, or contact opensource-codeofconduct@amazon.com with any additional questions or comments.
License
This project is licensed under the Apache v2.0 License.
Copyright
Copyright OpenSearch Contributors. See NOTICE for details.