Skip to content

Commit

Permalink
feat(gn4): fix conversion to ES filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Sep 13, 2023
1 parent 0ccb939 commit 7d34fcb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -131,8 +131,10 @@ describe('ElasticsearchService', () => {
},
},
{
query_string: {
query: '(Org:"world")',
match: {
Org: {
world: true,
},
},
},
],
Expand Down Expand Up @@ -180,8 +182,10 @@ describe('ElasticsearchService', () => {
},
},
{
query_string: {
query: '(Org:"world")',
match: {
Org: {
world: true,
},
},
},
{
Expand Down Expand Up @@ -234,8 +238,10 @@ describe('ElasticsearchService', () => {
},
},
{
query_string: {
query: '(Org:"world")',
match: {
Org: {
world: true,
},
},
},
{
Expand Down Expand Up @@ -320,8 +326,10 @@ describe('ElasticsearchService', () => {
},
},
{
query_string: {
query: '(Org:"world")',
match: {
Org: {
world: true,
},
},
},
],
Expand Down Expand Up @@ -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,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Aggregation,
AggregationParams,
AggregationsParams,
FilterAggregationParams,
FieldFilters,
SortByField,
} from '@geonetwork-ui/common/domain/search'
import { METADATA_LANGUAGE } from '../../metadata-language'
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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
Expand All @@ -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({
Expand Down Expand Up @@ -415,22 +418,36 @@ export class ElasticsearchService {
)
}

private searchFiltersToESFilters(
filters: FieldFilters
): Record<string, unknown> {
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: {
Expand Down
4 changes: 2 additions & 2 deletions libs/common/domain/src/lib/search/aggregation.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FieldName } from './search.model'
import { FieldFilter } from './filter.model'
import { FieldFilters } from './filter.model'

export interface TermsAggregationParams {
type: 'terms'
Expand All @@ -13,7 +13,7 @@ export interface HistogramAggregationParams {
field: FieldName
interval: number
}
export type FilterAggregationParams = Record<string, FieldFilter> | string
export type FilterAggregationParams = FieldFilters | string
export interface FiltersAggregationParams {
type: 'filters'
filters: Record<string, FilterAggregationParams>
Expand Down

0 comments on commit 7d34fcb

Please sign in to comment.