Skip to content

Commit

Permalink
feat: retrieve values for orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Dec 5, 2023
1 parent c7d0f9a commit b91f0fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
3 changes: 1 addition & 2 deletions libs/data-access/gn4/src/custom-api/thesaurus.api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -95,7 +95,6 @@ export class ThesaurusApiService {
)
}

console.log('here')
let headers = this.defaultHeaders

let httpHeaderAcceptSelected: string | undefined =
Expand Down
5 changes: 3 additions & 2 deletions libs/feature/search/src/lib/utils/service/fields.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OwnerSearchField,
SimpleSearchField,
ThesaurusTranslationSearchField,
TranslatedSearchField,
} from './fields'
import { forkJoin, Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
Expand Down Expand Up @@ -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
),
Expand Down
80 changes: 80 additions & 0 deletions libs/feature/search/src/lib/utils/service/fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { firstValueFrom, Observable, of, switchMap } from 'rxjs'
import {
SearchApiService,
ThesaurusApiService,
ToolsApiService,
} from '@geonetwork-ui/data-access/gn4'
Expand Down Expand Up @@ -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<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,
}
})
})
)
}
}

0 comments on commit b91f0fa

Please sign in to comment.