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 4dbe744ef7..2101fdb69b 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 @@ -1,6 +1,6 @@ import { ElasticsearchService } from './elasticsearch.service' import { ES_FIXTURE_AGGS_RESPONSE } from '@geonetwork-ui/common/fixtures' -import { EsSearchParams } from '../types/elasticsearch.model' +import { EsSearchParams } from '@geonetwork-ui/api/metadata-converter' describe('ElasticsearchService', () => { let service: ElasticsearchService @@ -131,8 +131,10 @@ describe('ElasticsearchService', () => { }, }, { - query_string: { - query: '(Org:"world")', + match: { + Org: { + world: true, + }, }, }, ], @@ -180,8 +182,10 @@ describe('ElasticsearchService', () => { }, }, { - query_string: { - query: '(Org:"world")', + match: { + Org: { + world: true, + }, }, }, { @@ -234,8 +238,10 @@ describe('ElasticsearchService', () => { }, }, { - query_string: { - query: '(Org:"world")', + match: { + Org: { + world: true, + }, }, }, { @@ -320,8 +326,10 @@ describe('ElasticsearchService', () => { }, }, { - query_string: { - query: '(Org:"world")', + match: { + Org: { + world: true, + }, }, }, ], @@ -621,15 +629,16 @@ describe('ElasticsearchService', () => { }) ).toStrictEqual({ myFilters: { - filters: { - filter1: { - match: { - field1: '100', - }, + filter1: { + match: { + field1: '100', }, - filter2: { - match: { - field2: { value1: true, value3: true }, + }, + filter2: { + match: { + field2: { + value1: true, + value3: true, }, }, }, 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 8a9b92af1d..f690a9356b 100644 --- a/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts +++ b/libs/api/repository/src/lib/gn4/elasticsearch/elasticsearch.service.ts @@ -5,7 +5,7 @@ import { Aggregation, AggregationParams, AggregationsParams, - FilterAggregationParams, + FieldFilters, SortByField, } from '@geonetwork-ui/common/domain/search' import { METADATA_LANGUAGE } from '../../metadata-language' @@ -81,6 +81,13 @@ export class ElasticsearchService { addMapping(node.field) } } + if ('match' in node && typeof node.match === 'object') { + for (const key in node.match) { + if (key in this.runtimeFields) { + addMapping(key) + } + } + } for (const runtimeField in this.runtimeFields) { if ( runtimeField in node && @@ -183,7 +190,6 @@ export class ElasticsearchService { uuids?: string[], geometry?: Geometry ) { - const queryFilters = this.stateFiltersToQueryString(fieldSearchFilters) const must = [this.queryFilterOnValues('isTemplate', 'n')] as Record< string, unknown @@ -210,12 +216,9 @@ export class ElasticsearchService { }, }) } - if (queryFilters) { - must.push({ - query_string: { - query: queryFilters, - }, - }) + if (fieldSearchFilters) { + const filters = this.searchFiltersToESFilters(fieldSearchFilters) + must.push(filters) } if (uuids) { must.push({ @@ -415,22 +418,36 @@ export class ElasticsearchService { ) } + private searchFiltersToESFilters( + filters: FieldFilters + ): Record { + const match = Object.keys(filters).reduce( + (prev, curr) => ({ + ...prev, + [curr]: filters[curr], + }), + {} + ) + return { match } + } + buildAggregationsPayload(aggregations: AggregationsParams): any { - const mapFilterAggregation = (filterAgg: FilterAggregationParams) => ({ - match: filterAgg, - }) const mapToESAggregation = (aggregation: AggregationParams) => { switch (aggregation.type) { - case 'filters': - return { - filters: Object.keys(aggregation.filters).reduce( - (prev, curr) => ({ - ...prev, - [curr]: mapFilterAggregation(aggregation.filters[curr]), - }), - {} - ), - } + case 'filters': { + return Object.keys(aggregation.filters).reduce( + (prev, curr) => ({ + ...prev, + [curr]: + typeof aggregation.filters[curr] === 'string' + ? aggregation.filters[curr] + : this.searchFiltersToESFilters( + aggregation.filters[curr] as FieldFilters + ), + }), + {} + ) + } case 'terms': return { terms: { diff --git a/libs/common/domain/src/lib/search/aggregation.model.ts b/libs/common/domain/src/lib/search/aggregation.model.ts index 443c9149b5..77a0b150fb 100644 --- a/libs/common/domain/src/lib/search/aggregation.model.ts +++ b/libs/common/domain/src/lib/search/aggregation.model.ts @@ -1,5 +1,5 @@ import { FieldName } from './search.model' -import { FieldFilter } from './filter.model' +import { FieldFilters } from './filter.model' export interface TermsAggregationParams { type: 'terms' @@ -13,7 +13,7 @@ export interface HistogramAggregationParams { field: FieldName interval: number } -export type FilterAggregationParams = Record | string +export type FilterAggregationParams = FieldFilters | string export interface FiltersAggregationParams { type: 'filters' filters: Record