From 0efc3c3e3ec439e1ef19621360cf0a8cb55d83d8 Mon Sep 17 00:00:00 2001 From: Ronit Agarwala Date: Wed, 6 Dec 2023 21:53:36 -0500 Subject: [PATCH] Update README. Add section for post-deployment search API calls. --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index ffb1086..c5ba681 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,54 @@ const client = await search() Now `client` is an instance of the [OpenSearch JavaScript client](https://opensearch.org/docs/latest/clients/javascript/index/), and you can call any client method on it — for example, [`client.index.create()`](https://opensearch.org/docs/latest/clients/javascript/index/#creating-an-index), [`client.index()`](https://opensearch.org/docs/latest/clients/javascript/index/#indexing-a-document), or [`client.search()`](https://opensearch.org/docs/latest/clients/javascript/index/#searching-for-documents). +## Making post-deployment requests to OpenSearch or ElasticSearch from your application + +If you would like to make requests to automatically configure your OpenSearch or ElasticSearch instance after deployment, you may optionally add a postdeploy-search.js file in the root directory of your Architect project. This file should export a function that takes no arguments as its default export. Be sure to connect with your OpenSearch or ElasticSearch instance before making your requests. + +Here's an sample postdeploy-search.js file for making requests to OpenSearch: + +```ts +import { search } from '@nasa-gcn/architect-functions-search' + +export default async function () { + const client = await search() + + //Set cluster settings + const cluster_settings_request = { + method: 'PUT', + path: '/_cluster/settings', + body: { + persistent: { + plugins: { + ml_commons: { + only_run_on_ml_node: 'false', + model_access_control_enabled: 'true', + native_memory_threshold: '99', + }, + }, + }, + }, + } + + try { + const resp = await client.transport.request(cluster_settings_request) + + if (resp && resp.statusCode == 200) { + console.log('Updated ML-related cluster settings.') + } else { + console.log( + 'Error. Could not update cluster settings. Returned with response: ', + resp + ) + return + } + } catch (e) { + console.log('Error: ', e) + return + } +} +``` + ## Advanced usage from your application If you would like to manually connect to OpenSearch from your application, then you will need to sign your requests using [AWS SIG4](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html); the OpenSearch client library provides the [`AwsSigv4Signer`](https://opensearch.org/docs/latest/clients/javascript/index/#authenticating-with-amazon-opensearch-service--aws-sigv4) helper to automate this.