Skip to content

Commit

Permalink
WIP: Add new ContactField that differs between gn version
Browse files Browse the repository at this point in the history
  • Loading branch information
Angi-Kinas committed Dec 7, 2023
1 parent d56f845 commit f909efc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
8 changes: 2 additions & 6 deletions libs/feature/search/src/lib/utils/service/fields.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, Injector } from '@angular/core'
import {
AbstractSearchField,
ContactField,
FieldValue,
FullTextSearchField,
GnUiTranslationSearchField,
Expand All @@ -10,7 +11,6 @@ import {
OwnerSearchField,
SimpleSearchField,
ThesaurusTranslationSearchField,
TranslatedSearchField,
} from './fields'
import { forkJoin, Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
Expand Down Expand Up @@ -70,11 +70,7 @@ export class FieldsService {
q: new FullTextSearchField(),
license: new LicenseSearchField(this.injector),
owner: new OwnerSearchField(this.injector),
contact: new TranslatedSearchField(
'contactForResource.organisationObject.default',
'asc',
this.injector
),
contact: new ContactField('asc', this.injector), // new TranslationField('OrgForResource')
} as Record<string, AbstractSearchField>

get supportedFields() {
Expand Down
101 changes: 72 additions & 29 deletions libs/feature/search/src/lib/utils/service/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { ElasticsearchService } from '@geonetwork-ui/api/repository/gn4'
import { LangService } from '@geonetwork-ui/util/i18n'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { coerce, valid } from 'semver'

export type FieldValue = string | number
export interface FieldAvailableValue {
Expand Down Expand Up @@ -74,13 +76,17 @@ export class SimpleSearchField implements AbstractSearchField {
})
)
}
getFiltersForValues(values: FieldValue[]): Observable<FieldFilters> {
getFiltersForValues(
values: FieldValue[],
esFieldName: string = this.esFieldName
): Observable<FieldFilters> {
return of({
[this.esFieldName]: values.reduce((acc, val) => {
[esFieldName]: values.reduce((acc, val) => {
return { ...acc, [val.toString()]: true }
}, {}),
})
}

getValuesForFilter(filters: FieldFilters): Observable<FieldValue[]> {
const filter = filters[this.esFieldName]
if (!filter) return of([])
Expand Down Expand Up @@ -379,40 +385,73 @@ export class OwnerSearchField extends SimpleSearchField {
}
}

export class TranslatedSearchField extends SimpleSearchField {
// ResourceContactField
// contact of the data not metadata
// data is the resource
export class ContactField extends SimpleSearchField {
protected repository = this.injector.get(RecordsRepositoryInterface)
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 platformService = this.injector.get(PlatformServiceInterface)
public esFieldName: string
private esResearchName: string
private version: Observable<string> = this.platformService
.getApiVersion()
.pipe(map((version) => version))

constructor(
esFieldName: string,
order: 'asc' | 'desc' = 'asc',
injector: Injector
) {
super(esFieldName, order, injector)
this.esResearchName = this.esFieldName.substring(
0,
this.esFieldName.lastIndexOf('.')
constructor(public order: 'asc' | 'desc' = 'asc', public injector: Injector) {
super('OrgForResource', order, injector)
this.esFieldName = 'contactForResource.organisationObject.default.keyword'
}

getFiltersForValues(values: FieldValue[]): Observable<FieldFilters> {
return this.version.pipe(
switchMap((version) =>
version == '4.2.2'
? super.getFiltersForValues(values)
: super.getFiltersForValues(values, this.esFieldName)
)
)
}
getValuesForFilter(filters: FieldFilters): Observable<FieldValue[]> {
return this.version.pipe(
switchMap((version) =>
version == '4.2.2'
? super.getValuesForFilter(filters)
: this.tmpFunction(filters)
)
)
}

tmpFunction(filters: FieldFilters) {
const filter = filters[this.esFieldName]
console.log(filters)
if (!filter) return of([])
const values =
typeof filter === 'string'
? [filter]
: Object.keys(filter).filter((v) => filter[v])
console.log(values)
return of(values)
}

getTranslatedAggregations() {
return this.searchApiService
.search(
'bucket',
JSON.stringify(
this.esService.getSearchRequestBody({
[this.esFieldName.split('.')[0]]: {
contactForResource: {
nested: {
path: this.esFieldName.split('.')[0],
path: 'contactForResource',
},
aggs: {
default: {
terms: {
field: `${this.esResearchName}.default.keyword`,
field: `contactForResource.organisationObject.default.keyword`,
exclude: '',
size: 5000,
order: { _key: this.order },
Expand All @@ -422,7 +461,7 @@ export class TranslatedSearchField extends SimpleSearchField {
terms: {
size: 50,
exclude: '',
field: `${this.esResearchName}.${this.langService.gnLang}.keyword`,
field: `contactForResource.organisationObject.${this.langService.gnLang}.keyword`,
},
},
},
Expand All @@ -434,26 +473,30 @@ export class TranslatedSearchField extends SimpleSearchField {
)
.pipe(
map(
(response) =>
response.aggregations[this.esFieldName.split('.')[0]].default
.buckets
(response) => response.aggregations.contactForResource.default.buckets
),
shareReplay()
)
}

getAvailableValues(): Observable<FieldAvailableValue[]> {
// 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,
}
})
})
return this.version.pipe(
switchMap((version) =>
version == '4.2.2'
? super.getAvailableValues() //TODO copy from simplesearch field
: this.getTranslatedAggregations().pipe(
map((response) =>
response.map((tmp) => {
const label = tmp.translation.buckets[0]?.key || tmp.key
return {
label: `${label} (${tmp.doc_count})`,
value: tmp.key,
}
})
)
)
)
)
}
}

0 comments on commit f909efc

Please sign in to comment.