@opensearch-project/opensearch 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +112 -8
- package/DEVELOPER_GUIDE.md +55 -5
- package/MAINTAINERS.md +9 -3
- package/RELEASING.md +3 -2
- package/USER_GUIDE.md +21 -2
- package/api/types.d.ts +6 -6
- package/guides/advanced_index_actions.md +91 -0
- package/guides/bulk.md +166 -0
- package/guides/document_lifecycle.md +162 -0
- package/guides/index_lifecycle.md +152 -0
- package/guides/index_template.md +228 -0
- package/guides/search.md +217 -0
- package/lib/Connection.js +1 -1
- package/lib/Serializer.js +242 -3
- package/lib/aws/index.d.ts +1 -0
- package/lib/errors.d.ts +2 -2
- package/lib/errors.js +2 -2
- package/lib/pool/BaseConnectionPool.js +18 -9
- package/lib/pool/CloudConnectionPool.js +2 -2
- package/lib/pool/ConnectionPool.js +4 -4
- package/lib/pool/index.d.ts +35 -6
- package/package.json +6 -6
package/guides/search.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# Search
|
|
2
|
+
OpenSearch provides a powerful search API that allows you to search for documents in an index. The search API supports a number of parameters that allow you to customize the search operation. In this guide, we will explore the search API and its parameters.
|
|
3
|
+
# Setup
|
|
4
|
+
Let's start by creating an index and adding some documents to it:
|
|
5
|
+
```javascript
|
|
6
|
+
var host = "localhost";
|
|
7
|
+
var protocol = "https";
|
|
8
|
+
var port = 9200;
|
|
9
|
+
var auth = "admin:admin"; // For testing only. Don't store credentials in code.
|
|
10
|
+
var ca_certs_path = "/full/path/to/root-ca.pem";
|
|
11
|
+
|
|
12
|
+
// Optional client certificates if you don't want to use HTTP basic authentication.
|
|
13
|
+
// var client_cert_path = '/full/path/to/client.pem'
|
|
14
|
+
// var client_key_path = '/full/path/to/client-key.pem'
|
|
15
|
+
|
|
16
|
+
// Create a client with SSL/TLS enabled.
|
|
17
|
+
var { Client } = require("@opensearch-project/opensearch");
|
|
18
|
+
var fs = require("fs");
|
|
19
|
+
var client = new Client({
|
|
20
|
+
node: protocol + "://" + auth + "@" + host + ":" + port,
|
|
21
|
+
ssl: {
|
|
22
|
+
ca: fs.readFileSync(ca_certs_path),
|
|
23
|
+
// You can turn off certificate verification (rejectUnauthorized: false) if you're using
|
|
24
|
+
// self-signed certificates with a hostname mismatch.
|
|
25
|
+
// cert: fs.readFileSync(client_cert_path),
|
|
26
|
+
// key: fs.readFileSync(client_key_path)
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await client.indices.create({index: 'movies'});
|
|
31
|
+
|
|
32
|
+
for(let i = 0; i < 10; i++) {
|
|
33
|
+
await client.index({
|
|
34
|
+
index: 'movies',
|
|
35
|
+
id: i,
|
|
36
|
+
body: {
|
|
37
|
+
title: `The Dark Knight ${i}`,
|
|
38
|
+
director: 'Christopher Nolan',
|
|
39
|
+
year: 2008 + i
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await client.index({
|
|
45
|
+
index: 'movies',
|
|
46
|
+
body: {
|
|
47
|
+
title: 'The Godfather',
|
|
48
|
+
director: 'Francis Ford Coppola',
|
|
49
|
+
year: 1972
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await client.index({
|
|
54
|
+
index: 'movies',
|
|
55
|
+
body: {
|
|
56
|
+
title: 'The Shawshank Redemption',
|
|
57
|
+
director: 'Frank Darabont',
|
|
58
|
+
year: 1994
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await client.index({index: 'movies', refresh: true});
|
|
63
|
+
```
|
|
64
|
+
# Search API
|
|
65
|
+
### Basic Search
|
|
66
|
+
The search API allows you to search for documents in an index. The following example searches for ALL documents in the movies index:
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
const response = await client.search({
|
|
70
|
+
index: 'movies'
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
You can also search for documents that match a specific query. The following example searches for documents that match the query `dark knight`:
|
|
74
|
+
```javascript
|
|
75
|
+
const response = await client.search({
|
|
76
|
+
index: 'movies',
|
|
77
|
+
body: {
|
|
78
|
+
query: {
|
|
79
|
+
match: {
|
|
80
|
+
title: 'dark knight'
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
console.log(response.body.hits.hits);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
OpenSearch query DSL allows you to specify complex queries. Check out the [OpenSearch query DSL documentation](https://opensearch.org/docs/latest/query-dsl/) for more information.
|
|
89
|
+
|
|
90
|
+
### Basic Pagination
|
|
91
|
+
The search API allows you to paginate through the search results. The following example searches for documents that match the query `dark knight`, sorted by `year` in ascending order, and returns the first 2 results after skipping the first 5 results:
|
|
92
|
+
```javascript
|
|
93
|
+
search_body = {
|
|
94
|
+
query: {
|
|
95
|
+
match: {
|
|
96
|
+
title: 'dark knight'
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
sort: [
|
|
100
|
+
{
|
|
101
|
+
year: {
|
|
102
|
+
order: 'asc'
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
await client.search({
|
|
109
|
+
index: 'movies',
|
|
110
|
+
size: 2,
|
|
111
|
+
from: 5,
|
|
112
|
+
body: search_body
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
With sorting, you can also use the `search_after` parameter to paginate through the search results. Let's say you have already displayed the first page of results, and you want to display the next page. You can use the `search_after` parameter to paginate through the search results. The following example will demonstrate how to get the first 3 pages of results using the search query of the previous example:
|
|
117
|
+
```javascript
|
|
118
|
+
const page_1 = await client.search({
|
|
119
|
+
index: 'movies',
|
|
120
|
+
size: 2,
|
|
121
|
+
body: search_body
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const page_2 = await client.search({
|
|
125
|
+
index: 'movies',
|
|
126
|
+
size: 2,
|
|
127
|
+
body: {
|
|
128
|
+
...search_body,
|
|
129
|
+
search_after: page_1[page_1.length - 1].sort
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const page_3 = await client.search({
|
|
134
|
+
index: 'movies',
|
|
135
|
+
size: 2,
|
|
136
|
+
body: {
|
|
137
|
+
...search_body,
|
|
138
|
+
search_after: page_2[page_2.length - 1].sort
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
### Pagination with scroll
|
|
143
|
+
When retrieving large amounts of non-real-time data, you can use the `scroll` parameter to paginate through the search results.
|
|
144
|
+
```javascript
|
|
145
|
+
const page_1 = await client.search({
|
|
146
|
+
index: 'movies',
|
|
147
|
+
scroll: '1m',
|
|
148
|
+
size: 2,
|
|
149
|
+
body: search_body
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const page_2 = await client.scroll({
|
|
153
|
+
scroll_id: page_1._scroll_id,
|
|
154
|
+
scroll: '1m'
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const page_3 = await client.scroll({
|
|
158
|
+
scroll_id: page_2._scroll_id,
|
|
159
|
+
scroll: '1m'
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
### Pagination with point in time
|
|
163
|
+
The scroll example above has one weakness: if the index is updated while you are scrolling through the results, they will be paginated inconsistently. To avoid this, you should use the "Point in Time" feature. The following example demonstrates how to use the `point_in_time` and `pit_id` parameters to paginate through the search results:
|
|
164
|
+
```javascript
|
|
165
|
+
/** Create a point in time */
|
|
166
|
+
const pit = client.create_pit({
|
|
167
|
+
index: 'movies',
|
|
168
|
+
keep_alive: '1m'
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/** Include pit info in the search body */
|
|
172
|
+
const pit_search_body = {
|
|
173
|
+
..search_body,
|
|
174
|
+
pit: {
|
|
175
|
+
id: pit.pit_id,
|
|
176
|
+
keep_alive: '1m'
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Get the first 3 pages of results */
|
|
181
|
+
const page_1 = await client.search({
|
|
182
|
+
size: 2,
|
|
183
|
+
body: pit_search_body
|
|
184
|
+
});
|
|
185
|
+
console.log(page_1.body.hits.hits);
|
|
186
|
+
|
|
187
|
+
const page_2 = await client.search({
|
|
188
|
+
size: 2,
|
|
189
|
+
body: {
|
|
190
|
+
...pit_search_body,
|
|
191
|
+
search_after: page_1[page_1.length - 1].sort
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
console.log(page_2.body.hits.hits);
|
|
195
|
+
|
|
196
|
+
const page_3 = await client.search({
|
|
197
|
+
size: 2,
|
|
198
|
+
body: {
|
|
199
|
+
...pit_search_body,
|
|
200
|
+
search_after: page2[page_2.length-1].sort
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
console.log(page_3.body.hits.hits);
|
|
204
|
+
|
|
205
|
+
/** Print out the titles of the first 3 pages of results */
|
|
206
|
+
console.log(page_1.map(hit => hit._source.title));
|
|
207
|
+
console.log(page_2.map(hit => hit._source.title));
|
|
208
|
+
console.log(page_3.map(hit => hit._source.title));
|
|
209
|
+
|
|
210
|
+
/** Delete the point in time */
|
|
211
|
+
client.delete_pit({body: {pit_id: pit.pit_id}});
|
|
212
|
+
```
|
|
213
|
+
Note that a point-in-time is associated with an index or a set of index. So, when performing a search with a point-in-time, you DO NOT specify the index in the search.
|
|
214
|
+
# Cleanup
|
|
215
|
+
```javascript
|
|
216
|
+
await client.indices.delete({index: 'movies'});
|
|
217
|
+
```
|
package/lib/Connection.js
CHANGED
|
@@ -134,7 +134,7 @@ class Connection {
|
|
|
134
134
|
request.once('error', () => {}); // we need to catch the request aborted error
|
|
135
135
|
debug('Request aborted', params);
|
|
136
136
|
this._openRequests--;
|
|
137
|
-
callback(new RequestAbortedError(), null);
|
|
137
|
+
callback(new RequestAbortedError('Request aborted'), null);
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
request.on('response', onResponse);
|
package/lib/Serializer.js
CHANGED
|
@@ -35,6 +35,60 @@ const sjson = require('secure-json-parse');
|
|
|
35
35
|
const { SerializationError, DeserializationError } = require('./errors');
|
|
36
36
|
const kJsonOptions = Symbol('secure json parse options');
|
|
37
37
|
|
|
38
|
+
/* In JavaScript, a `Number` is a 64-bit floating-point value which can store 16 digits. However, the
|
|
39
|
+
* serializer and deserializer will need to cater to numeric values generated by other languages which
|
|
40
|
+
* can have up to 19 digits. Native JSON parser and stringifier, incapable of handling the extra
|
|
41
|
+
* digits, corrupt the values, making them unusable.
|
|
42
|
+
*
|
|
43
|
+
* To work around this limitation, the deserializer converts long sequences of digits into strings and
|
|
44
|
+
* marks them before applying the parser. During the parsing, string values that begin with the mark
|
|
45
|
+
* are converted to `BigInt` values.
|
|
46
|
+
* Similarly, during stringification, the serializer converts `BigInt` values to marked strings and
|
|
47
|
+
* when done, it replaces them with plain numerals.
|
|
48
|
+
*
|
|
49
|
+
* `Number.MAX_SAFE_INTEGER`, 9,007,199,254,740,991, is the largest number that the native methods can
|
|
50
|
+
* parse and stringify, and any numeral greater than that would need to be translated using the
|
|
51
|
+
* workaround; all 17-digits or longer and only tail-end of the 16-digits need translation. It would
|
|
52
|
+
* be unfair to all the 16-digit numbers if the translation applied to `\d{16,}` only to cover the
|
|
53
|
+
* less than 10%. Hence, a RegExp is created to only match numerals too long to be a number.
|
|
54
|
+
*
|
|
55
|
+
* To make the explanation simpler, let's assume that MAX_SAFE_INTEGER is 8921 which has 4 digits.
|
|
56
|
+
* Starting from the right, we take each digit onwards, `[<start>-9]`:
|
|
57
|
+
* 1) 7922 - 7929: 792[2-9]\d{0}
|
|
58
|
+
* 2) 7930 - 7999: 79[3-9]\d{1}
|
|
59
|
+
* 9) 9 + 1 = 10 which results in a rollover; no need to do anything.
|
|
60
|
+
* 8) 9000 - 9999: [9-9]\d{3}
|
|
61
|
+
* Finally we add anything 5 digits or longer: `\d{5,}
|
|
62
|
+
*
|
|
63
|
+
* PS, a better solution would use AST but considering its performance penalty, RegExp is the next
|
|
64
|
+
* the best solution.
|
|
65
|
+
*/
|
|
66
|
+
const isBigIntSupported = typeof BigInt !== 'undefined';
|
|
67
|
+
const maxIntAsString = String(Number.MAX_SAFE_INTEGER);
|
|
68
|
+
const maxIntLength = maxIntAsString.length;
|
|
69
|
+
// Sub-patterns for each digit
|
|
70
|
+
const bigIntMatcherTokens = [`\\d{${maxIntAsString.length + 1},}`];
|
|
71
|
+
for (let i = 0; i < maxIntLength; i++) {
|
|
72
|
+
if (maxIntAsString[i] !== '9') {
|
|
73
|
+
bigIntMatcherTokens.push(
|
|
74
|
+
maxIntAsString.substring(0, i) +
|
|
75
|
+
`[${parseInt(maxIntAsString[i], 10) + 1}-9]` +
|
|
76
|
+
`\\d{${maxIntLength - i - 1}}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* The matcher that looks for `": <numerals>, ...}` and `[..., <numeral>, ...]`
|
|
82
|
+
*
|
|
83
|
+
* The pattern starts by looking for `":` not immediately preceded by a `\`. That should be
|
|
84
|
+
* followed by any of the numeric sub-patterns. A comma, end of an array, end of an object, or
|
|
85
|
+
* the end of the input are the only acceptable elements after it.
|
|
86
|
+
*/
|
|
87
|
+
const bigIntMatcher = new RegExp(
|
|
88
|
+
`((?:\\[|,|(?<!\\\\)"\\s*:)\\s*)(-?(?:${bigIntMatcherTokens.join('|')}))(\\s*)(?=,|}|]|$)`,
|
|
89
|
+
'g'
|
|
90
|
+
);
|
|
91
|
+
|
|
38
92
|
class Serializer {
|
|
39
93
|
constructor(opts = {}) {
|
|
40
94
|
const disable = opts.disablePrototypePoisoningProtection;
|
|
@@ -44,11 +98,173 @@ class Serializer {
|
|
|
44
98
|
};
|
|
45
99
|
}
|
|
46
100
|
|
|
101
|
+
/* The characters with a highly unlikely chance of occurrence in strings, alone or in combination.
|
|
102
|
+
*
|
|
103
|
+
* ToDo: When support for ancient versions of Node.js are dropped, replace with
|
|
104
|
+
* _bigIntMarkChars = ['෴', '߷', '֍'];
|
|
105
|
+
*/
|
|
106
|
+
get _bigIntMarkChars() {
|
|
107
|
+
return ['෴', '߷', '֍'];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* Generates an array of all combinations of `_bigIntMarkChars` with the requested length. */
|
|
111
|
+
_bigIntMarkerCombinations(length = 3) {
|
|
112
|
+
const results = [];
|
|
113
|
+
const arr = this._bigIntMarkChars;
|
|
114
|
+
const arrLength = arr.length;
|
|
115
|
+
const temp = Array(length);
|
|
116
|
+
|
|
117
|
+
(function fill(pos, start) {
|
|
118
|
+
if (pos === length) return results.push(temp.join(''));
|
|
119
|
+
|
|
120
|
+
for (let i = start; i < arrLength; i++) {
|
|
121
|
+
temp[pos] = arr[i];
|
|
122
|
+
fill(pos + 1, i);
|
|
123
|
+
}
|
|
124
|
+
})(0, 0);
|
|
125
|
+
|
|
126
|
+
return results;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* Experiments with different combinations of various lengths, until one is found to not be in
|
|
130
|
+
* the input string.
|
|
131
|
+
*/
|
|
132
|
+
_getSuitableBigIntMarker(json) {
|
|
133
|
+
let bigIntMarker;
|
|
134
|
+
let length = 0;
|
|
135
|
+
do {
|
|
136
|
+
length++;
|
|
137
|
+
this._bigIntMarkerCombinations(length).some((marker) => {
|
|
138
|
+
if (json.indexOf(marker) === -1) {
|
|
139
|
+
bigIntMarker = marker;
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
} while (!bigIntMarker);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
bigIntMarker,
|
|
147
|
+
length,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
_parseWithBigInt(json) {
|
|
152
|
+
const { bigIntMarker, length } = this._getSuitableBigIntMarker(json);
|
|
153
|
+
|
|
154
|
+
let hadException;
|
|
155
|
+
let markedJSON = json.replace(bigIntMatcher, `$1"${bigIntMarker}$2"$3`);
|
|
156
|
+
|
|
157
|
+
/* RegExp cannot replace AST and the process of marking adds quotes. So, any false-positive hit
|
|
158
|
+
* will make the JSON string unparseable.
|
|
159
|
+
*
|
|
160
|
+
* To find those instances, we try to parse and watch for the location of any errors. If an error
|
|
161
|
+
* is caused by the marking, we remove that single marking and try again.
|
|
162
|
+
*/
|
|
163
|
+
do {
|
|
164
|
+
try {
|
|
165
|
+
hadException = false;
|
|
166
|
+
JSON.parse(markedJSON);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
hadException = true;
|
|
169
|
+
/* There are two types of exception objects that can be raised:
|
|
170
|
+
* 1) a proper object with lineNumber and columnNumber which we can use
|
|
171
|
+
* 2) a textual message with the position that we need to parse
|
|
172
|
+
*/
|
|
173
|
+
let { lineNumber, columnNumber } = e;
|
|
174
|
+
if (!lineNumber || !columnNumber) {
|
|
175
|
+
const match =
|
|
176
|
+
// ToDo: When support for ancient versions of Node.js are dropped, replace with
|
|
177
|
+
// e?.message?.match?.()
|
|
178
|
+
e &&
|
|
179
|
+
e.message &&
|
|
180
|
+
typeof e.message.match === 'function' &&
|
|
181
|
+
e.message.match(/^Unexpected token.*at position (\d+)$/);
|
|
182
|
+
if (match) {
|
|
183
|
+
lineNumber = 1;
|
|
184
|
+
// The position is zero-indexed; adding 1 to normalize it for the -2 that comes later
|
|
185
|
+
columnNumber = parseInt(match[1], 10) + 1;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (lineNumber < 1 || columnNumber < 2) {
|
|
190
|
+
// The problem is not with this replacement; just return a failure.
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/* We need to skip e.lineNumber - 1 number of `\n` occurrences.
|
|
195
|
+
* Then, we need to go to e.columnNumber - 2 to look for `"<mark>\d+"`; we need to `-1` to
|
|
196
|
+
* account for the quote but an additional `-1` is needed because columnNumber starts from 1.
|
|
197
|
+
*/
|
|
198
|
+
const re = new RegExp(
|
|
199
|
+
`^((?:.*\\n){${lineNumber - 1}}[^\\n]{${columnNumber - 2}})"${bigIntMarker}(-?\\d+)"`
|
|
200
|
+
);
|
|
201
|
+
if (!re.test(markedJSON)) {
|
|
202
|
+
// The exception is not caused by adding the marker
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// We have found a bad replacement; let's remove it.
|
|
207
|
+
markedJSON = markedJSON.replace(re, '$1$2');
|
|
208
|
+
}
|
|
209
|
+
} while (hadException);
|
|
210
|
+
|
|
211
|
+
const bigIntMarkFinder = new RegExp(`^${bigIntMarker}-?\\d+$`);
|
|
212
|
+
|
|
213
|
+
// Exceptions will trickle up to the caller
|
|
214
|
+
return sjson.parse(
|
|
215
|
+
markedJSON,
|
|
216
|
+
(key, val) =>
|
|
217
|
+
/* Convert marked values to BigInt values.
|
|
218
|
+
* The `startsWith` is purely for performance, to avoid running `test` if not needed.
|
|
219
|
+
*/
|
|
220
|
+
typeof val === 'string' && val.startsWith(bigIntMarker) && bigIntMarkFinder.test(val)
|
|
221
|
+
? BigInt(val.substring(length)) // eslint-disable-line no-undef
|
|
222
|
+
: val,
|
|
223
|
+
this[kJsonOptions]
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
_stringifyWithBigInt(object, candidate) {
|
|
228
|
+
const { bigIntMarker } = this._getSuitableBigIntMarker(candidate);
|
|
229
|
+
|
|
230
|
+
/* The matcher that looks for "<marker><numerals>"
|
|
231
|
+
* Because we have made sure that `bigIntMarker` was never present in the original object, we can
|
|
232
|
+
* carelessly assume every "<marker><numerals>" is due to our marking.
|
|
233
|
+
*/
|
|
234
|
+
const markedBigIntMatcher = new RegExp(`"${bigIntMarker}(-?\\d+)"`, 'g');
|
|
235
|
+
|
|
236
|
+
return (
|
|
237
|
+
JSON.stringify(
|
|
238
|
+
object,
|
|
239
|
+
/* Convert BigInt values to a string and mark them.
|
|
240
|
+
* Can't be bothered with Number values beyond safe values because they are already corrupted.
|
|
241
|
+
*/
|
|
242
|
+
(key, val) => (typeof val === 'bigint' ? `${bigIntMarker}${val.toString()}` : val)
|
|
243
|
+
)
|
|
244
|
+
// Replace marked substrings with just the numerals
|
|
245
|
+
.replace(markedBigIntMatcher, '$1')
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
47
249
|
serialize(object) {
|
|
48
250
|
debug('Serializing', object);
|
|
49
251
|
let json;
|
|
252
|
+
let numeralsAreNumbers = true;
|
|
253
|
+
const checkForBigInts = (key, val) => {
|
|
254
|
+
if (typeof val === 'bigint') {
|
|
255
|
+
numeralsAreNumbers = false;
|
|
256
|
+
// Number() is much faster than parseInt() on BigInt values
|
|
257
|
+
return Number(val);
|
|
258
|
+
}
|
|
259
|
+
return val;
|
|
260
|
+
};
|
|
50
261
|
try {
|
|
51
|
-
json = JSON.stringify(object);
|
|
262
|
+
json = JSON.stringify(object, isBigIntSupported ? checkForBigInts : null);
|
|
263
|
+
|
|
264
|
+
if (isBigIntSupported && !numeralsAreNumbers) {
|
|
265
|
+
const temp = this._stringifyWithBigInt(object, json);
|
|
266
|
+
if (temp) json = temp;
|
|
267
|
+
}
|
|
52
268
|
} catch (err) {
|
|
53
269
|
throw new SerializationError(err.message, object);
|
|
54
270
|
}
|
|
@@ -58,8 +274,31 @@ class Serializer {
|
|
|
58
274
|
deserialize(json) {
|
|
59
275
|
debug('Deserializing', json);
|
|
60
276
|
let object;
|
|
277
|
+
let numeralsAreNumbers = true;
|
|
278
|
+
const checkForLargeNumerals = (key, val) => {
|
|
279
|
+
if (
|
|
280
|
+
numeralsAreNumbers &&
|
|
281
|
+
typeof val === 'number' &&
|
|
282
|
+
(val < Number.MAX_SAFE_INTEGER || val > Number.MAX_SAFE_INTEGER)
|
|
283
|
+
) {
|
|
284
|
+
numeralsAreNumbers = false;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return val;
|
|
288
|
+
};
|
|
61
289
|
try {
|
|
62
|
-
object = sjson.parse(
|
|
290
|
+
object = sjson.parse(
|
|
291
|
+
json,
|
|
292
|
+
isBigIntSupported ? checkForLargeNumerals : null,
|
|
293
|
+
this[kJsonOptions]
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
if (isBigIntSupported && !numeralsAreNumbers) {
|
|
297
|
+
const temp = this._parseWithBigInt(json);
|
|
298
|
+
if (temp) {
|
|
299
|
+
object = temp;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
63
302
|
} catch (err) {
|
|
64
303
|
throw new DeserializationError(err.message, json);
|
|
65
304
|
}
|
|
@@ -69,7 +308,7 @@ class Serializer {
|
|
|
69
308
|
ndserialize(array) {
|
|
70
309
|
debug('ndserialize', array);
|
|
71
310
|
if (Array.isArray(array) === false) {
|
|
72
|
-
throw new SerializationError('The argument provided is not an array');
|
|
311
|
+
throw new SerializationError('The argument provided is not an array', array);
|
|
73
312
|
}
|
|
74
313
|
let ndjson = '';
|
|
75
314
|
for (let i = 0, len = array.length; i < len; i++) {
|
package/lib/aws/index.d.ts
CHANGED
package/lib/errors.d.ts
CHANGED
|
@@ -51,7 +51,7 @@ export declare class ConnectionError<
|
|
|
51
51
|
name: string;
|
|
52
52
|
message: string;
|
|
53
53
|
meta: ApiResponse<TResponse, TContext>;
|
|
54
|
-
constructor(message: string, meta
|
|
54
|
+
constructor(message: string, meta?: ApiResponse);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export declare class NoLivingConnectionsError<
|
|
@@ -104,7 +104,7 @@ export declare class RequestAbortedError<
|
|
|
104
104
|
name: string;
|
|
105
105
|
message: string;
|
|
106
106
|
meta: ApiResponse<TResponse, TContext>;
|
|
107
|
-
constructor(message: string, meta
|
|
107
|
+
constructor(message: string, meta?: ApiResponse);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
export declare class NotCompatibleError<
|
package/lib/errors.js
CHANGED
|
@@ -70,7 +70,7 @@ class NoLivingConnectionsError extends OpenSearchClientError {
|
|
|
70
70
|
|
|
71
71
|
class SerializationError extends OpenSearchClientError {
|
|
72
72
|
constructor(message, data) {
|
|
73
|
-
super(message
|
|
73
|
+
super(message);
|
|
74
74
|
Error.captureStackTrace(this, SerializationError);
|
|
75
75
|
this.name = 'SerializationError';
|
|
76
76
|
this.message = message || 'Serialization Error';
|
|
@@ -80,7 +80,7 @@ class SerializationError extends OpenSearchClientError {
|
|
|
80
80
|
|
|
81
81
|
class DeserializationError extends OpenSearchClientError {
|
|
82
82
|
constructor(message, data) {
|
|
83
|
-
super(message
|
|
83
|
+
super(message);
|
|
84
84
|
Error.captureStackTrace(this, DeserializationError);
|
|
85
85
|
this.name = 'DeserializationError';
|
|
86
86
|
this.message = message || 'Deserialization Error';
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
const { URL } = require('url');
|
|
33
33
|
const debug = require('debug')('opensearch');
|
|
34
34
|
const Connection = require('../Connection');
|
|
35
|
+
const { ConfigurationError } = require('../errors');
|
|
35
36
|
const noop = () => {};
|
|
36
37
|
|
|
37
38
|
class BaseConnectionPool {
|
|
@@ -64,6 +65,9 @@ class BaseConnectionPool {
|
|
|
64
65
|
* Creates a new connection instance.
|
|
65
66
|
*/
|
|
66
67
|
createConnection(opts) {
|
|
68
|
+
if (opts instanceof Connection) {
|
|
69
|
+
throw new ConfigurationError('The argument provided is already a Connection instance.');
|
|
70
|
+
}
|
|
67
71
|
if (typeof opts === 'string') {
|
|
68
72
|
opts = this.urlToHost(opts);
|
|
69
73
|
}
|
|
@@ -102,18 +106,25 @@ class BaseConnectionPool {
|
|
|
102
106
|
*/
|
|
103
107
|
addConnection(opts) {
|
|
104
108
|
if (Array.isArray(opts)) {
|
|
105
|
-
|
|
109
|
+
opts.forEach((o) => this.addConnection(o));
|
|
110
|
+
return;
|
|
106
111
|
}
|
|
107
|
-
|
|
108
112
|
if (typeof opts === 'string') {
|
|
109
113
|
opts = this.urlToHost(opts);
|
|
110
114
|
}
|
|
111
115
|
|
|
112
|
-
const
|
|
113
|
-
const
|
|
116
|
+
const connectionId = opts.id;
|
|
117
|
+
const connectionUrl = opts.url.href;
|
|
118
|
+
|
|
119
|
+
if (connectionId || connectionUrl) {
|
|
120
|
+
const connectionById = this.connections.find((c) => c.id === connectionId);
|
|
121
|
+
const connectionByUrl = this.connections.find((c) => c.id === connectionUrl);
|
|
114
122
|
|
|
115
|
-
|
|
116
|
-
|
|
123
|
+
if (connectionById || connectionByUrl) {
|
|
124
|
+
throw new ConfigurationError(
|
|
125
|
+
`Connection with id '${connectionId || connectionUrl}' is already present`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
117
128
|
}
|
|
118
129
|
|
|
119
130
|
this.update([...this.connections, opts]);
|
|
@@ -133,10 +144,8 @@ class BaseConnectionPool {
|
|
|
133
144
|
|
|
134
145
|
/**
|
|
135
146
|
* Empties the connection pool.
|
|
136
|
-
*
|
|
137
|
-
* @returns {ConnectionPool}
|
|
138
147
|
*/
|
|
139
|
-
empty(callback) {
|
|
148
|
+
empty(callback = noop) {
|
|
140
149
|
debug('Emptying the connection pool');
|
|
141
150
|
let openConnections = this.size;
|
|
142
151
|
this.connections.forEach((connection) => {
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
'use strict';
|
|
31
31
|
|
|
32
32
|
const BaseConnectionPool = require('./BaseConnectionPool');
|
|
33
|
+
const noop = () => {};
|
|
33
34
|
|
|
34
35
|
class CloudConnectionPool extends BaseConnectionPool {
|
|
35
36
|
constructor(opts) {
|
|
@@ -49,9 +50,8 @@ class CloudConnectionPool extends BaseConnectionPool {
|
|
|
49
50
|
/**
|
|
50
51
|
* Empties the connection pool.
|
|
51
52
|
*
|
|
52
|
-
* @returns {ConnectionPool}
|
|
53
53
|
*/
|
|
54
|
-
empty(callback) {
|
|
54
|
+
empty(callback = noop) {
|
|
55
55
|
super.empty(() => {
|
|
56
56
|
this.cloudConnection = null;
|
|
57
57
|
callback();
|
|
@@ -73,7 +73,7 @@ class ConnectionPool extends BaseConnectionPool {
|
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* Marks a connection as 'dead'.
|
|
76
|
-
* If needed adds the connection to the dead list
|
|
76
|
+
* If needed, adds the connection to the dead list
|
|
77
77
|
* and then increments the `deadCount`.
|
|
78
78
|
*
|
|
79
79
|
* @param {object} connection
|
|
@@ -82,9 +82,9 @@ class ConnectionPool extends BaseConnectionPool {
|
|
|
82
82
|
const { id } = connection;
|
|
83
83
|
debug(`Marking as 'dead' connection '${id}'`);
|
|
84
84
|
if (this.dead.indexOf(id) === -1) {
|
|
85
|
-
// It might happen that `markDead` is called
|
|
85
|
+
// It might happen that `markDead` is called just after
|
|
86
86
|
// a pool update, and in such case we will add to the dead
|
|
87
|
-
// list a node that no longer
|
|
87
|
+
// list a node that no longer exists. The following check verifies
|
|
88
88
|
// that the connection is still part of the pool before
|
|
89
89
|
// marking it as dead.
|
|
90
90
|
for (let i = 0; i < this.size; i++) {
|
|
@@ -227,7 +227,7 @@ class ConnectionPool extends BaseConnectionPool {
|
|
|
227
227
|
*
|
|
228
228
|
* @returns {ConnectionPool}
|
|
229
229
|
*/
|
|
230
|
-
empty(callback) {
|
|
230
|
+
empty(callback = noop) {
|
|
231
231
|
super.empty(() => {
|
|
232
232
|
this.dead = [];
|
|
233
233
|
callback();
|