JavaScript client

The OpenSearch JavaScript (JS) 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 from npm:

  1. npm install @opensearch-project/opensearch

To install a specific major version of the client, run the following command:

  1. npm install @opensearch-project/opensearch@<version>

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:

  1. const { Client } = require("@opensearch-project/opensearch");

Sample code

  1. "use strict";
  2. var host = "localhost";
  3. var protocol = "https";
  4. var port = 9200;
  5. var auth = "admin:admin"; // For testing only. Don't store credentials in code.
  6. var ca_certs_path = "/full/path/to/root-ca.pem";
  7. // Optional client certificates if you don't want to use HTTP basic authentication.
  8. // var client_cert_path = '/full/path/to/client.pem'
  9. // var client_key_path = '/full/path/to/client-key.pem'
  10. // Create a client with SSL/TLS enabled.
  11. var { Client } = require("@opensearch-project/opensearch");
  12. var fs = require("fs");
  13. var client = new Client({
  14. node: protocol + "://" + auth + "@" + host + ":" + port,
  15. ssl: {
  16. ca: fs.readFileSync(ca_certs_path),
  17. // You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
  18. // cert: fs.readFileSync(client_cert_path),
  19. // key: fs.readFileSync(client_key_path)
  20. },
  21. });
  22. async function search() {
  23. // Create an index with non-default settings.
  24. var index_name = "books";
  25. var settings = {
  26. settings: {
  27. index: {
  28. number_of_shards: 4,
  29. number_of_replicas: 3,
  30. },
  31. },
  32. };
  33. var response = await client.indices.create({
  34. index: index_name,
  35. body: settings,
  36. });
  37. console.log("Creating index:");
  38. console.log(response.body);
  39. // Add a document to the index.
  40. var document = {
  41. title: "The Outsider",
  42. author: "Stephen King",
  43. year: "2018",
  44. genre: "Crime fiction",
  45. };
  46. var id = "1";
  47. var response = await client.index({
  48. id: id,
  49. index: index_name,
  50. body: document,
  51. refresh: true,
  52. });
  53. console.log("Adding document:");
  54. console.log(response.body);
  55. // Search for the document.
  56. var query = {
  57. query: {
  58. match: {
  59. title: {
  60. query: "The Outsider",
  61. },
  62. },
  63. },
  64. };
  65. var response = await client.search({
  66. index: index_name,
  67. body: query,
  68. });
  69. console.log("Search results:");
  70. console.log(response.body.hits);
  71. // Delete the document.
  72. var response = await client.delete({
  73. index: index_name,
  74. id: id,
  75. });
  76. console.log("Deleting document:");
  77. console.log(response.body);
  78. // Delete the index.
  79. var response = await client.indices.delete({
  80. index: index_name,
  81. });
  82. console.log("Deleting index:");
  83. console.log(response.body);
  84. }
  85. search().catch(console.log);

Circuit Breaker

The memoryCircuitBreaker parameter in the Cluster Settings API gives you the ability to reject large query responses where the size of the response could crash OpenSearch Dashboards. To set the Circuit Breaker setting, use the POST _cluster/settings API operation on your active JS OpenSearch cluster.

memoryCircuitBreaker contains two fields:

  • enabled: A Boolean used to turn the Circuit Breaker on or off. Defaults to false.
  • maxPercentage: The threshold that determines whether the Circuit Breaker engages. The input range must be between [0 ,1]. Any number that exceeds that range will correct to 1.0.

The following example turns on the Circuit Breaker and sets the maximum percentage of a query response to 80% of the cluster’s storage. You can customize this example for use in the POST _cluster/settings request body.

  1. memoryCircuitBreaker: {
  2. enabled: true,
  3. maxPercentage: 0.8
  4. }