From eebb5bae4a57780f1dca7423801328e382e6f54e Mon Sep 17 00:00:00 2001 From: Jannic Veith Date: Wed, 4 Sep 2024 13:28:21 +0200 Subject: [PATCH 1/2] Increase bucket limit --- .../assets/search/asset-search.service.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts b/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts index 9f7afd8f..046a2f91 100644 --- a/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts +++ b/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts @@ -25,6 +25,7 @@ import { BulkOperationContainer, QueryDslNumberRangeQuery, QueryDslQueryContainer, + SearchRequest, SearchTotalHits, } from '@elastic/elasticsearch/lib/api/types'; import { Injectable } from '@nestjs/common'; @@ -231,16 +232,25 @@ export class AssetSearchService { doc_count: number; } - const aggregateGroup = async (query: AssetSearchQuery, operator: string, groupName: string, fieldName?: string) => { + const aggregateGroup = async ( + query: AssetSearchQuery, + operator: 'terms' | 'min' | 'max', + groupName: string, + fieldName?: string + ) => { const elasticDslQuery = mapQueryToElasticDsl({ ...query, [groupName]: undefined }); + const field: { field: string; size?: number } = { field: fieldName ?? groupName, size: 10_000 }; + if (operator !== 'terms') { + delete field.size; + } return ( await this.elastic.search({ index: INDEX, size: 0, query: elasticDslQuery, track_total_hits: true, - aggs: { - agg: { [operator]: { field: fieldName ?? groupName } }, + aggregations: { + agg: { [operator]: field }, }, }) ).aggregations?.agg; From 728c713f07131246c4b119e6deb1a5d0e279449a Mon Sep 17 00:00:00 2001 From: Jannic Veith Date: Mon, 9 Sep 2024 10:19:47 +0200 Subject: [PATCH 2/2] Use named variables instead of magical numbers --- .../src/features/assets/search/asset-search.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts b/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts index 046a2f91..b4658797 100644 --- a/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts +++ b/apps/server-asset-sg/src/features/assets/search/asset-search.service.ts @@ -238,8 +238,9 @@ export class AssetSearchService { groupName: string, fieldName?: string ) => { + const NUMBER_OF_BUCKETS = 10_000; const elasticDslQuery = mapQueryToElasticDsl({ ...query, [groupName]: undefined }); - const field: { field: string; size?: number } = { field: fieldName ?? groupName, size: 10_000 }; + const field: { field: string; size?: number } = { field: fieldName ?? groupName, size: NUMBER_OF_BUCKETS }; if (operator !== 'terms') { delete field.size; }