Răsfoiți Sursa

feat(elastic): add totalItemsMaxSize option (#977)

Relates to #976
Artem Danilov 4 ani în urmă
părinte
comite
52dccadb3e

+ 1 - 1
packages/elasticsearch-plugin/docker-compose.yml

@@ -1,7 +1,7 @@
 version: "3"
 services:
   elasticsearch:
-    image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1
+    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
     container_name: elasticsearch
     environment:
       - discovery.type=single-node

+ 2 - 0
packages/elasticsearch-plugin/src/build-elastic-body.spec.ts

@@ -230,6 +230,7 @@ describe('buildElasticBody()', () => {
             size: 10,
             query: { bool: { filter: [CHANNEL_ID_TERM, LANGUAGE_CODE_TERM] } },
             sort: [],
+            track_total_hits: 10000,
         });
     });
 
@@ -329,6 +330,7 @@ describe('buildElasticBody()', () => {
                 },
             },
             sort: [{ 'productName.keyword': { order: 'desc' } }],
+            track_total_hits: 10000,
         });
     });
 

+ 1 - 0
packages/elasticsearch-plugin/src/build-elastic-body.ts

@@ -120,6 +120,7 @@ export function buildElasticBody(
         sort: sortArray,
         from: skip || 0,
         size: take || 10,
+        track_total_hits: searchConfig.totalItemsMaxSize,
     };
 }
 

+ 2 - 2
packages/elasticsearch-plugin/src/elasticsearch.service.ts

@@ -135,7 +135,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                 });
                 return {
                     items: body.hits.hits.map(hit => this.mapProductToSearchResult(hit)),
-                    totalItems: body.hits.total.value,
+                    totalItems: body.hits.total ? body.hits.total.value : 0,
                 };
             } catch (e) {
                 Logger.error(e.message, loggerCtx, e.stack);
@@ -149,7 +149,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                 });
                 return {
                     items: body.hits.hits.map(hit => this.mapVariantToSearchResult(hit)),
-                    totalItems: body.hits.total.value,
+                    totalItems: body.hits.total ? body.hits.total.value : 0,
                 };
             } catch (e) {
                 Logger.error(e.message, loggerCtx, e.stack);

+ 14 - 0
packages/elasticsearch-plugin/src/options.ts

@@ -173,6 +173,19 @@ export interface SearchConfig {
      */
     collectionMaxSize?: number;
 
+    /**
+     * @description
+     * The maximum number of totalItems to return from the search query. Internally, this
+     * value sets the "track_total_hits" property of an Elasticsearch query.
+     * If this parameter is set to "True", accurate count of totalItems will be returned.
+     * If this parameter is set to "False", totalItems will be returned as 0.
+     * If this parameter is set to integer, accurate count of totalItems will be returned not bigger than integer.
+     *
+     * @default
+     * 10000
+     */
+    totalItemsMaxSize?: number|boolean;
+
     // prettier-ignore
     /**
      * @description
@@ -326,6 +339,7 @@ export const defaultOptions: ElasticsearchRuntimeOptions = {
     searchConfig: {
         facetValueMaxSize: 50,
         collectionMaxSize: 50,
+        totalItemsMaxSize: 10000,
         multiMatchType: 'best_fields',
         boostFields: {
             productName: 1,

+ 1 - 0
packages/elasticsearch-plugin/src/types.ts

@@ -90,6 +90,7 @@ export type SearchRequestBody = {
     sort?: any[];
     from?: number;
     size?: number;
+    track_total_hits?: number|boolean,
     aggs?: any;
 };