Skip to content

Commit

Permalink
feat(elasticsearch): support @elastic/elasticsearch client
Browse files Browse the repository at this point in the history
BREAKING: Support new elasticsearch configuration format.

As part of updating to elasticsearch 8, I'm updating pelias from the
deprecated `elastic` package to it's successor `@elastic/elasticsearch`
which supports both es7 and 8 (and some older versions too, but I think we
only care about 7+).

Note that this is a backwards incompatible change - old pelias code
using the `elasticsearch` package will not be able to use these new
configs.

apiVersion is determined by the client version, so it's no longer
necessary.

`keepAlive` is true by default, you need to set `agent: false` to
disable it:

    const client = new Client({
      node: 'http://localhost:9200',
      // Disable agent and keep-alive
      agent: false
    })

`hosts` is now `nodes` (or `node`) and has a different format.

Deleted parameters that aren't used in the new client:

 - `env`: I'm not sure what it's for. I suspect it's a pelias thing, not
   an elasticsearch thing, which seems like a conflation of the
   responsibilities of 'esclient config' vs 'pelias env mgmt'. Maybe I'm
   confused though.

 - `log`: The new elasticsearch client emits events instead of logging.
  • Loading branch information
michaelkirk committed Mar 12, 2024
1 parent 72e4a4c commit a5d196e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 53 deletions.
14 changes: 1 addition & 13 deletions config/defaults.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
{
"esclient": {
"apiVersion": "7.x",
"keepAlive": true,
"requestTimeout": "120000",
"hosts": [{
"env": "development",
"protocol": "http",
"host": "localhost",
"port": 9200
}],
"log": [{
"type": "stdio",
"json": false,
"level": [ "error", "warning" ]
}]
"nodes": ["http://localhost:9200"]
},
"elasticsearch": {
"settings": {
Expand Down
15 changes: 1 addition & 14 deletions config/env.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
{
"esclient": {
"hosts": [
{
"env": "production",
"protocol": "http",
"host": "localhost",
"port": 9200
},
{
"env": "production",
"protocol": "http",
"host": "localhost",
"port": 9300
}
]
"nodes": ["http://localhost:9200", "http://localhost:9300"]
},
"imports": {
"geonames": {
Expand Down
19 changes: 1 addition & 18 deletions test/expected-deep.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
{
"esclient": {
"apiVersion": "7.x",
"keepAlive": true,
"requestTimeout": "120000",
"hosts": [{
"env": "production",
"protocol": "http",
"host": "localhost",
"port": 9200
},{
"env": "production",
"protocol": "http",
"host": "localhost",
"port": 9300
}],
"log": [{
"type": "stdio",
"json": false,
"level": [ "error", "warning" ]
}]
"nodes": ["http://localhost:9200", "http://localhost:9300"]
},
"elasticsearch": {
"settings": {
Expand Down
16 changes: 8 additions & 8 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module.exports.generate.development = function(test) {
t.equal(typeof config, 'object', 'valid function');
t.deepEqual(c, defaults, 'defaults');
t.equal(typeof c.esclient, 'object', 'valid property');
t.equal(Object.keys(c.esclient).length, 5, 'copied all default properties');
t.equal(c.esclient.hosts.length, 1, 'defaults');
t.equal(Object.keys(c.esclient).length, 2, 'copied all default properties');
t.equal(c.esclient.nodes.length, 1, 'defaults');
t.end();

// reset localpath
Expand All @@ -37,7 +37,7 @@ module.exports.generate.production = function(test) {
t.notDeepEqual(c, defaults, 'valid function');
t.equal(typeof c.esclient, 'object', 'valid property');
t.equal(Object.keys(c.esclient).length, 1, 'deleted all default properties');
t.equal(c.esclient.hosts.length, 2, 'shallow merge');
t.equal(c.esclient.nodes.length, 2, 'shallow merge');
t.end();

// unset the PELIAS_CONFIG env var
Expand All @@ -52,8 +52,8 @@ module.exports.generate.production = function(test) {
t.equal(typeof config, 'object', 'valid function');
t.notDeepEqual(c, defaults, 'valid function');
t.equal(typeof c.esclient, 'object', 'valid property');
t.equal(Object.keys(c.esclient).length, 5, 'keep all default properties');
t.equal(c.esclient.hosts.length, 2, 'deep merge should set two hosts');
t.equal(Object.keys(c.esclient).length, 2, 'keep all default properties');
t.equal(c.esclient.nodes.length, 2, 'deep merge should set two nodes');
t.end();

// unset the PELIAS_CONFIG env var
Expand All @@ -68,8 +68,8 @@ module.exports.generate.production = function(test) {
t.equal(typeof config, 'object', 'valid function');
t.notDeepEqual(c, defaults, 'valid function');
t.equal(typeof c.esclient, 'object', 'valid property');
t.equal(Object.keys(c.esclient).length, 5, 'keep all default properties');
t.equal(c.esclient.hosts.length, 2, 'deep merge should set two hosts');
t.equal(Object.keys(c.esclient).length, 2, 'keep all default properties');
t.equal(c.esclient.nodes.length, 2, 'deep merge should set two nodes');
t.end();

// unset the PELIAS_CONFIG env var
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports.generate.local = function(test) {
t.equal(typeof config, 'object', 'valid function');
t.notDeepEqual(c, defaults, 'valid function');
t.equal(typeof c.esclient, 'object', 'valid property');
t.equal(Object.keys(c.esclient).length, 5, 'keep all default properties');
t.equal(Object.keys(c.esclient).length, 2, 'keep all default properties');
t.equal(c.interpolation.client.adapter, 'http', 'interpolation client');
t.equal(c.interpolation.client.host, 'http://localhost:9999', 'interpolation client');
t.equal(c.imports.geonames.datapath, '/media/hdd', 'local paths');
Expand Down

0 comments on commit a5d196e

Please sign in to comment.