-
-
Notifications
You must be signed in to change notification settings - Fork 18
support modern and legacy elasticsearch clients #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
missinglink
wants to merge
1
commit into
master
Choose a base branch
from
v2-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| const _ = require('lodash'); | ||
|
|
||
| /** | ||
| * This module is responsible for mapping betwen the legacy `npm elasticsearch` | ||
| * config format and the modern `npm @elastic/elasticsearch` format. | ||
| * | ||
| * legacy: https://www.elastic.co/guide/en/elasticsearch/client/elasticsearch-js/16.x/config-options.html | ||
| * modern: https://www.elastic.co/docs/reference/elasticsearch/clients/javascript/basic-config#maxretries | ||
| */ | ||
|
|
||
| const modernToLegacy = (modern) => { | ||
| const legacy = {}; | ||
|
|
||
| // modern config allows for *either* the key 'node' (string) or 'nodes' ([]string) | ||
| const nodes = _.has(modern, 'nodes') ? _.get(modern, 'nodes') : [_.get(modern, 'node', {})]; | ||
| _.set(legacy, 'hosts', nodes); | ||
|
|
||
| // map basic configuration options | ||
| _.set(legacy, 'maxRetries', _.get(modern, 'maxRetries', undefined)); | ||
| _.set(legacy, 'requestTimeout', _.get(modern, 'requestTimeout', undefined)); | ||
|
|
||
| return legacy; | ||
| }; | ||
|
|
||
| const legacyToModern = (legacy) => { | ||
| const modern = {}; | ||
|
|
||
| // legacy config allows for *either* the key 'host' (string) or 'hosts' ([]string|[]Object) | ||
| const hosts = _.has(legacy, 'hosts') ? _.get(legacy, 'hosts') : [_.get(legacy, 'host', {})]; | ||
| _.set(modern, 'nodes', hosts.map(host => { | ||
| if (_.isObject(host)) { host = `${host.protocol}://${host.host}:${host.port}`; } | ||
| return host; | ||
| })); | ||
|
|
||
| // map basic configuration options | ||
| _.set(modern, 'maxRetries', _.get(legacy, 'maxRetries', undefined)); | ||
| _.set(modern, 'requestTimeout', _.get(legacy, 'requestTimeout', undefined)); | ||
|
|
||
| return modern; | ||
| }; | ||
|
|
||
| module.exports = { modernToLegacy, legacyToModern }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,20 @@ | ||
| const _ = require('lodash'); | ||
| const elasticsearch = require('elasticsearch'); | ||
| const settings = require('pelias-config').generate(); | ||
| const config = require('pelias-config').generate(); | ||
| const { modernToLegacy } = require('../configMap'); | ||
|
|
||
| module.exports = function(){ | ||
| return new elasticsearch.Client( settings.esclient || {} ); | ||
|
|
||
| // use legacy config | ||
| if (_.has(config, 'esclient')) { | ||
| return new elasticsearch.Client(config.esclient); | ||
| } | ||
|
|
||
| // check for modern config | ||
| if (_.has(config, 'elasticsearch.client')) { | ||
| return new elasticsearch.Client(modernToLegacy(config.elasticsearch.client)); | ||
| } | ||
|
|
||
| // no config specified | ||
| return new elasticsearch.Client({}); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| const _ = require('lodash'); | ||
| const { Client } = require('@elastic/elasticsearch'); | ||
| const config = require('pelias-config').generate(); | ||
| const { legacyToModern } = require('../configMap'); | ||
|
|
||
| module.exports = function(){ | ||
|
|
||
| // use modern config | ||
| if (_.has(config, 'elasticsearch.client')) { | ||
| return new Client(config.elasticsearch.client); | ||
| } | ||
|
|
||
| // check for legacy config | ||
| if (_.has(config, 'esclient')) { | ||
| return new Client(legacyToModern(config.esclient)); | ||
| } | ||
|
|
||
| // no config specified | ||
| return new Client({}); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const through = require('through2'); | ||
|
|
||
| function streamFactory( opts ){ | ||
| opts = opts || {}; | ||
| if( !opts.client ){ opts.client = require('./client')(); } | ||
|
|
||
| // passthrough stream | ||
| const stream = through.obj(); | ||
|
|
||
| stream.bulk = opts.client.helpers.bulk({ | ||
| datasource: stream, | ||
| onDocument (doc) { return doc; } | ||
| }); | ||
|
|
||
| return stream; | ||
| } | ||
|
|
||
| module.exports = streamFactory; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be a breaking change to the interface of the
dbclientmodule, meaning we'll need changes in all importers. Is that intended?If so can you provide some instructions on how to test this PR as it stands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately I believe this is unavoidable due to the current export being on the root?
dbclient/index.js
Line 5 in e98d950
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably handle it as a breaking change, then migrate all importers to use the
v1.createWriteStreampath instead of the module root.I'm open to ideas, just working around the limitations we have.