Update client name
This commit is contained in:
parent
18d3891879
commit
2d228a86ca
|
@ -10,19 +10,18 @@ The OpenSearch Javascript client provides a safer and easier way to interact wit
|
||||||
|
|
||||||
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.
|
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
|
## Setup
|
||||||
|
|
||||||
To add the client to your project, install it from npm:
|
To add the client to your project, install it from npm:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @opensearch/opensearch
|
npm install @opensearch-project/opensearch
|
||||||
```
|
```
|
||||||
|
|
||||||
To install a specific major version of the client, run the following command:
|
To install a specific major version of the client, run the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @opensearch/opensearch@<version>
|
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](https://github.com/opensearch-project/opensearch-js) on GitHub.
|
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.
|
||||||
|
@ -30,115 +29,113 @@ If you prefer to add the client manually or just want to examine the source code
|
||||||
Then require the client:
|
Then require the client:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const { Client } = require('@opensearch/opensearch')
|
const { Client } = require("@opensearch-project/opensearch");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Sample code
|
## Sample code
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
var host = 'localhost';
|
var host = "localhost";
|
||||||
var protocol = 'https';
|
var protocol = "https";
|
||||||
var port = 9200;
|
var port = 9200;
|
||||||
var auth = 'admin:admin'; // For testing only. Don't store credentials in code.
|
var auth = "admin:admin"; // For testing only. Don't store credentials in code.
|
||||||
var ca_certs_path = '/full/path/to/root-ca.pem';
|
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_cert_path = '/full/path/to/client.pem'
|
||||||
// var client_key_path = '/full/path/to/client-key.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/opensearch');
|
var { Client } = require("@opensearch/opensearch");
|
||||||
var fs = require('fs');
|
var fs = require("fs");
|
||||||
var client = new Client({
|
var client = new Client({
|
||||||
node: protocol + '://' + auth + '@' + host + ':' + port,
|
node: protocol + "://" + auth + "@" + host + ":" + port,
|
||||||
ssl: {
|
ssl: {
|
||||||
ca: fs.readFileSync(ca_certs_path),
|
ca: fs.readFileSync(ca_certs_path),
|
||||||
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
|
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
|
||||||
// cert: fs.readFileSync(client_cert_path),
|
// cert: fs.readFileSync(client_cert_path),
|
||||||
// key: fs.readFileSync(client_key_path)
|
// key: fs.readFileSync(client_key_path)
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
async function search() {
|
async function search() {
|
||||||
|
|
||||||
// Create an index with non-default settings.
|
// Create an index with non-default settings.
|
||||||
var index_name = 'books'
|
var index_name = "books";
|
||||||
var settings = {
|
var settings = {
|
||||||
'settings': {
|
settings: {
|
||||||
'index': {
|
index: {
|
||||||
'number_of_shards': 4,
|
number_of_shards: 4,
|
||||||
'number_of_replicas': 3
|
number_of_replicas: 3,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
var response = await client.indices.create({
|
var response = await client.indices.create({
|
||||||
index: index_name,
|
index: index_name,
|
||||||
body: settings
|
body: settings,
|
||||||
})
|
});
|
||||||
|
|
||||||
console.log('Creating index:')
|
console.log("Creating index:");
|
||||||
console.log(response.body)
|
console.log(response.body);
|
||||||
|
|
||||||
// Add a document to the index.
|
// Add a document to the index.
|
||||||
var document = {
|
var document = {
|
||||||
'title': 'The Outsider',
|
title: "The Outsider",
|
||||||
'author': 'Stephen King',
|
author: "Stephen King",
|
||||||
'year': '2018',
|
year: "2018",
|
||||||
'genre': 'Crime fiction'
|
genre: "Crime fiction",
|
||||||
}
|
};
|
||||||
|
|
||||||
var id = '1'
|
var id = "1";
|
||||||
|
|
||||||
var response = await client.index({
|
var response = await client.index({
|
||||||
id: id,
|
id: id,
|
||||||
index: index_name,
|
index: index_name,
|
||||||
body: document,
|
body: document,
|
||||||
refresh: true
|
refresh: true,
|
||||||
})
|
});
|
||||||
|
|
||||||
console.log('Adding document:')
|
console.log("Adding document:");
|
||||||
console.log(response.body)
|
console.log(response.body);
|
||||||
|
|
||||||
// Search for the document.
|
// Search for the document.
|
||||||
var query = {
|
var query = {
|
||||||
'query': {
|
query: {
|
||||||
'match': {
|
match: {
|
||||||
'title': {
|
title: {
|
||||||
'query': 'The Outsider'
|
query: "The Outsider",
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
var response = await client.search({
|
var response = await client.search({
|
||||||
index: index_name,
|
index: index_name,
|
||||||
body: query
|
body: query,
|
||||||
})
|
});
|
||||||
|
|
||||||
console.log('Search results:')
|
console.log("Search results:");
|
||||||
console.log(response.body.hits)
|
console.log(response.body.hits);
|
||||||
|
|
||||||
// Delete the document.
|
// Delete the document.
|
||||||
var response = await client.delete({
|
var response = await client.delete({
|
||||||
index: index_name,
|
index: index_name,
|
||||||
id: id
|
id: id,
|
||||||
})
|
});
|
||||||
|
|
||||||
console.log('Deleting document:')
|
console.log("Deleting document:");
|
||||||
console.log(response.body)
|
console.log(response.body);
|
||||||
|
|
||||||
// Delete the index.
|
// Delete the index.
|
||||||
var response = await client.indices.delete({
|
var response = await client.indices.delete({
|
||||||
index: index_name
|
index: index_name,
|
||||||
})
|
});
|
||||||
|
|
||||||
console.log('Deleting index:')
|
console.log("Deleting index:");
|
||||||
console.log(response.body)
|
console.log(response.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
search().catch(console.log)
|
search().catch(console.log);
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue