From dcd2da7a81fa488e693c0ad6ecb73aee64c36a54 Mon Sep 17 00:00:00 2001 From: Florent Gravin Date: Thu, 19 Oct 2023 16:33:22 +0200 Subject: [PATCH] feat(search): add * fallback on searched fields if lang=current --- .../elasticsearch.service.spec.ts | 27 ++++++++++++++++++- .../elasticsearch/elasticsearch.service.ts | 26 ++++++++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.spec.ts b/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.spec.ts index 9bea27fac6..1e9165077e 100644 --- a/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.spec.ts @@ -362,7 +362,7 @@ describe('ElasticsearchService', () => { }) describe('#injectLangInQueryStringFields - Search language', () => { - const queryStringFields = ['resourceTitleObject.${searchLang}'] + let queryStringFields = ['resourceTitleObject.${searchLang}'] describe('When no lang from config', () => { beforeEach(() => { service['metadataLang'] = undefined @@ -399,6 +399,31 @@ describe('ElasticsearchService', () => { )[1] ).toEqual('langeng') }) + it('add * fallback with low priority', () => { + queryStringFields = [ + 'resourceTitleObject.${searchLang}^5', + 'tag.${searchLang}^4', + 'resourceAbstractObject.${searchLang}^3', + 'lineageObject.${searchLang}^2', + 'any.${searchLang}', + 'uuid', + ] + expect( + service['injectLangInQueryStringFields'](queryStringFields) + ).toEqual([ + 'resourceTitleObject.langeng^5', + 'tag.langeng^4', + 'resourceAbstractObject.langeng^3', + 'lineageObject.langeng^2', + 'any.langeng', + 'uuid', + 'resourceTitleObject.*', + 'tag.*', + 'resourceAbstractObject.*', + 'lineageObject.*', + 'any.*', + ]) + }) }) }) diff --git a/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts b/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts index 14a35832d2..a4a82a76ec 100644 --- a/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts +++ b/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts @@ -171,14 +171,30 @@ export class ElasticsearchService { } private injectLangInQueryStringFields(queryStringFields: string[]) { - const queryLang = this.metadataLang - ? this.metadataLang === 'current' + const queryLang = this.getQueryLang() + return [ + ...queryStringFields.map((field) => { + return field.replace(/\$\{searchLang\}/g, queryLang) + }), + ...(this.isCurrentSearchLang() + ? queryStringFields + .filter((field) => /\$\{searchLang\}/.test(field)) + .map((field) => { + return field.replace(/\.\$\{searchLang\}(\^\d+)?/, '.*') + }) + : []), + ] + } + + private getQueryLang(): string { + return this.metadataLang + ? this.isCurrentSearchLang() ? `lang${this.lang3}` : `lang${this.metadataLang}` : `*` - return queryStringFields.map((field) => { - return field.replace(/\$\{searchLang\}/g, queryLang) - }) + } + private isCurrentSearchLang() { + return this.metadataLang === 'current' } private buildPayloadQuery(