Skip to content

Commit ec45e5f

Browse files
authored
Es library upgrade (#38)
* Updates to new Library * Remove old ES Lib
1 parent 759e810 commit ec45e5f

File tree

7 files changed

+90
-44
lines changed

7 files changed

+90
-44
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## Unreleased
6+
### Changed
7+
* Moved to the new @elastic/elasticsearch library
8+
59
## [2.5.2] - 06-04-2021
610
### Fixed
711
* Now handle negative numbers in a more consistent manner

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
{
23
"esConnections": {
34
"local": {
@@ -13,4 +14,4 @@
1314
}
1415
}
1516
}
16-
}
17+
}

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"author": "Danny Hatcher",
1414
"license": "Apache-2.0",
1515
"dependencies": {
16+
"@elastic/elasticsearch": "^7.13.0",
1617
"@koopjs/logger": "^2.0.6",
1718
"@mapbox/geojson-rewind": "^0.5.0",
18-
"elasticsearch": "14.0.0",
1919
"flat": "^5.0.0",
2020
"koop": "^4.0.4",
2121
"moment": "2.20.1",

src/model.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (koop) {
2323
featureCollection.metadata.timeInfo.timeExtent = [this.startFieldStats.min, this.endFieldStats.max];
2424
};
2525

26-
this.getTileOffset = function (z, minimumOffset= 4.864) {
26+
this.getTileOffset = function (z, minimumOffset = 4.864) {
2727
// Emulate the offset that would come from a feature service request with a minimum of 4.864
2828
// Level 22 has 0.019 meters per pixel and this increases by a factor of 2 as the tile level goes down
2929
return Math.max(minimumOffset, 0.019 * Math.pow(2, 22 - parseInt(z)));
@@ -151,7 +151,7 @@ module.exports = function (koop) {
151151
});
152152
this.esClients[esId].count(countQuery).then(function (resp) {
153153
logger.debug("count resp:", resp);
154-
featureCollection.count = resp.count;
154+
featureCollection.count = resp.body.count;
155155
callback(null, featureCollection);
156156
}, function (err) {
157157
logger.error(err.message);
@@ -195,6 +195,7 @@ module.exports = function (koop) {
195195
// let startESQueryMillis = Date.now().valueOf();
196196
// console.log(JSON.stringify(esQuery, null, 2));
197197
let searchResponse = await this.esClients[esId].search(esQuery);
198+
searchResponse = searchResponse.body;
198199
// logger.debug(`Got ES Response In: ${(Date.now().valueOf() - startESQueryMillis)/1000} seconds`);
199200
// let startParseMillis = Date.now().valueOf();
200201
let totalHits = isNaN(searchResponse.hits.total) ? searchResponse.hits.total.value : searchResponse.hits.total;
@@ -391,37 +392,34 @@ module.exports = function (koop) {
391392
}
392393

393394

394-
function queryHashAggregations(indexConfig, mapping, esQuery, geohashUtil, esClient) {
395-
return new Promise((resolve, reject) => {
396-
// just aggs, no need to get documents back
397-
esQuery.body.size = 1;
398-
399-
esQuery.body.aggregations = {
400-
agg_grid: {
401-
geohash_grid: {
402-
field: indexConfig.geometryField,
403-
precision: geohashUtil.precision
404-
}
395+
async function queryHashAggregations(indexConfig, mapping, esQuery, geohashUtil, esClient) {
396+
// just aggs, no need to get documents back
397+
esQuery.body.size = 1;
398+
esQuery.body.aggregations = {
399+
agg_grid: {
400+
geohash_grid: {
401+
field: indexConfig.geometryField,
402+
precision: geohashUtil.precision
405403
}
406-
};
407-
408-
esClient.search(esQuery).then(response => {
409-
let geohashFeatures = [];
410-
let hitConverter = new HitConverter();
411-
for (let i = 0; i < response.aggregations.agg_grid.buckets.length; i++) {
412-
let feature = hitConverter.featureFromGeoHashBucket(response.aggregations.agg_grid.buckets[i],
413-
response.hits.hits[0], indexConfig, mapping, esQuery.body.query.bool);
414-
if (feature) {
415-
geohashFeatures.push(feature);
416-
}
404+
}
405+
};
406+
try {
407+
let result = await esClient.search(esQuery);
408+
let response = result.body;
409+
let geohashFeatures = [];
410+
let hitConverter = new HitConverter();
411+
for (let i = 0; i < response.aggregations.agg_grid.buckets.length; i++) {
412+
let feature = hitConverter.featureFromGeoHashBucket(response.aggregations.agg_grid.buckets[i],
413+
response.hits.hits[0], indexConfig, mapping, esQuery.body.query.bool);
414+
if (feature) {
415+
geohashFeatures.push(feature);
417416
}
418-
resolve(geohashFeatures);
419-
}).catch(error => {
420-
logger.error(error);
421-
reject(error);
422-
})
423-
424-
});
417+
}
418+
return Promise.resolve(geohashFeatures);
419+
} catch (e) {
420+
logger.error(e);
421+
return Promise.reject(e);
422+
}
425423
}
426424

427425
function tile2long(x, z) {
@@ -481,7 +479,7 @@ module.exports = function (koop) {
481479
try {
482480
logger.debug(JSON.stringify(queryBody, null, 2));
483481
let response = await esClient.search(queryBody);
484-
let shapeHits = response.hits.hits;
482+
let shapeHits = response.body.hits.hits;
485483
return Promise.resolve(shapeHits);
486484
} catch (e) {
487485
return Promise.reject(e);
@@ -896,7 +894,7 @@ module.exports = function (koop) {
896894
},
897895
properties: customAggregation.defaultReturnFields(mapping, indexConfig, query.customAggregations)
898896
};
899-
if(defaultFeature.geometry.type === "Point"){
897+
if (defaultFeature.geometry.type === "Point") {
900898
defaultFeature.geometry.coordinates = defaultFeature.geometry.coordinates[0][0];
901899
}
902900
aggCollection.features = [defaultFeature];

src/utils/elasticConnectUtil.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const appConfig = require('config');
2-
const elasticsearch = require('elasticsearch');
2+
const {Client} = require('@elastic/elasticsearch');
33

44
function initializeESClients() {
55
let esClients = {};
@@ -47,21 +47,21 @@ function initializeESClients() {
4747
esCert = fs.readFileSync(esCert);
4848
}
4949

50-
let esClient = new elasticsearch.Client({
51-
hosts: hosts,
52-
log: 'error',
50+
let esClient = new Client({
51+
node: hosts,
5352
requestTimeout: 900000,
5453
keepAlive: false,
5554
ssl: {
5655
key: esKey,
5756
cert: esCert,
5857
pfx: pfx,
59-
passphrase: passphrase
58+
passphrase: passphrase,
59+
rejectUnauthorized: false
6060
}
6161
});
6262
esClients[connectInfo.id] = esClient;
6363
}
6464
return esClients;
6565
}
6666

67-
exports.initializeESClients = initializeESClients;
67+
exports.initializeESClients = initializeESClients;

src/utils/indexInfo.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class IndexInfo{
2020
this.mappings[esName] = {};
2121
}
2222
if(type !== undefined){
23-
this.mappings[esName][indexName] = result[Object.keys(result)[0]].mappings[type];
23+
this.mappings[esName][indexName] = result.body[Object.keys(result.body)[0]].mappings[type];
2424
resolve(this.mappings[esName][indexName]);
2525
} else {
26-
const mappings = result[Object.keys(result)[0]].mappings;
26+
const mappings = result.body[Object.keys(result.body)[0]].mappings;
2727
this.mappings[esName][indexName] = mappings.properties;
2828
resolve(this.mappings[esName][indexName]);
2929
}
@@ -59,4 +59,4 @@ class IndexInfo{
5959
}
6060
}
6161

62-
module.exports = IndexInfo;
62+
module.exports = IndexInfo;

0 commit comments

Comments
 (0)