Skip to content

Commit

Permalink
fix(wc): remove ConfigFilters specificity
Browse files Browse the repository at this point in the history
filters are SearchFilters like others and are merged with state filters before building the ES payload
  • Loading branch information
fgravin committed Aug 25, 2023
1 parent d24311e commit 4ebbe7c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,24 @@
You can add filters for the search in your
<b><i>gn-results-list</i></b> web component
</p>
<p>eg. Filter on <i>service</i> records:</p>
<p>eg. Filter on <i>dataset</i> records:</p>
<ul>
<li>
<p>With Elasticsearch syntax</p>
<code>
{"elastic": [{ "term": { "cl_hierarchyLevel.key": "service" } }]}
</code>
</li>
<li>
<p>With GN configuration</p>
<code>
{"custom": { "cl_hierarchyLevel.key": { "service": true } }}
</code>
<p>With GNUI syntax</p>
<code> {"cl_hierarchyLevel.key": { "service": true } } </code>
</li>
</ul>
</div>

<h1>Filter on services</h1>
<h2>elasticsearch object</h2>
<gn-results-list
api-url="https://apps.titellus.net/geonetwork/srv/api"
size="5"
layout="TITLE"
search-id="elastic"
filter='{"elastic": [{ "term": { "cl_hierarchyLevel.key": "service" } }]}'
></gn-results-list>
<h2>custom object</h2>
<h1>Filter on datasets</h1>
<gn-results-list
api-url="https://apps.titellus.net/geonetwork/srv/api"
size="5"
layout="TITLE"
search-id="custom"
primary-color="#0f4395"
main-font="'Inter', sans-serif"
filter='{"custom": { "cl_hierarchyLevel.key": { "service": true } }}'
filter='{"cl_hierarchyLevel.key": { "dataset": true } }'
></gn-results-list>
</main>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
main-color="#555"
background-color="#fdfbff"
main-font="'Inter', sans-serif"
filter='{"elastic": [{ "term": { "OrgForResource": "Géo2France" } }]}'
filter='{"OrgForResource": { "Géo2France": true } }'
title-font="'DM Serif Display', serif"
show-more="auto"
></gn-results-list>
Expand Down
5 changes: 0 additions & 5 deletions libs/api/metadata-converter/src/lib/gn4/types/search.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ export interface SearchFilters {

type SearchFiltersFieldsLeaf = Record<string, boolean>

export interface StateConfigFilters {
custom?: SearchFilters
elastic?: any
}

export interface Organisation {
name: string
description?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,58 +356,6 @@ describe('ElasticsearchService', () => {
})
})

describe('#buildPayloadFilter', () => {
describe('when elastic object config', () => {
it('returns the input config', () => {
const filter = service['buildPayloadFilter']({
elastic: { term: { 'cl_hierarchyLevel.key': 'service' } },
})
expect(filter).toEqual([
{ term: { 'cl_hierarchyLevel.key': 'service' } },
])
})
})
describe('when elastic array config', () => {
it('returns the input config', () => {
const filter = service['buildPayloadFilter']({
elastic: [{ term: { 'cl_hierarchyLevel.key': 'service' } }],
})
expect(filter).toEqual([
{ term: { 'cl_hierarchyLevel.key': 'service' } },
])
})
})
describe('when custom config', () => {
it('returns the corresponding query_string', () => {
const filter = service['buildPayloadFilter']({
custom: {
'cl_hierarchyLevel.key': {
service: true,
},
},
})
expect(filter).toEqual([
{ query_string: { query: '(cl_hierarchyLevel.key:"service")' } },
])
})
})
describe('when having both config', () => {
it('elastic config priors', () => {
const filter = service['buildPayloadFilter']({
elastic: [{ term: { 'cl_hierarchyLevel.key': 'service' } }],
custom: {
'cl_hierarchyLevel.key': {
service: true,
},
},
})
expect(filter).toEqual([
{ term: { 'cl_hierarchyLevel.key': 'service' } },
])
})
})
})

describe('#buildAutocompletePayload', () => {
describe('given an autocomplete config', () => {
it('returns the search payload', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
RequestFields,
SearchFilters,
SortParams,
StateConfigFilters,
TermsAggregationResult,
} from '@geonetwork-ui/api/metadata-converter'

Expand All @@ -40,7 +39,7 @@ export class ElasticsearchService {
sortBy: SortByField = null,
requestFields: RequestFields = [],
searchFilters: SearchFilters = {},
configFilters: StateConfigFilters = {},
configFilters: SearchFilters = {},
uuids?: string[],
geometry?: Geometry
): EsSearchParams {
Expand Down Expand Up @@ -180,7 +179,7 @@ export class ElasticsearchService {

private buildPayloadQuery(
{ any, ...fieldSearchFilters }: SearchFilters,
configFilters: StateConfigFilters,
configFilters: SearchFilters,
uuids?: string[],
geometry?: Geometry
) {
Expand All @@ -198,7 +197,6 @@ export class ElasticsearchService {
]),
}
const should = [] as Record<string, unknown>[]
const filter = this.buildPayloadFilter(configFilters)

if (any) {
must.push({
Expand Down Expand Up @@ -254,34 +252,16 @@ export class ElasticsearchService {
must,
must_not,
should,
filter,
filter: [],
},
}
}

private buildPayloadFilter({ custom, elastic }: StateConfigFilters) {
const query = []
if (elastic) {
if (!Array.isArray(elastic)) {
query.push(elastic)
} else {
query.push(...elastic)
}
} else if (custom) {
query.push({
query_string: {
query: this.stateFiltersToQueryString(custom),
},
})
}
return query
}

buildMoreOnAggregationPayload(
aggregations: any,
key: string,
searchFilters: SearchFilters,
configFilters: StateConfigFilters
configFilters: SearchFilters
): EsSearchParams {
return {
aggregations: { [key]: aggregations[key] },
Expand Down

0 comments on commit 4ebbe7c

Please sign in to comment.