From 0856c622bf7fe2060fe224b7d9030fbb754d3b14 Mon Sep 17 00:00:00 2001 From: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:12:07 -0500 Subject: [PATCH] Update javascript client sample program and Vale rules (#6435) Signed-off-by: Fanit Kolchina --- .../OpenSearch/LinksDoubleParentheses.yml | 3 +- .../styles/OpenSearch/LinksDoubleSlash.yml | 2 +- _clients/javascript/index.md | 86 ++++++++++++++++--- _query-dsl/term-vs-full-text.md | 2 +- 4 files changed, 78 insertions(+), 15 deletions(-) diff --git a/.github/vale/styles/OpenSearch/LinksDoubleParentheses.yml b/.github/vale/styles/OpenSearch/LinksDoubleParentheses.yml index d22a3238..bc4f3aed 100644 --- a/.github/vale/styles/OpenSearch/LinksDoubleParentheses.yml +++ b/.github/vale/styles/OpenSearch/LinksDoubleParentheses.yml @@ -5,5 +5,4 @@ nonword: true scope: raw tokens: - '\]\({2,}[^)]*?\){1,}' - - '\]\([^]]*?\){2,}' - + \ No newline at end of file diff --git a/.github/vale/styles/OpenSearch/LinksDoubleSlash.yml b/.github/vale/styles/OpenSearch/LinksDoubleSlash.yml index 369db6a1..528fb73a 100644 --- a/.github/vale/styles/OpenSearch/LinksDoubleSlash.yml +++ b/.github/vale/styles/OpenSearch/LinksDoubleSlash.yml @@ -4,4 +4,4 @@ level: error nonword: true scope: raw tokens: - - '\(\{\{site.url\}\}\{\{site.baseurl\}\}.*\/{2,}.*\)' + - '\(\{\{site.url\}\}\{\{site.baseurl\}\}[^)]*?\/{2,}[^)]*?\)' diff --git a/_clients/javascript/index.md b/_clients/javascript/index.md index e2bcc2b3..58e9f190 100644 --- a/_clients/javascript/index.md +++ b/_clients/javascript/index.md @@ -17,7 +17,7 @@ You can use helper methods to simplify the use of complicated API tasks. For mor ## Setup -To add the client to your project, install it from [npm](https://www.npmjs.com): +To add the client to your project, install it from [`npm`](https://www.npmjs.com): ```bash npm install @opensearch-project/opensearch @@ -31,7 +31,7 @@ npm install @opensearch-project/opensearch@ ``` {% include copy.html %} -If you prefer to add the client manually or just want to examine the source code, see [opensearch-js](https://github.com/opensearch-project/opensearch-js) on GitHub. +If you prefer to add the client manually or only want to examine the source code, see [`opensearch-js`](https://github.com/opensearch-project/opensearch-js) on GitHub. Then require the client: @@ -71,7 +71,22 @@ var client = new Client({ ``` {% include copy.html %} -## Authenticating with Amazon OpenSearch Service – AWS Sigv4 +If you are not using the Security plugin, create a client object with the address `http://localhost:9200`: + +```javascript +var host = "localhost"; +var protocol = "http"; +var port = 9200; + +// Create a client +var { Client } = require("@opensearch-project/opensearch"); +var client = new Client({ + node: protocol + "://" + host + ":" + port +}); +``` +{% include copy.html %} + +## Authenticating with Amazon OpenSearch Service: AWS Signature Version 4 Use the following code to authenticate with AWS V2 SDK: @@ -345,7 +360,23 @@ var response = await client.update({ } }); ``` +{% include copy.html %} +For example, the following code updates the `genre` field and adds a `tv_adapted` field to the document specified by `id`: + +```javascript +var response = await client.update({ + index: index_name, + id: id, + body: { + doc: { + genre: "Detective fiction", + tv_adapted: true + } + }, + refresh: true + }); +``` {% include copy.html %} ## Deleting a document @@ -384,11 +415,11 @@ var port = 9200; var auth = "admin:"; // For testing only. Don't store credentials in code. var ca_certs_path = "/full/path/to/root-ca.pem"; -// Optional client certificates if you don't want to use HTTP basic authentication. +// Optional client certificates if you don't want to use HTTP basic authentication // var client_cert_path = '/full/path/to/client.pem' // var client_key_path = '/full/path/to/client-key.pem' -// Create a client with SSL/TLS enabled. +// Create a client with SSL/TLS enabled var { Client } = require("@opensearch-project/opensearch"); var fs = require("fs"); var client = new Client({ @@ -403,7 +434,7 @@ var client = new Client({ }); async function search() { - // Create an index with non-default settings. + // Create an index with non-default settings var index_name = "books"; var settings = { @@ -423,7 +454,7 @@ async function search() { console.log("Creating index:"); console.log(response.body); - // Add a document to the index. + // Add a document to the index var document = { title: "The Outsider", author: "Stephen King", @@ -443,7 +474,7 @@ async function search() { console.log("Adding document:"); console.log(response.body); - // Search for the document. + // Search for the document var query = { query: { match: { @@ -460,9 +491,41 @@ async function search() { }); console.log("Search results:"); - console.log(response.body.hits); + console.log(JSON.stringify(response.body.hits, null, " ")); - // Delete the document. + // Update a document + var response = await client.update({ + index: index_name, + id: id, + body: { + doc: { + genre: "Detective fiction", + tv_adapted: true + } + }, + refresh: true + }); + + // Search for the updated document + var query = { + query: { + match: { + title: { + query: "The Outsider", + }, + }, + }, + }; + + var response = await client.search({ + index: index_name, + body: query, + }); + + console.log("Search results:"); + console.log(JSON.stringify(response.body.hits, null, " ")); + + // Delete the document var response = await client.delete({ index: index_name, id: id, @@ -471,7 +534,7 @@ async function search() { console.log("Deleting document:"); console.log(response.body); - // Delete the index. + // Delete the index var response = await client.indices.delete({ index: index_name, }); @@ -483,6 +546,7 @@ async function search() { search().catch(console.log); ``` {% include copy.html %} + ## Circuit breaker The `memoryCircuitBreaker` option can be used to prevent errors caused by a response payload being too large to fit into the heap memory available to the client. diff --git a/_query-dsl/term-vs-full-text.md b/_query-dsl/term-vs-full-text.md index cbf5368f..e5019c4e 100644 --- a/_query-dsl/term-vs-full-text.md +++ b/_query-dsl/term-vs-full-text.md @@ -8,7 +8,7 @@ redirect_from: # Term-level and full-text queries compared -You can use both term-level and full-text queries to search text, but while term-level queries are usually used to search structured data, full-text queries are used for full-text search. The main difference between term-level and full-text queries is that term-level queries search documents for an exact specified term, while full-text queries [analyze]({{{site.url}}{{site.baseurl}}/analyzers/) the query string. The following table summarizes the differences between term-level and full-text queries. +You can use both term-level and full-text queries to search text, but while term-level queries are usually used to search structured data, full-text queries are used for full-text search. The main difference between term-level and full-text queries is that term-level queries search documents for an exact specified term, while full-text queries [analyze]({{site.url}}{{site.baseurl}}/analyzers/) the query string. The following table summarizes the differences between term-level and full-text queries. | | Term-level queries | Full-text queries :--- | :--- | :---