-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add all prefLabels to flexSearch index
The problem was that through multiple calls of `index.add(concept.id, i18n(language)(concept.prefLabel))` the content for the respective id got overwritten. This way only the last added label was present in the flexSearch index. To overcome this I added two functions in a new file `searchIndex.js`. `buildIndexAndScheme` creates the index scheme and the index dependent on how many languages are present. In `addToIndex` labels are concatenated to their language. With the array labelsToAdd, we have the possibility to later add on more labels per language, e.g. `skos:example` or `skos:altLabel`. We then save it just like the `.index` file, to later load it in the `App.js`template. With the adjustemnt in line 79 it is possible to choose whether to search across all languages or just the current chosen one from pageContext. Might resolve: #127, #128
- Loading branch information
Showing
4 changed files
with
68 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { i18n } = require('./common') | ||
const flexsearch = require('flexsearch') | ||
|
||
const buildIndexAndScheme = (languages) => { | ||
const indexScheme = { | ||
doc: { | ||
id: 'id', | ||
field: [...languages], | ||
}, | ||
} | ||
const index = flexsearch.create(indexScheme) | ||
return { index, indexScheme } | ||
} | ||
|
||
const addToIndex = (languages, concept, index) => { | ||
// object that holds languages as key and we add labels to them in an array | ||
const fields = Object.fromEntries( | ||
[...languages].map((language) => [[language], '']) | ||
) | ||
// which labels should be added to the search index | ||
const labelsToAdd = ['prefLabel', 'altLabel'] | ||
|
||
labelsToAdd.forEach((label) => { | ||
[...languages].forEach((language) => { | ||
const i18nLabels = | ||
Array.isArray(concept[label]) && concept[label][language] ? i18n(language)(concept[label]) | ||
: concept[label] ? [i18n(language)(concept[label])] | ||
: [''] | ||
fields[language] = fields[language].concat(' ' + i18nLabels.join(' ')) | ||
}) | ||
}) | ||
index.add({ id: concept.id, ...fields }) | ||
return index | ||
} | ||
|
||
module.exports = { | ||
buildIndexAndScheme, | ||
addToIndex, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters