@vendure/elasticsearch-plugin 1.2.1 → 1.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -69,11 +69,11 @@ function buildElasticBody(input, searchConfig, channelId, languageCode, enabledO
69
69
  }
70
70
  if (priceRange) {
71
71
  ensureBoolFilterExists(query);
72
- query.bool.filter = query.bool.filter.concat(createPriceFilters(priceRange, false, !!groupByProduct));
72
+ query.bool.filter = query.bool.filter.concat(createPriceFilters(priceRange, false));
73
73
  }
74
74
  if (priceRangeWithTax) {
75
75
  ensureBoolFilterExists(query);
76
- query.bool.filter = query.bool.filter.concat(createPriceFilters(priceRangeWithTax, true, !!groupByProduct));
76
+ query.bool.filter = query.bool.filter.concat(createPriceFilters(priceRangeWithTax, true));
77
77
  }
78
78
  const sortArray = [];
79
79
  if (sort) {
@@ -83,11 +83,11 @@ function buildElasticBody(input, searchConfig, channelId, languageCode, enabledO
83
83
  });
84
84
  }
85
85
  if (sort.price) {
86
- const priceField = groupByProduct ? 'priceMin' : 'price';
86
+ const priceField = 'price';
87
87
  sortArray.push({ [priceField]: { order: sort.price === generated_types_1.SortOrder.ASC ? 'asc' : 'desc' } });
88
88
  }
89
89
  }
90
- return {
90
+ const body = {
91
91
  query: searchConfig.mapQuery
92
92
  ? searchConfig.mapQuery(query, input, searchConfig, channelId, enabledOnly)
93
93
  : query,
@@ -96,6 +96,10 @@ function buildElasticBody(input, searchConfig, channelId, languageCode, enabledO
96
96
  size: take || 10,
97
97
  track_total_hits: searchConfig.totalItemsMaxSize,
98
98
  };
99
+ if (groupByProduct) {
100
+ body.collapse = { field: `productId` };
101
+ }
102
+ return body;
99
103
  }
100
104
  exports.buildElasticBody = buildElasticBody;
101
105
  function ensureBoolFilterExists(query) {
@@ -103,37 +107,17 @@ function ensureBoolFilterExists(query) {
103
107
  query.bool.filter = [];
104
108
  }
105
109
  }
106
- function createPriceFilters(range, withTax, groupByProduct) {
110
+ function createPriceFilters(range, withTax) {
107
111
  const withTaxFix = withTax ? 'WithTax' : '';
108
- if (groupByProduct) {
109
- return [
110
- {
111
- range: {
112
- [`price${withTaxFix}Min`]: {
113
- gte: range.min,
114
- },
115
- },
116
- },
117
- {
118
- range: {
119
- [`price${withTaxFix}Max`]: {
120
- lte: range.max,
121
- },
112
+ return [
113
+ {
114
+ range: {
115
+ ['price' + withTaxFix]: {
116
+ gte: range.min,
117
+ lte: range.max,
122
118
  },
123
119
  },
124
- ];
125
- }
126
- else {
127
- return [
128
- {
129
- range: {
130
- ['price' + withTaxFix]: {
131
- gte: range.min,
132
- lte: range.max,
133
- },
134
- },
135
- },
136
- ];
137
- }
120
+ },
121
+ ];
138
122
  }
139
123
  //# sourceMappingURL=build-elastic-body.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-elastic-body.js","sourceRoot":"","sources":["../../src/build-elastic-body.ts"],"names":[],"mappings":";;;AAAA,yEAA2G;AAC3G,wCAAiE;AAKjE;;GAEG;AACH,SAAgB,gBAAgB,CAC5B,KAAyB,EACzB,YAAwC,EACxC,SAAa,EACb,YAA0B,EAC1B,cAAuB,KAAK;IAE5B,MAAM,EACF,IAAI,EACJ,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,cAAc,EACd,cAAc,EACd,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GACpB,GAAG,KAAK,CAAC;IACV,MAAM,KAAK,GAAQ;QACf,IAAI,EAAE,EAAE;KACX,CAAC;IACF,sBAAsB,CAAC,KAAK,CAAC,CAAC;IAC9B,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;IAChD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC;IAEnD,IAAI,IAAI,EAAE;QAC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;KACnD,CAAC;AACN,CAAC;AAnHD,4CAmHC;AAED,SAAS,sBAAsB,CAAC,KAAiC;IAC7D,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;QACpB,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;KAC1B;AACL,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAiB,EAAE,OAAgB,EAAE,cAAuB;IACpF,MAAM,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5C,IAAI,cAAc,EAAE;QAChB,OAAO;YACH;gBACI,KAAK,EAAE;oBACH,CAAC,QAAQ,UAAU,KAAK,CAAC,EAAE;wBACvB,GAAG,EAAE,KAAK,CAAC,GAAG;qBACjB;iBACJ;aACJ;YACD;gBACI,KAAK,EAAE;oBACH,CAAC,QAAQ,UAAU,KAAK,CAAC,EAAE;wBACvB,GAAG,EAAE,KAAK,CAAC,GAAG;qBACjB;iBACJ;aACJ;SACJ,CAAC;KACL;SAAM;QACH,OAAO;YACH;gBACI,KAAK,EAAE;oBACH,CAAC,OAAO,GAAG,UAAU,CAAC,EAAE;wBACpB,GAAG,EAAE,KAAK,CAAC,GAAG;wBACd,GAAG,EAAE,KAAK,CAAC,GAAG;qBACjB;iBACJ;aACJ;SACJ,CAAC;KACL;AACL,CAAC"}
1
+ {"version":3,"file":"build-elastic-body.js","sourceRoot":"","sources":["../../src/build-elastic-body.ts"],"names":[],"mappings":";;;AAAA,yEAA2G;AAC3G,wCAAiE;AAKjE;;GAEG;AACH,SAAgB,gBAAgB,CAC5B,KAAyB,EACzB,YAAwC,EACxC,SAAa,EACb,YAA0B,EAC1B,cAAuB,KAAK;IAE5B,MAAM,EACF,IAAI,EACJ,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,cAAc,EACd,cAAc,EACd,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GACpB,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,YAAY,CAAC,QAAQ;YACxB,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,CAAC;YAC3E,CAAC,CAAC,KAAK;QACX,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,IAAI,IAAI,CAAC;QACf,IAAI,EAAE,IAAI,IAAI,EAAE;QAChB,gBAAgB,EAAE,YAAY,CAAC,iBAAiB;KACnD,CAAC;IACF,IAAI,cAAc,EAAE;QAChB,IAAI,CAAC,QAAQ,GAAG,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;KAC1C;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAvHD,4CAuHC;AAED,SAAS,sBAAsB,CAAC,KAAiC;IAC7D,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;QACpB,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;KAC1B;AACL,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAiB,EAAE,OAAgB;IAC3D,MAAM,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5C,OAAO;QACH;YACI,KAAK,EAAE;gBACH,CAAC,OAAO,GAAG,UAAU,CAAC,EAAE;oBACpB,GAAG,EAAE,KAAK,CAAC,GAAG;oBACd,GAAG,EAAE,KAAK,CAAC,GAAG;iBACjB;aACJ;SACJ;KACJ,CAAC;AACN,CAAC"}
@@ -1,4 +1,3 @@
1
1
  export declare const ELASTIC_SEARCH_OPTIONS: unique symbol;
2
2
  export declare const VARIANT_INDEX_NAME = "variants";
3
- export declare const PRODUCT_INDEX_NAME = "products";
4
3
  export declare const loggerCtx = "ElasticsearchPlugin";
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loggerCtx = exports.PRODUCT_INDEX_NAME = exports.VARIANT_INDEX_NAME = exports.ELASTIC_SEARCH_OPTIONS = void 0;
3
+ exports.loggerCtx = exports.VARIANT_INDEX_NAME = exports.ELASTIC_SEARCH_OPTIONS = void 0;
4
4
  exports.ELASTIC_SEARCH_OPTIONS = Symbol('ELASTIC_SEARCH_OPTIONS');
5
5
  exports.VARIANT_INDEX_NAME = 'variants';
6
- exports.PRODUCT_INDEX_NAME = 'products';
7
6
  exports.loggerCtx = 'ElasticsearchPlugin';
8
7
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAC1D,QAAA,kBAAkB,GAAG,UAAU,CAAC;AAChC,QAAA,kBAAkB,GAAG,UAAU,CAAC;AAChC,QAAA,SAAS,GAAG,qBAAqB,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAC1D,QAAA,kBAAkB,GAAG,UAAU,CAAC;AAChC,QAAA,SAAS,GAAG,qBAAqB,CAAC"}
@@ -1,9 +1,9 @@
1
1
  import { Job as GraphQLJob, QuerySearchArgs, SearchResponse } from '@vendure/common/lib/generated-types';
2
2
  import { Omit } from '@vendure/common/lib/omit';
3
- import { Collection, FacetValue, RequestContext, SearchResolver } from '@vendure/core';
3
+ import { Collection, FacetValue, RequestContext, SearchJobBufferService, SearchResolver } from '@vendure/core';
4
4
  import { ElasticsearchService } from './elasticsearch.service';
5
5
  import { ElasticSearchInput, SearchPriceData } from './types';
6
- export declare class ShopElasticSearchResolver implements Omit<SearchResolver, 'facetValues' | 'collections' | 'reindex'> {
6
+ export declare class ShopElasticSearchResolver implements Pick<SearchResolver, 'search'> {
7
7
  private elasticsearchService;
8
8
  constructor(elasticsearchService: ElasticsearchService);
9
9
  search(ctx: RequestContext, args: QuerySearchArgs): Promise<Omit<SearchResponse, 'facetValues' | 'collections'>>;
@@ -11,11 +11,14 @@ export declare class ShopElasticSearchResolver implements Omit<SearchResolver, '
11
11
  input: ElasticSearchInput;
12
12
  }): Promise<SearchPriceData>;
13
13
  }
14
- export declare class AdminElasticSearchResolver implements Omit<SearchResolver, 'facetValues' | 'collections'> {
14
+ export declare class AdminElasticSearchResolver implements Pick<SearchResolver, 'search' | 'reindex'> {
15
15
  private elasticsearchService;
16
- constructor(elasticsearchService: ElasticsearchService);
16
+ private searchJobBufferService;
17
+ constructor(elasticsearchService: ElasticsearchService, searchJobBufferService: SearchJobBufferService);
17
18
  search(ctx: RequestContext, args: QuerySearchArgs): Promise<Omit<SearchResponse, 'facetValues' | 'collections'>>;
18
19
  reindex(ctx: RequestContext): Promise<GraphQLJob>;
20
+ pendingSearchIndexUpdates(...args: any[]): Promise<any>;
21
+ runPendingSearchIndexUpdates(...args: any[]): Promise<any>;
19
22
  }
20
23
  export declare class EntityElasticSearchResolver implements Pick<SearchResolver, 'facetValues' | 'collections'> {
21
24
  private elasticsearchService;
@@ -54,8 +54,9 @@ ShopElasticSearchResolver = __decorate([
54
54
  ], ShopElasticSearchResolver);
55
55
  exports.ShopElasticSearchResolver = ShopElasticSearchResolver;
56
56
  let AdminElasticSearchResolver = class AdminElasticSearchResolver {
57
- constructor(elasticsearchService) {
57
+ constructor(elasticsearchService, searchJobBufferService) {
58
58
  this.elasticsearchService = elasticsearchService;
59
+ this.searchJobBufferService = searchJobBufferService;
59
60
  }
60
61
  async search(ctx, args) {
61
62
  const result = await this.elasticsearchService.search(ctx, args.input, false);
@@ -66,6 +67,14 @@ let AdminElasticSearchResolver = class AdminElasticSearchResolver {
66
67
  async reindex(ctx) {
67
68
  return this.elasticsearchService.reindex(ctx);
68
69
  }
70
+ async pendingSearchIndexUpdates(...args) {
71
+ return this.searchJobBufferService.getPendingSearchUpdates();
72
+ }
73
+ async runPendingSearchIndexUpdates(...args) {
74
+ // Intentionally not awaiting this method call
75
+ this.searchJobBufferService.runPendingSearchUpdates();
76
+ return { success: true };
77
+ }
69
78
  };
70
79
  __decorate([
71
80
  graphql_1.Query(),
@@ -84,9 +93,24 @@ __decorate([
84
93
  __metadata("design:paramtypes", [core_1.RequestContext]),
85
94
  __metadata("design:returntype", Promise)
86
95
  ], AdminElasticSearchResolver.prototype, "reindex", null);
96
+ __decorate([
97
+ graphql_1.Query(),
98
+ core_1.Allow(generated_types_1.Permission.UpdateCatalog, generated_types_1.Permission.UpdateProduct),
99
+ __metadata("design:type", Function),
100
+ __metadata("design:paramtypes", [Object]),
101
+ __metadata("design:returntype", Promise)
102
+ ], AdminElasticSearchResolver.prototype, "pendingSearchIndexUpdates", null);
103
+ __decorate([
104
+ graphql_1.Mutation(),
105
+ core_1.Allow(generated_types_1.Permission.UpdateCatalog, generated_types_1.Permission.UpdateProduct),
106
+ __metadata("design:type", Function),
107
+ __metadata("design:paramtypes", [Object]),
108
+ __metadata("design:returntype", Promise)
109
+ ], AdminElasticSearchResolver.prototype, "runPendingSearchIndexUpdates", null);
87
110
  AdminElasticSearchResolver = __decorate([
88
111
  graphql_1.Resolver('SearchResponse'),
89
- __metadata("design:paramtypes", [elasticsearch_service_1.ElasticsearchService])
112
+ __metadata("design:paramtypes", [elasticsearch_service_1.ElasticsearchService,
113
+ core_1.SearchJobBufferService])
90
114
  ], AdminElasticSearchResolver);
91
115
  exports.AdminElasticSearchResolver = AdminElasticSearchResolver;
92
116
  let EntityElasticSearchResolver = class EntityElasticSearchResolver {
@@ -1 +1 @@
1
- {"version":3,"file":"elasticsearch-resolver.js","sourceRoot":"","sources":["../../src/elasticsearch-resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,6CAAwF;AACxF,yEAK6C;AAE7C,wCAAmG;AAEnG,mEAA+D;AAI/D,IAAa,yBAAyB,GAAtC,MAAa,yBAAyB;IAElC,YAAoB,oBAA0C;QAA1C,yBAAoB,GAApB,oBAAoB,CAAsB;IAAG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;CACJ,CAAA;AAhBG;IADC,sBAAY,EAAE;IAEV,WAAA,UAAG,EAAE,CAAA;IACL,WAAA,gBAAM,EAAE,CAAA;;qCADG,qBAAc;;8DAK7B;AAGD;IADC,sBAAY,EAAE;IAEV,WAAA,UAAG,EAAE,CAAA;IACL,WAAA,gBAAM,EAAE,CAAA;;qCADG,qBAAc;;8DAK7B;AAnBQ,2BAA2B;IADvC,kBAAQ,CAAC,gBAAgB,CAAC;qCAEmB,4CAAoB;GADrD,2BAA2B,CAoBvC;AApBY,kEAA2B"}
@@ -20,6 +20,7 @@ export declare class ElasticsearchService implements OnModuleInit, OnModuleDestr
20
20
  * Perform a fulltext search according to the provided input arguments.
21
21
  */
22
22
  search(ctx: RequestContext, input: ElasticSearchInput, enabledOnly?: boolean): Promise<Omit<ElasticSearchResponse, 'facetValues' | 'collections' | 'priceRange'>>;
23
+ totalHits(ctx: RequestContext, input: ElasticSearchInput, enabledOnly?: boolean): Promise<number>;
23
24
  /**
24
25
  * Return a list of all FacetValues which appear in the result set.
25
26
  */
@@ -34,6 +35,13 @@ export declare class ElasticsearchService implements OnModuleInit, OnModuleDestr
34
35
  collection: Collection;
35
36
  count: number;
36
37
  }>>;
38
+ getDistinctBucketsOfField(ctx: RequestContext, input: ElasticSearchInput, enabledOnly: boolean | undefined, field: string, aggregation_max_size: number): Promise<Array<{
39
+ key: string;
40
+ doc_count: number;
41
+ total: {
42
+ value: number;
43
+ };
44
+ }>>;
37
45
  priceRange(ctx: RequestContext, input: ElasticSearchInput): Promise<SearchPriceData>;
38
46
  /**
39
47
  * Rebuilds the full search index.
@@ -42,5 +50,5 @@ export declare class ElasticsearchService implements OnModuleInit, OnModuleDestr
42
50
  private mapVariantToSearchResult;
43
51
  private mapProductToSearchResult;
44
52
  private getSearchResultAssets;
45
- private addCustomMappings;
53
+ private static addCustomMappings;
46
54
  }
@@ -14,9 +14,9 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14
14
  var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  return (mod && mod.__esModule) ? mod : { "default": mod };
16
16
  };
17
+ var ElasticsearchService_1;
17
18
  Object.defineProperty(exports, "__esModule", { value: true });
18
19
  exports.ElasticsearchService = void 0;
19
- const elasticsearch_1 = require("@elastic/elasticsearch");
20
20
  const common_1 = require("@nestjs/common");
21
21
  const core_1 = require("@vendure/core");
22
22
  const es6_1 = __importDefault(require("fast-deep-equal/es6"));
@@ -24,7 +24,7 @@ const build_elastic_body_1 = require("./build-elastic-body");
24
24
  const constants_1 = require("./constants");
25
25
  const elasticsearch_index_service_1 = require("./elasticsearch-index.service");
26
26
  const indexing_utils_1 = require("./indexing-utils");
27
- let ElasticsearchService = class ElasticsearchService {
27
+ let ElasticsearchService = ElasticsearchService_1 = class ElasticsearchService {
28
28
  constructor(options, searchService, elasticsearchIndexService, configService, facetValueService, collectionService) {
29
29
  this.options = options;
30
30
  this.searchService = searchService;
@@ -35,10 +35,7 @@ let ElasticsearchService = class ElasticsearchService {
35
35
  searchService.adopt(this);
36
36
  }
37
37
  onModuleInit() {
38
- var _a, _b;
39
- const { host, port } = this.options;
40
- const node = (_b = (_a = this.options.clientOptions) === null || _a === void 0 ? void 0 : _a.node) !== null && _b !== void 0 ? _b : `${host}:${port}`;
41
- this.client = new elasticsearch_1.Client(Object.assign({ node }, this.options.clientOptions));
38
+ this.client = indexing_utils_1.getClient(this.options);
42
39
  }
43
40
  onModuleDestroy() {
44
41
  return this.client.close();
@@ -73,7 +70,7 @@ let ElasticsearchService = class ElasticsearchService {
73
70
  const result = await this.client.indices.exists({ index });
74
71
  if (!result.body) {
75
72
  core_1.Logger.verbose(`Index "${index}" does not exist. Creating...`, constants_1.loggerCtx);
76
- await indexing_utils_1.createIndices(this.client, indexPrefix, this.options.indexSettings, this.options.indexMappingProperties, this.configService.entityIdStrategy.primaryKeyType);
73
+ await indexing_utils_1.createIndices(this.client, indexPrefix, this.options.indexSettings, this.options.indexMappingProperties);
77
74
  }
78
75
  else {
79
76
  core_1.Logger.verbose(`Index "${index}" exists`, constants_1.loggerCtx);
@@ -84,7 +81,7 @@ let ElasticsearchService = class ElasticsearchService {
84
81
  const nameSalt = Math.random().toString(36).substring(7);
85
82
  const tempPrefix = `temp-` + `${tempName}-${nameSalt}-`;
86
83
  const tempIndex = tempPrefix + indexName;
87
- await indexing_utils_1.createIndices(this.client, tempPrefix, this.options.indexSettings, this.options.indexMappingProperties, this.configService.entityIdStrategy.primaryKeyType, false);
84
+ await indexing_utils_1.createIndices(this.client, tempPrefix, this.options.indexSettings, this.options.indexMappingProperties, false);
88
85
  const tempIndexSettingsResult = await this.client.indices.getSettings({ index: tempIndex });
89
86
  const tempIndexSettings = tempIndexSettingsResult.body[tempIndex].settings.index;
90
87
  const indexParamsToExclude = [
@@ -115,12 +112,11 @@ let ElasticsearchService = class ElasticsearchService {
115
112
  core_1.Logger.warn(`Index "${index}" mapping differs from index mapping in vendure config! Consider re-indexing the data.`, constants_1.loggerCtx);
116
113
  }
117
114
  await this.client.indices.delete({
118
- index: [tempPrefix + `products`, tempPrefix + `variants`],
115
+ index: [tempPrefix + `variants`],
119
116
  });
120
117
  }
121
118
  };
122
119
  await createIndex(constants_1.VARIANT_INDEX_NAME);
123
- await createIndex(constants_1.PRODUCT_INDEX_NAME);
124
120
  }
125
121
  /**
126
122
  * Perform a fulltext search according to the provided input arguments.
@@ -132,12 +128,13 @@ let ElasticsearchService = class ElasticsearchService {
132
128
  if (groupByProduct) {
133
129
  try {
134
130
  const { body } = await this.client.search({
135
- index: indexPrefix + constants_1.PRODUCT_INDEX_NAME,
131
+ index: indexPrefix + constants_1.VARIANT_INDEX_NAME,
136
132
  body: elasticSearchBody,
137
133
  });
134
+ const totalItems = await this.totalHits(ctx, input, groupByProduct);
138
135
  return {
139
136
  items: body.hits.hits.map(hit => this.mapProductToSearchResult(hit)),
140
- totalItems: body.hits.total ? body.hits.total.value : 0,
137
+ totalItems,
141
138
  };
142
139
  }
143
140
  catch (e) {
@@ -162,41 +159,47 @@ let ElasticsearchService = class ElasticsearchService {
162
159
  }
163
160
  }
164
161
  }
165
- /**
166
- * Return a list of all FacetValues which appear in the result set.
167
- */
168
- async facetValues(ctx, input, enabledOnly = false) {
169
- const { indexPrefix } = this.options;
170
- const elasticSearchBody = build_elastic_body_1.buildElasticBody(input, this.options.searchConfig, ctx.channelId, ctx.languageCode, enabledOnly);
162
+ async totalHits(ctx, input, enabledOnly = false) {
163
+ const { indexPrefix, searchConfig } = this.options;
164
+ const elasticSearchBody = build_elastic_body_1.buildElasticBody(input, searchConfig, ctx.channelId, ctx.languageCode, enabledOnly);
171
165
  elasticSearchBody.from = 0;
172
166
  elasticSearchBody.size = 0;
173
167
  elasticSearchBody.aggs = {
174
- facetValue: {
175
- terms: {
176
- field: 'facetValueIds',
177
- size: this.options.searchConfig.facetValueMaxSize,
168
+ total: {
169
+ cardinality: {
170
+ field: `productId`,
178
171
  },
179
172
  },
180
173
  };
181
- let body;
182
- try {
183
- const result = await this.client.search({
184
- index: indexPrefix + (input.groupByProduct ? constants_1.PRODUCT_INDEX_NAME : constants_1.VARIANT_INDEX_NAME),
185
- body: elasticSearchBody,
186
- });
187
- body = result.body;
188
- }
189
- catch (e) {
190
- core_1.Logger.error(e.message, constants_1.loggerCtx, e.stack);
191
- throw e;
174
+ const { body } = await this.client.search({
175
+ index: indexPrefix + constants_1.VARIANT_INDEX_NAME,
176
+ body: elasticSearchBody,
177
+ });
178
+ const { aggregations } = body;
179
+ if (!aggregations) {
180
+ throw new core_1.InternalServerError('An error occurred when querying Elasticsearch for priceRange aggregations');
192
181
  }
193
- const buckets = body.aggregations ? body.aggregations.facetValue.buckets : [];
182
+ return aggregations.total ? aggregations.total.value : 0;
183
+ }
184
+ /**
185
+ * Return a list of all FacetValues which appear in the result set.
186
+ */
187
+ async facetValues(ctx, input, enabledOnly = false) {
188
+ const { groupByProduct } = input;
189
+ const buckets = await this.getDistinctBucketsOfField(ctx, input, enabledOnly, `facetValueIds`, this.options.searchConfig.facetValueMaxSize);
194
190
  const facetValues = await this.facetValueService.findByIds(ctx, buckets.map(b => b.key));
195
- return facetValues.map((facetValue, index) => {
191
+ return facetValues.map(facetValue => {
196
192
  const bucket = buckets.find(b => b.key.toString() === facetValue.id.toString());
193
+ let count;
194
+ if (groupByProduct) {
195
+ count = bucket ? bucket.total.value : 0;
196
+ }
197
+ else {
198
+ count = bucket ? bucket.doc_count : 0;
199
+ }
197
200
  return {
198
201
  facetValue,
199
- count: bucket ? bucket.doc_count : 0,
202
+ count,
200
203
  };
201
204
  });
202
205
  }
@@ -204,22 +207,51 @@ let ElasticsearchService = class ElasticsearchService {
204
207
  * Return a list of all Collections which appear in the result set.
205
208
  */
206
209
  async collections(ctx, input, enabledOnly = false) {
210
+ const { groupByProduct } = input;
211
+ const buckets = await this.getDistinctBucketsOfField(ctx, input, enabledOnly, `collectionIds`, this.options.searchConfig.collectionMaxSize);
212
+ const collections = await this.collectionService.findByIds(ctx, buckets.map(b => b.key));
213
+ return collections.map(collection => {
214
+ const bucket = buckets.find(b => b.key.toString() === collection.id.toString());
215
+ let count;
216
+ if (groupByProduct) {
217
+ count = bucket ? bucket.total.value : 0;
218
+ }
219
+ else {
220
+ count = bucket ? bucket.doc_count : 0;
221
+ }
222
+ return {
223
+ collection,
224
+ count,
225
+ };
226
+ });
227
+ }
228
+ async getDistinctBucketsOfField(ctx, input, enabledOnly = false, field, aggregation_max_size) {
207
229
  const { indexPrefix } = this.options;
230
+ const { groupByProduct } = input;
208
231
  const elasticSearchBody = build_elastic_body_1.buildElasticBody(input, this.options.searchConfig, ctx.channelId, ctx.languageCode, enabledOnly);
209
232
  elasticSearchBody.from = 0;
210
233
  elasticSearchBody.size = 0;
211
234
  elasticSearchBody.aggs = {
212
- collection: {
235
+ aggregation_field: {
213
236
  terms: {
214
- field: 'collectionIds',
215
- size: this.options.searchConfig.collectionMaxSize,
237
+ field,
238
+ size: aggregation_max_size,
216
239
  },
217
240
  },
218
241
  };
242
+ if (groupByProduct) {
243
+ elasticSearchBody.aggs.aggregation_field.aggs = {
244
+ total: {
245
+ cardinality: {
246
+ field: `productId`,
247
+ },
248
+ },
249
+ };
250
+ }
219
251
  let body;
220
252
  try {
221
253
  const result = await this.client.search({
222
- index: indexPrefix + (input.groupByProduct ? constants_1.PRODUCT_INDEX_NAME : constants_1.VARIANT_INDEX_NAME),
254
+ index: indexPrefix + constants_1.VARIANT_INDEX_NAME,
223
255
  body: elasticSearchBody,
224
256
  });
225
257
  body = result.body;
@@ -228,58 +260,49 @@ let ElasticsearchService = class ElasticsearchService {
228
260
  core_1.Logger.error(e.message, constants_1.loggerCtx, e.stack);
229
261
  throw e;
230
262
  }
231
- const buckets = body.aggregations ? body.aggregations.collection.buckets : [];
232
- const collections = await this.collectionService.findByIds(ctx, buckets.map(b => b.key));
233
- return collections.map(collection => {
234
- const bucket = buckets.find(b => b.key.toString() === collection.id.toString());
235
- return {
236
- collection,
237
- count: bucket ? bucket.doc_count : 0,
238
- };
239
- });
263
+ return body.aggregations ? body.aggregations.aggregation_field.buckets : [];
240
264
  }
241
265
  async priceRange(ctx, input) {
242
266
  const { indexPrefix, searchConfig } = this.options;
243
- const { groupByProduct } = input;
244
267
  const elasticSearchBody = build_elastic_body_1.buildElasticBody(input, searchConfig, ctx.channelId, ctx.languageCode, true);
245
268
  elasticSearchBody.from = 0;
246
269
  elasticSearchBody.size = 0;
247
270
  elasticSearchBody.aggs = {
248
271
  minPrice: {
249
272
  min: {
250
- field: groupByProduct ? 'priceMin' : 'price',
273
+ field: 'price',
251
274
  },
252
275
  },
253
276
  minPriceWithTax: {
254
277
  min: {
255
- field: groupByProduct ? 'priceWithTaxMin' : 'priceWithTax',
278
+ field: 'priceWithTax',
256
279
  },
257
280
  },
258
281
  maxPrice: {
259
282
  max: {
260
- field: groupByProduct ? 'priceMax' : 'price',
283
+ field: 'price',
261
284
  },
262
285
  },
263
286
  maxPriceWithTax: {
264
287
  max: {
265
- field: groupByProduct ? 'priceWithTaxMax' : 'priceWithTax',
288
+ field: 'priceWithTax',
266
289
  },
267
290
  },
268
291
  prices: {
269
292
  histogram: {
270
- field: groupByProduct ? 'priceMin' : 'price',
293
+ field: 'price',
271
294
  interval: searchConfig.priceRangeBucketInterval,
272
295
  },
273
296
  },
274
297
  pricesWithTax: {
275
298
  histogram: {
276
- field: groupByProduct ? 'priceWithTaxMin' : 'priceWithTax',
299
+ field: 'priceWithTax',
277
300
  interval: searchConfig.priceRangeBucketInterval,
278
301
  },
279
302
  },
280
303
  };
281
304
  const { body } = await this.client.search({
282
- index: indexPrefix + (input.groupByProduct ? constants_1.PRODUCT_INDEX_NAME : constants_1.VARIANT_INDEX_NAME),
305
+ index: indexPrefix + constants_1.VARIANT_INDEX_NAME,
283
306
  body: elasticSearchBody,
284
307
  });
285
308
  const { aggregations } = body;
@@ -307,7 +330,6 @@ let ElasticsearchService = class ElasticsearchService {
307
330
  * Rebuilds the full search index.
308
331
  */
309
332
  async reindex(ctx) {
310
- const { indexPrefix } = this.options;
311
333
  const job = await this.elasticsearchIndexService.reindex(ctx);
312
334
  // tslint:disable-next-line:no-non-null-assertion
313
335
  return job;
@@ -321,21 +343,21 @@ let ElasticsearchService = class ElasticsearchService {
321
343
  }, priceWithTax: {
322
344
  value: source.priceWithTax,
323
345
  }, score: hit._score || 0 });
324
- this.addCustomMappings(result, source, this.options.customProductVariantMappings);
346
+ ElasticsearchService_1.addCustomMappings(result, source, this.options.customProductVariantMappings, false);
325
347
  return result;
326
348
  }
327
349
  mapProductToSearchResult(hit) {
328
350
  const source = hit._source;
329
351
  const { productAsset, productVariantAsset } = this.getSearchResultAssets(source);
330
352
  const result = Object.assign(Object.assign({}, source), { productAsset,
331
- productVariantAsset, productId: source.productId.toString(), productName: source.productName, productVariantId: source.productVariantId.toString(), productVariantName: source.productVariantName, facetIds: source.facetIds, facetValueIds: source.facetValueIds, collectionIds: source.collectionIds, sku: source.sku, slug: source.slug, price: {
332
- min: source.priceMin,
333
- max: source.priceMax,
353
+ productVariantAsset, enabled: source.productEnabled, productId: source.productId.toString(), productName: source.productName, productVariantId: source.productVariantId.toString(), productVariantName: source.productVariantName, facetIds: source.productFacetIds, facetValueIds: source.productFacetValueIds, collectionIds: source.productCollectionIds, sku: source.sku, slug: source.slug, price: {
354
+ min: source.productPriceMin,
355
+ max: source.productPriceMax,
334
356
  }, priceWithTax: {
335
- min: source.priceWithTaxMin,
336
- max: source.priceWithTaxMax,
357
+ min: source.productPriceWithTaxMin,
358
+ max: source.productPriceWithTaxMax,
337
359
  }, channelIds: [], score: hit._score || 0 });
338
- this.addCustomMappings(result, source, this.options.customProductMappings);
360
+ ElasticsearchService_1.addCustomMappings(result, source, this.options.customProductMappings, true);
339
361
  return result;
340
362
  }
341
363
  getSearchResultAssets(source) {
@@ -355,19 +377,19 @@ let ElasticsearchService = class ElasticsearchService {
355
377
  : undefined;
356
378
  return { productAsset, productVariantAsset };
357
379
  }
358
- addCustomMappings(result, source, mappings) {
380
+ static addCustomMappings(result, source, mappings, groupByProduct) {
359
381
  const customMappings = Object.keys(mappings);
360
382
  if (customMappings.length) {
361
383
  const customMappingsResult = {};
362
384
  for (const name of customMappings) {
363
- customMappingsResult[name] = source[name];
385
+ customMappingsResult[name] = source[groupByProduct ? `product-${name}` : `variant-${name}`];
364
386
  }
365
387
  result.customMappings = customMappingsResult;
366
388
  }
367
389
  return result;
368
390
  }
369
391
  };
370
- ElasticsearchService = __decorate([
392
+ ElasticsearchService = ElasticsearchService_1 = __decorate([
371
393
  common_1.Injectable(),
372
394
  __param(0, common_1.Inject(constants_1.ELASTIC_SEARCH_OPTIONS)),
373
395
  __metadata("design:paramtypes", [Object, core_1.SearchService,