@cumulus/es-client 13.1.0 → 13.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/esDefaults.js +3 -0
- package/esSearchAfter.js +57 -0
- package/package.json +8 -8
- package/queries.js +3 -1
- package/search.js +7 -5
package/esDefaults.js
CHANGED
package/esSearchAfter.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { Search } = require('./search');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ES search_after wrapper class for Search.
|
|
5
|
+
*
|
|
6
|
+
* This class just allows a user to access the search_after api of elasticsearch for
|
|
7
|
+
* long lived requests that require multiple server requests.
|
|
8
|
+
*
|
|
9
|
+
* Use: Instantiate a class with the queryStringParameters, type, and index.
|
|
10
|
+
* and call the object.query() until no more results are returned.
|
|
11
|
+
*/
|
|
12
|
+
class ESSearchAfter extends Search {
|
|
13
|
+
/**
|
|
14
|
+
* Build search params for search-after API.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Object} ES search params
|
|
17
|
+
*/
|
|
18
|
+
_buildSearch() {
|
|
19
|
+
const params = super._buildSearch();
|
|
20
|
+
delete params.from;
|
|
21
|
+
delete params.to;
|
|
22
|
+
|
|
23
|
+
if (this.params.searchContext) {
|
|
24
|
+
// express seems to decode the URI params for us so we don't need to call decodeURIComponent
|
|
25
|
+
params.body.search_after = JSON.parse(this.params.searchContext);
|
|
26
|
+
}
|
|
27
|
+
return params;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Query ES search-after API.
|
|
32
|
+
*
|
|
33
|
+
* @returns {Promise<Object>} Object containing query meta and results
|
|
34
|
+
*/
|
|
35
|
+
async query() {
|
|
36
|
+
if (!this.client) {
|
|
37
|
+
this.client = await super.constructor.es();
|
|
38
|
+
}
|
|
39
|
+
const searchParams = this._buildSearch();
|
|
40
|
+
const response = await this.client.search(searchParams);
|
|
41
|
+
|
|
42
|
+
const hits = response.body.hits.hits;
|
|
43
|
+
const meta = this._metaTemplate();
|
|
44
|
+
|
|
45
|
+
meta.count = response.body.hits.total;
|
|
46
|
+
meta.page = this.page;
|
|
47
|
+
if (hits.length > 0) {
|
|
48
|
+
meta.searchContext = encodeURIComponent(JSON.stringify(hits[hits.length - 1].sort));
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
meta,
|
|
52
|
+
results: hits.map((s) => s._source),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = ESSearchAfter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cumulus/es-client",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.3.0",
|
|
4
4
|
"description": "Utilities for working with Elasticsearch",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CUMULUS",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"author": "Cumulus Authors",
|
|
31
31
|
"license": "Apache-2.0",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@cumulus/common": "13.
|
|
34
|
-
"@cumulus/errors": "13.
|
|
35
|
-
"@cumulus/logger": "13.
|
|
36
|
-
"@cumulus/message": "13.
|
|
33
|
+
"@cumulus/common": "13.3.0",
|
|
34
|
+
"@cumulus/errors": "13.3.0",
|
|
35
|
+
"@cumulus/logger": "13.3.0",
|
|
36
|
+
"@cumulus/message": "13.3.0",
|
|
37
37
|
"@elastic/elasticsearch": "^5.6.20",
|
|
38
38
|
"aws-elasticsearch-connector": "8.2.0",
|
|
39
39
|
"aws-sdk": "^2.585.0",
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
"p-limit": "^1.2.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@cumulus/aws-client": "13.
|
|
46
|
-
"@cumulus/test-data": "13.
|
|
45
|
+
"@cumulus/aws-client": "13.3.0",
|
|
46
|
+
"@cumulus/test-data": "13.3.0",
|
|
47
47
|
"p-each-series": "^2.1.0"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "eedae36804f1b1e753161a3e7eb105dacd40d889"
|
|
50
50
|
}
|
package/queries.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
const omit = require('lodash/omit');
|
|
11
11
|
|
|
12
|
+
const { defaultSortParams } = require('./esDefaults');
|
|
12
13
|
const { convertTextField } = require('./textFields');
|
|
13
14
|
|
|
14
15
|
const regexes = {
|
|
@@ -57,7 +58,7 @@ const build = {
|
|
|
57
58
|
return { [sortField]: { order: key.startsWith('-') ? 'desc' : 'asc' } };
|
|
58
59
|
});
|
|
59
60
|
} else {
|
|
60
|
-
sort =
|
|
61
|
+
sort = defaultSortParams;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
return sort;
|
|
@@ -226,6 +227,7 @@ module.exports = function query(params) {
|
|
|
226
227
|
'prefix',
|
|
227
228
|
'infix',
|
|
228
229
|
'fields',
|
|
230
|
+
'searchContext',
|
|
229
231
|
]
|
|
230
232
|
);
|
|
231
233
|
|
package/search.js
CHANGED
|
@@ -327,20 +327,22 @@ class BaseSearch {
|
|
|
327
327
|
if (!this.client) {
|
|
328
328
|
this.client = await this.constructor.es(null, this.metrics);
|
|
329
329
|
}
|
|
330
|
-
const
|
|
331
|
-
const
|
|
330
|
+
const response = await this.client.search(searchParams);
|
|
331
|
+
const hits = response.body.hits.hits;
|
|
332
332
|
|
|
333
333
|
const meta = this._metaTemplate();
|
|
334
334
|
meta.limit = this.size;
|
|
335
335
|
meta.page = this.page;
|
|
336
|
-
meta.count =
|
|
336
|
+
meta.count = response.body.hits.total;
|
|
337
|
+
if (hits.length > 0) {
|
|
338
|
+
meta.searchContext = encodeURIComponent(JSON.stringify(hits[hits.length - 1].sort));
|
|
339
|
+
}
|
|
337
340
|
|
|
338
341
|
return {
|
|
339
342
|
meta,
|
|
340
|
-
results:
|
|
343
|
+
results: hits.map((s) => s._source),
|
|
341
344
|
};
|
|
342
345
|
} catch (error) {
|
|
343
|
-
//log.error(e, logDetails);
|
|
344
346
|
return error;
|
|
345
347
|
}
|
|
346
348
|
}
|