|
|
@@ -1,7 +1,7 @@
|
|
|
+import { NodeOptions } from '@elastic/elasticsearch';
|
|
|
import {
|
|
|
AssetEvent,
|
|
|
CollectionModificationEvent,
|
|
|
- DeepRequired,
|
|
|
EventBus,
|
|
|
HealthCheckRegistryService,
|
|
|
ID,
|
|
|
@@ -26,7 +26,7 @@ import { ElasticsearchHealthIndicator } from './elasticsearch.health';
|
|
|
import { ElasticsearchService } from './elasticsearch.service';
|
|
|
import { generateSchemaExtensions } from './graphql-schema-extensions';
|
|
|
import { ElasticsearchIndexerController } from './indexer.controller';
|
|
|
-import { ElasticsearchOptions, mergeWithDefaults } from './options';
|
|
|
+import { ElasticsearchOptions, ElasticsearchRuntimeOptions, mergeWithDefaults } from './options';
|
|
|
|
|
|
/**
|
|
|
* @description
|
|
|
@@ -37,11 +37,11 @@ import { ElasticsearchOptions, mergeWithDefaults } from './options';
|
|
|
*
|
|
|
* **Requires Elasticsearch v7.0 or higher.**
|
|
|
*
|
|
|
- * `yarn add \@vendure/elasticsearch-plugin`
|
|
|
+ * `yarn add \@elastic/elasticsearch \@vendure/elasticsearch-plugin`
|
|
|
*
|
|
|
* or
|
|
|
*
|
|
|
- * `npm install \@vendure/elasticsearch-plugin`
|
|
|
+ * `npm install \@elastic/elasticsearch \@vendure/elasticsearch-plugin`
|
|
|
*
|
|
|
* Make sure to remove the `DefaultSearchPlugin` if it is still in the VendureConfig plugins array.
|
|
|
*
|
|
|
@@ -208,12 +208,14 @@ import { ElasticsearchOptions, mergeWithDefaults } from './options';
|
|
|
? [ShopElasticSearchResolver, CustomMappingsResolver]
|
|
|
: [ShopElasticSearchResolver];
|
|
|
},
|
|
|
- schema: () => generateSchemaExtensions(ElasticsearchPlugin.options),
|
|
|
+ // `any` cast is there due to a strange error "Property '[Symbol.iterator]' is missing in type... URLSearchParams"
|
|
|
+ // which looks like possibly a TS/definitions bug.
|
|
|
+ schema: () => generateSchemaExtensions(ElasticsearchPlugin.options as any),
|
|
|
},
|
|
|
workers: [ElasticsearchIndexerController],
|
|
|
})
|
|
|
export class ElasticsearchPlugin implements OnVendureBootstrap {
|
|
|
- private static options: DeepRequired<ElasticsearchOptions>;
|
|
|
+ private static options: ElasticsearchRuntimeOptions;
|
|
|
|
|
|
/** @internal */
|
|
|
constructor(
|
|
|
@@ -235,17 +237,18 @@ export class ElasticsearchPlugin implements OnVendureBootstrap {
|
|
|
/** @internal */
|
|
|
async onVendureBootstrap(): Promise<void> {
|
|
|
const { host, port } = ElasticsearchPlugin.options;
|
|
|
+ const nodeName = this.nodeName();
|
|
|
try {
|
|
|
const pingResult = await this.elasticsearchService.checkConnection();
|
|
|
} catch (e) {
|
|
|
- Logger.error(`Could not connect to Elasticsearch instance at "${host}:${port}"`, loggerCtx);
|
|
|
+ Logger.error(`Could not connect to Elasticsearch instance at "${nodeName}"`, loggerCtx);
|
|
|
Logger.error(JSON.stringify(e), loggerCtx);
|
|
|
this.healthCheckRegistryService.registerIndicatorFunction(() =>
|
|
|
this.elasticsearchHealthIndicator.startupCheckFailed(e.message),
|
|
|
);
|
|
|
return;
|
|
|
}
|
|
|
- Logger.info(`Sucessfully connected to Elasticsearch instance at "${host}:${port}"`, loggerCtx);
|
|
|
+ Logger.info(`Successfully connected to Elasticsearch instance at "${nodeName}"`, loggerCtx);
|
|
|
|
|
|
await this.elasticsearchService.createIndicesIfNotExists();
|
|
|
this.elasticsearchIndexService.initJobQueue();
|
|
|
@@ -321,4 +324,29 @@ export class ElasticsearchPlugin implements OnVendureBootstrap {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns a string representation of the target node(s) that the Elasticsearch
|
|
|
+ * client is configured to connect to.
|
|
|
+ */
|
|
|
+ private nodeName(): string {
|
|
|
+ const { host, port, clientOptions } = ElasticsearchPlugin.options;
|
|
|
+ const node = clientOptions?.node;
|
|
|
+ const nodes = clientOptions?.nodes;
|
|
|
+ if (nodes) {
|
|
|
+ return [...nodes].join(', ');
|
|
|
+ }
|
|
|
+ if (node) {
|
|
|
+ if (Array.isArray(node)) {
|
|
|
+ return (node as any[])
|
|
|
+ .map((n: string | NodeOptions) => {
|
|
|
+ return typeof n === 'string' ? n : n.url.toString();
|
|
|
+ })
|
|
|
+ .join(', ');
|
|
|
+ } else {
|
|
|
+ return typeof node === 'string' ? node : node.url.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return `${host}:${port}`;
|
|
|
+ }
|
|
|
}
|