Skip to content

Commit

Permalink
🔎update flexsearch (finally)
Browse files Browse the repository at this point in the history
  • Loading branch information
sroertgen committed Aug 16, 2023
1 parent 6e72156 commit 56792b9
Show file tree
Hide file tree
Showing 4 changed files with 2,171 additions and 1,571 deletions.
43 changes: 28 additions & 15 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { DataFactory } = n3
const { namedNode } = DataFactory
const path = require("path")
const fs = require("fs-extra")
const flexsearch = require("flexsearch")
const { Index } = require("flexsearch")
const omitEmpty = require("omit-empty")
const { i18n, getFilePath, parseLanguages } = require("./src/common")
const context = require("./src/context")
Expand Down Expand Up @@ -71,6 +71,30 @@ const getTurtleFiles = function (dirPath, arrayOfFiles) {
return arrayOfFiles
}

/**
* Exports an index and saves it
* @param {object} index
* @param {object} conceptScheme
* @param {string} language
*
**/
const exportIndex = (index, conceptScheme, language) => {
index.export(function (key, data) {
const path = getFilePath(
conceptScheme.id + `/search/${language}/${key}`,
`json`
)
createData({
path,
data: data !== undefined ? data : "",
})
// fs.writeFileSync(
// `${searchIndexPath}${key}.json`,
// data !== undefined ? (data) : ""
// );
})
}

exports.onPreBootstrap = async ({ createContentDigest, actions, getNode }) => {
const { createNode, createNodeField } = actions
const ttlFiles = getTurtleFiles("./data", [])
Expand Down Expand Up @@ -248,13 +272,9 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
const languagesOfCS = languagesByCS[conceptScheme.id]
const indexes = Object.fromEntries(
[...languagesOfCS].map((l) => {
const index = flexsearch.create({
const index = new Index({
tokenize: tokenizer,
})
index.addMatcher({
"[Ää]": "a", // replaces all 'ä' to 'a'
"[Öö]": "o",
"[Üü]": "u",
charset: "latin",
})
return [l, index]
})
Expand Down Expand Up @@ -307,10 +327,6 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
)
})

languagesOfCS.forEach((l) => {
console.log(`Built index for language "${l}"`, indexes[l].info())
})

languagesOfCS.forEach((language) =>
createPage({
path: getFilePath(conceptScheme.id, `${language}.html`),
Expand All @@ -336,10 +352,7 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
})
// create index files
languagesOfCS.forEach((language) =>
createData({
path: getFilePath(conceptScheme.id, `${language}.index`),
data: JSON.stringify(indexes[language].export(), null, 2),
})
exportIndex(indexes[language], conceptScheme, language)
)
}
)
Expand Down
Loading

0 comments on commit 56792b9

Please sign in to comment.