diff --git a/libs/data-access/gn4/src/custom-api/thesaurus.api.service.ts b/libs/data-access/gn4/src/custom-api/thesaurus.api.service.ts index 4da8b73ebf..138d7bfc14 100644 --- a/libs/data-access/gn4/src/custom-api/thesaurus.api.service.ts +++ b/libs/data-access/gn4/src/custom-api/thesaurus.api.service.ts @@ -8,7 +8,7 @@ import { Observable } from 'rxjs' import { CustomHttpParameterCodec } from '../openapi/encoder' import { Configuration } from '../openapi/configuration' -import { BASE_PATH, COLLECTION_FORMATS } from '../openapi/variables' +import { BASE_PATH } from '../openapi/variables' export interface thesaurusResponse { values: { [key: string]: string } @@ -95,7 +95,6 @@ export class ThesaurusApiService { ) } - console.log('here') let headers = this.defaultHeaders let httpHeaderAcceptSelected: string | undefined = diff --git a/libs/feature/search/src/lib/utils/service/fields.service.ts b/libs/feature/search/src/lib/utils/service/fields.service.ts index 07f4061073..7212f4b82f 100644 --- a/libs/feature/search/src/lib/utils/service/fields.service.ts +++ b/libs/feature/search/src/lib/utils/service/fields.service.ts @@ -10,6 +10,7 @@ import { OwnerSearchField, SimpleSearchField, ThesaurusTranslationSearchField, + TranslatedSearchField, } from './fields' import { forkJoin, Observable, of } from 'rxjs' import { map } from 'rxjs/operators' @@ -69,8 +70,8 @@ export class FieldsService { q: new FullTextSearchField(), license: new LicenseSearchField(this.injector), owner: new OwnerSearchField(this.injector), - contact: new GnUiTranslationSearchField( - 'OrgForResource', + contact: new TranslatedSearchField( + 'contactForResource.organisationObject.default', 'asc', this.injector ), diff --git a/libs/feature/search/src/lib/utils/service/fields.ts b/libs/feature/search/src/lib/utils/service/fields.ts index c960cc0a8e..0c640cb079 100644 --- a/libs/feature/search/src/lib/utils/service/fields.ts +++ b/libs/feature/search/src/lib/utils/service/fields.ts @@ -1,5 +1,6 @@ import { firstValueFrom, Observable, of, switchMap } from 'rxjs' import { + SearchApiService, ThesaurusApiService, ToolsApiService, } from '@geonetwork-ui/data-access/gn4' @@ -377,3 +378,82 @@ export class OwnerSearchField extends SimpleSearchField { return of([]) } } + +export class TranslatedSearchField extends SimpleSearchField { + protected searchApiService = this.injector.get(SearchApiService) + + // FIXME: this is required to register runtime fields; abstract this as well + protected esService = this.injector.get(ElasticsearchService) + private langService = this.injector.get(LangService) + private esResearchName: string + + constructor( + esFieldName: string, + order: 'asc' | 'desc' = 'asc', + injector: Injector + ) { + super(esFieldName, order, injector) + this.esResearchName = this.esFieldName.substring( + 0, + this.esFieldName.lastIndexOf('.') + ) + } + + getTranslatedAggregations() { + return this.searchApiService + .search( + 'bucket', + JSON.stringify( + this.esService.getSearchRequestBody({ + [this.esFieldName.split('.')[0]]: { + nested: { + path: this.esFieldName.split('.')[0], + }, + aggs: { + default: { + terms: { + field: `${this.esResearchName}.default.keyword`, + exclude: '', + size: 5000, + order: { _key: this.order }, + }, + aggs: { + translation: { + terms: { + size: 50, + exclude: '', + field: `${this.esResearchName}.${this.langService.gnLang}.keyword`, + }, + }, + }, + }, + }, + }, + }) + ) + ) + .pipe( + map( + (response) => + response.aggregations[this.esFieldName.split('.')[0]].default + .buckets + ), + shareReplay() + ) + } + + getAvailableValues(): Observable { + // sort values by alphabetical order + return this.getTranslatedAggregations().pipe( + map((response) => { + return response.map((tmp) => { + const label = tmp.translation.buckets[0]?.key || tmp.key + return { + label: `${label} (${tmp.doc_count})`, + value: tmp.key, + } + }) + }) + ) + } +}