Skip to content

Commit

Permalink
feat(search): handle 'current' lang option for ES request
Browse files Browse the repository at this point in the history
if current is set and UI lang is 'eng', then the requested fields are .langeng ones
  • Loading branch information
fgravin committed Oct 18, 2023
1 parent 0492232 commit 6f73c9d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ElasticsearchService } from './elasticsearch.service'
import { ES_FIXTURE_AGGS_RESPONSE } from '@geonetwork-ui/common/fixtures'
import { EsSearchParams } from '../types/elasticsearch.model'
import { LangService } from '@geonetwork-ui/util/i18n'
import { EsSearchParams } from '@geonetwork-ui/api/metadata-converter'

const langServiceMock = {
iso3: 'eng',
} as LangService

describe('ElasticsearchService', () => {
let service: ElasticsearchService
let searchFilters

beforeEach(() => {
service = new ElasticsearchService('fre')
service = new ElasticsearchService(langServiceMock, 'fre')
})

it('should be created', () => {
Expand Down Expand Up @@ -356,6 +361,47 @@ describe('ElasticsearchService', () => {
})
})

describe('#injectLangInQueryStringFields - Search language', () => {
const queryStringFields = ['resourceTitleObject.${searchLang}']
describe('When no lang from config', () => {
beforeEach(() => {
service['metadataLang'] = undefined
})
it('use * wildcard', () => {
expect(
service['injectLangInQueryStringFields'](queryStringFields)[0].split(
'.'
)[1]
).toEqual('*')
})
})
describe('When one lang in config', () => {
beforeEach(() => {
service['metadataLang'] = 'fre'
})
it('search in the config language', () => {
expect(
service['injectLangInQueryStringFields'](queryStringFields)[0].split(
'.'
)[1]
).toEqual('langfre')
})
})
describe('When "current" language from config"', () => {
beforeEach(() => {
service['metadataLang'] = 'current'
service['lang3'] = 'eng'
})
it('search in the UI language', () => {
expect(
service['injectLangInQueryStringFields'](queryStringFields)[0].split(
'.'
)[1]
).toEqual('langeng')
})
})
})

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 @@ -19,6 +19,7 @@ import {
SortParams,
TermsAggregationResult,
} from '@geonetwork-ui/api/metadata-converter'
import { LangService } from '@geonetwork-ui/util/i18n'

@Injectable({
providedIn: 'root',
Expand All @@ -27,8 +28,10 @@ export class ElasticsearchService {
// runtime fields are computed using a Painless script
// see: https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html
private runtimeFields: Record<string, string> = {}
private lang3 = this.langService.iso3

constructor(
private langService: LangService,
@Optional() @Inject(METADATA_LANGUAGE) private metadataLang: string
) {}

Expand Down Expand Up @@ -167,11 +170,12 @@ export class ElasticsearchService {
return fields.map((field) => ({ [field[1]]: field[0] }))
}

private injectLangInQueryStringFields(
queryStringFields: string[],
lang: string
) {
const queryLang = lang ? `lang${lang}` : `*`
private injectLangInQueryStringFields(queryStringFields: string[]) {
const queryLang = this.metadataLang
? this.metadataLang === 'current'
? `lang${this.lang3}`
: `lang${this.metadataLang}`
: `*`
return queryStringFields.map((field) => {
return field.replace(/\$\{searchLang\}/g, queryLang)
})
Expand Down Expand Up @@ -203,10 +207,7 @@ export class ElasticsearchService {
query_string: {
query: this.escapeSpecialCharacters(any),
default_operator: 'AND',
fields: this.injectLangInQueryStringFields(
ES_QUERY_STRING_FIELDS,
this.metadataLang
),
fields: this.injectLangInQueryStringFields(ES_QUERY_STRING_FIELDS),
},
})
}
Expand Down Expand Up @@ -290,15 +291,12 @@ export class ElasticsearchService {
multi_match: {
query,
type: 'bool_prefix',
fields: this.injectLangInQueryStringFields(
[
'resourceTitleObject.${searchLang}',
'resourceAbstractObject.${searchLang}',
'tag',
'resourceIdentifier',
],
this.metadataLang
),
fields: this.injectLangInQueryStringFields([
'resourceTitleObject.${searchLang}',
'resourceAbstractObject.${searchLang}',
'tag',
'resourceIdentifier',
]),
},
},
],
Expand Down

0 comments on commit 6f73c9d

Please sign in to comment.