Skip to content

Commit

Permalink
fix: upgrade flexsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
svalaskevicius committed Mar 27, 2024
1 parent ffc617b commit e2ad70f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
6 changes: 3 additions & 3 deletions plugins/plugin-flexsearch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuepress/plugin-flexsearch",
"version": "2.0.0-rc.18",
"version": "2.0.0-rc.21",
"description": "VuePress plugin - built-in search using flexsearch",
"keywords": [
"vuepress-plugin",
Expand Down Expand Up @@ -38,12 +38,12 @@
},
"dependencies": {
"chokidar": "^3.6.0",
"flexsearch": "^0.6",
"flexsearch": "^0.7.43",
"he": "^1.2.0",
"vue": "^3.4.21"
},
"peerDependencies": {
"vuepress": "2.0.0-rc.8"
"vuepress": "2.0.0-rc.9"
},
"publishConfig": {
"access": "public"
Expand Down
28 changes: 16 additions & 12 deletions plugins/plugin-flexsearch/src/client/composables/findInIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ export interface SearchIndexRet {

export type ClientSideSearchIndex = (string, number) => SearchIndexRet[]

const index = FS.create({
async: false,
doc: {
const index = new FS.Document({
document: {
id: 'id',
field: ['title', 'content'],
index: ['title', 'content'],
},
})
index.import(searchIndexRaw.idx)
for (const key in searchIndexRaw.idx) {
console.log('importing ' + key)
index.import(key, searchIndexRaw.idx[key])
}

export const findInIndex: ClientSideSearchIndex = (q: string, c: number) => {
const searchResult: any = index.search(q, c)
return searchResult.map((r) => {
return {
path: searchIndexRaw.paths[r.id],
title: r.title,
content: r.content,
}
const searchResult: any = index.search(q, { enrich: true, limit: c })
return searchResult.flatMap((r) => {
return r.result.map((fieldResult) => {
return {
path: searchIndexRaw.paths[fieldResult.id],
title: fieldResult.doc.title,
content: fieldResult.doc.content,
}
})
})
}
13 changes: 9 additions & 4 deletions plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ export const prepareSearchIndex = async ({
// generate search index
const pages = app.pages.filter(isSearchable)

const index = FS.create({
doc: {
const index = new FS.Document({
document: {
id: 'id',
field: ['title', 'content'],
index: ['title', 'content'],
store: true,
},
tokenize: 'forward',
})

const paths: string[] = []
Expand All @@ -54,7 +56,10 @@ export const prepareSearchIndex = async ({
index.add(d)
})

const data = index.export()
const data = {}
await index.export((key, datum) => {
data[key] = datum
})

// search index file content
let content = `\
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e2ad70f

Please sign in to comment.