Skip to content

Commit

Permalink
feat(search): add * fallback on searched fields if lang=current
Browse files Browse the repository at this point in the history
  • Loading branch information
fgravin committed Oct 19, 2023
1 parent 6f73c9d commit dcd2da7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('ElasticsearchService', () => {
})

describe('#injectLangInQueryStringFields - Search language', () => {
const queryStringFields = ['resourceTitleObject.${searchLang}']
let queryStringFields = ['resourceTitleObject.${searchLang}']
describe('When no lang from config', () => {
beforeEach(() => {
service['metadataLang'] = undefined
Expand Down Expand Up @@ -399,6 +399,31 @@ describe('ElasticsearchService', () => {
)[1]
).toEqual('langeng')
})
it('add * fallback with low priority', () => {
queryStringFields = [
'resourceTitleObject.${searchLang}^5',
'tag.${searchLang}^4',
'resourceAbstractObject.${searchLang}^3',
'lineageObject.${searchLang}^2',
'any.${searchLang}',
'uuid',
]
expect(
service['injectLangInQueryStringFields'](queryStringFields)
).toEqual([
'resourceTitleObject.langeng^5',
'tag.langeng^4',
'resourceAbstractObject.langeng^3',
'lineageObject.langeng^2',
'any.langeng',
'uuid',
'resourceTitleObject.*',
'tag.*',
'resourceAbstractObject.*',
'lineageObject.*',
'any.*',
])
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,30 @@ export class ElasticsearchService {
}

private injectLangInQueryStringFields(queryStringFields: string[]) {
const queryLang = this.metadataLang
? this.metadataLang === 'current'
const queryLang = this.getQueryLang()
return [
...queryStringFields.map((field) => {
return field.replace(/\$\{searchLang\}/g, queryLang)
}),
...(this.isCurrentSearchLang()
? queryStringFields
.filter((field) => /\$\{searchLang\}/.test(field))
.map((field) => {
return field.replace(/\.\$\{searchLang\}(\^\d+)?/, '.*')
})
: []),
]
}

private getQueryLang(): string {
return this.metadataLang
? this.isCurrentSearchLang()
? `lang${this.lang3}`
: `lang${this.metadataLang}`
: `*`
return queryStringFields.map((field) => {
return field.replace(/\$\{searchLang\}/g, queryLang)
})
}
private isCurrentSearchLang() {
return this.metadataLang === 'current'
}

private buildPayloadQuery(
Expand Down

0 comments on commit dcd2da7

Please sign in to comment.