/
default-search-plugin.ts
235 lines (221 loc) · 9.66 KB
/
default-search-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { SearchReindexResponse } from '@vendure/common/lib/generated-types';
import { ID, Type } from '@vendure/common/lib/shared-types';
import { buffer, debounceTime, delay, filter, map } from 'rxjs/operators';
import { Column } from 'typeorm';
import { Injector } from '../../common';
import { idsAreEqual } from '../../common/utils';
import { EventBus } from '../../event-bus/event-bus';
import { AssetEvent } from '../../event-bus/events/asset-event';
import { CollectionModificationEvent } from '../../event-bus/events/collection-modification-event';
import { ProductChannelEvent } from '../../event-bus/events/product-channel-event';
import { ProductEvent } from '../../event-bus/events/product-event';
import { ProductVariantChannelEvent } from '../../event-bus/events/product-variant-channel-event';
import { ProductVariantEvent } from '../../event-bus/events/product-variant-event';
import { TaxRateModificationEvent } from '../../event-bus/events/tax-rate-modification-event';
import { JobQueueService } from '../../job-queue/job-queue.service';
import { PluginCommonModule } from '../plugin-common.module';
import { VendurePlugin } from '../vendure-plugin';
import { stockStatusExtension } from './api/api-extensions';
import { AdminFulltextSearchResolver, ShopFulltextSearchResolver } from './api/fulltext-search.resolver';
import { BUFFER_SEARCH_INDEX_UPDATES, PLUGIN_INIT_OPTIONS } from './constants';
import { SearchIndexItem } from './entities/search-index-item.entity';
import { FulltextSearchService } from './fulltext-search.service';
import { IndexerController } from './indexer/indexer.controller';
import { SearchIndexService } from './indexer/search-index.service';
import { SearchJobBufferService } from './search-job-buffer/search-job-buffer.service';
import { DefaultSearchPluginInitOptions } from './types';
export interface DefaultSearchReindexResponse extends SearchReindexResponse {
timeTaken: number;
indexedItemCount: number;
}
/**
* @description
* The DefaultSearchPlugin provides a full-text Product search based on the full-text searching capabilities of the
* underlying database.
*
* The DefaultSearchPlugin is bundled with the `\@vendure/core` package. If you are not using an alternative search
* plugin, then make sure this one is used, otherwise you will not be able to search products via the
* [`search` query](/reference/graphql-api/shop/queries#search).
*
* :::caution
* Note that the quality of the fulltext search capabilities varies depending on the underlying database being used. For example,
* the MySQL & Postgres implementations will typically yield better results than the SQLite implementation.
* :::
*
*
* @example
* ```ts
* import { DefaultSearchPlugin, VendureConfig } from '\@vendure/core';
*
* export const config: VendureConfig = {
* // Add an instance of the plugin to the plugins array
* plugins: [
* DefaultSearchPlugin.init({
* indexStockStatus: true,
* bufferUpdates: true,
* }),
* ],
* };
* ```
*
* @docsCategory DefaultSearchPlugin
*/
@VendurePlugin({
imports: [PluginCommonModule],
providers: [
FulltextSearchService,
SearchIndexService,
IndexerController,
SearchJobBufferService,
{ provide: PLUGIN_INIT_OPTIONS, useFactory: () => DefaultSearchPlugin.options },
{
provide: BUFFER_SEARCH_INDEX_UPDATES,
useFactory: () => DefaultSearchPlugin.options.bufferUpdates === true,
},
],
adminApiExtensions: {
schema: () =>
DefaultSearchPlugin.options.indexStockStatus === true ? stockStatusExtension : undefined,
resolvers: [AdminFulltextSearchResolver],
},
shopApiExtensions: {
schema: () =>
DefaultSearchPlugin.options.indexStockStatus === true ? stockStatusExtension : undefined,
resolvers: [ShopFulltextSearchResolver],
},
entities: [SearchIndexItem],
compatibility: '>0.0.0',
})
export class DefaultSearchPlugin implements OnApplicationBootstrap, OnApplicationShutdown {
static options: DefaultSearchPluginInitOptions = {};
/** @internal */
constructor(
private eventBus: EventBus,
private searchIndexService: SearchIndexService,
private jobQueueService: JobQueueService,
private moduleRef: ModuleRef,
) {}
static init(options: DefaultSearchPluginInitOptions): Type<DefaultSearchPlugin> {
this.options = options;
if (options.indexStockStatus === true) {
this.addStockColumnsToEntity();
}
return DefaultSearchPlugin;
}
/** @internal */
async onApplicationBootstrap() {
this.eventBus.ofType(ProductEvent).subscribe(event => {
if (event.type === 'deleted') {
return this.searchIndexService.deleteProduct(event.ctx, event.product);
} else {
return this.searchIndexService.updateProduct(event.ctx, event.product);
}
});
this.eventBus.ofType(ProductVariantEvent).subscribe(event => {
if (event.type === 'deleted') {
return this.searchIndexService.deleteVariant(event.ctx, event.variants);
} else {
return this.searchIndexService.updateVariants(event.ctx, event.variants);
}
});
this.eventBus.ofType(AssetEvent).subscribe(event => {
if (event.type === 'updated') {
return this.searchIndexService.updateAsset(event.ctx, event.asset);
}
if (event.type === 'deleted') {
return this.searchIndexService.deleteAsset(event.ctx, event.asset);
}
});
this.eventBus.ofType(ProductChannelEvent).subscribe(event => {
if (event.type === 'assigned') {
return this.searchIndexService.assignProductToChannel(
event.ctx,
event.product.id,
event.channelId,
);
} else {
return this.searchIndexService.removeProductFromChannel(
event.ctx,
event.product.id,
event.channelId,
);
}
});
this.eventBus.ofType(ProductVariantChannelEvent).subscribe(event => {
if (event.type === 'assigned') {
return this.searchIndexService.assignVariantToChannel(
event.ctx,
event.productVariant.id,
event.channelId,
);
} else {
return this.searchIndexService.removeVariantFromChannel(
event.ctx,
event.productVariant.id,
event.channelId,
);
}
});
// TODO: Remove this buffering logic because because we have dedicated buffering based on #1137
const collectionModification$ = this.eventBus.ofType(CollectionModificationEvent);
const closingNotifier$ = collectionModification$.pipe(debounceTime(50));
collectionModification$
.pipe(
buffer(closingNotifier$),
filter(events => 0 < events.length),
map(events => ({
ctx: events[0].ctx,
ids: events.reduce((ids, e) => [...ids, ...e.productVariantIds], [] as ID[]),
})),
filter(e => 0 < e.ids.length),
)
.subscribe(events => {
return this.searchIndexService.updateVariantsById(events.ctx, events.ids);
});
this.eventBus
.ofType(TaxRateModificationEvent)
// The delay prevents a "TransactionNotStartedError" (in SQLite/sqljs) by allowing any existing
// transactions to complete before a new job is added to the queue (assuming the SQL-based
// JobQueueStrategy).
// TODO: should be able to remove owing to f0fd6625
.pipe(delay(1))
.subscribe(event => {
const defaultTaxZone = event.ctx.channel.defaultTaxZone;
if (defaultTaxZone && idsAreEqual(defaultTaxZone.id, event.taxRate.zone.id)) {
return this.searchIndexService.reindex(event.ctx);
}
});
await this.initSearchStrategy();
}
/** @internal */
async onApplicationShutdown(signal?: string) {
await this.destroySearchStrategy();
}
private async initSearchStrategy(): Promise<void> {
const injector = new Injector(this.moduleRef);
const searchService = injector.get(FulltextSearchService);
if (typeof searchService.searchStrategy.init === 'function') {
await searchService.searchStrategy.init(injector);
}
}
private async destroySearchStrategy(): Promise<void> {
const injector = new Injector(this.moduleRef);
const searchService = injector.get(FulltextSearchService);
if (typeof searchService.searchStrategy.destroy === 'function') {
await searchService.searchStrategy.destroy();
}
}
/**
* If the `indexStockStatus` option is set to `true`, we dynamically add a couple of
* columns to the SearchIndexItem entity. This is done in this way to allow us to add
* support for indexing the stock status, while preventing a backwards-incompatible
* schema change.
*/
private static addStockColumnsToEntity() {
const instance = new SearchIndexItem();
Column({ type: 'boolean', default: true })(instance, 'inStock');
Column({ type: 'boolean', default: true })(instance, 'productInStock');
}
}