diff --git a/plugins/plugin-flexsearch/package.json b/plugins/plugin-flexsearch/package.json index 2d353c4c4b..e243c697d8 100644 --- a/plugins/plugin-flexsearch/package.json +++ b/plugins/plugin-flexsearch/package.json @@ -38,7 +38,7 @@ }, "dependencies": { "chokidar": "^3.6.0", - "flexsearch": "^0.6", + "flexsearch": "^0.7.43", "he": "^1.2.0", "vue": "^3.4.21" }, diff --git a/plugins/plugin-flexsearch/src/client/composables/findInIndex.ts b/plugins/plugin-flexsearch/src/client/composables/findInIndex.ts index 2fde497f65..fe030e02d3 100644 --- a/plugins/plugin-flexsearch/src/client/composables/findInIndex.ts +++ b/plugins/plugin-flexsearch/src/client/composables/findInIndex.ts @@ -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, + } + }) }) } diff --git a/plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts b/plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts index 10be1260b7..419059f570 100644 --- a/plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts +++ b/plugins/plugin-flexsearch/src/node/prepareSearchIndex.ts @@ -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[] = [] @@ -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 = `\ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 08a8051cf9..ee4064550a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -419,8 +419,8 @@ importers: specifier: ^3.6.0 version: 3.6.0 flexsearch: - specifier: ^0.6 - version: 0.6.32 + specifier: ^0.7.43 + version: 0.7.43 he: specifier: ^1.2.0 version: 1.2.0 @@ -6961,8 +6961,8 @@ packages: resolution: {integrity: sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==} dev: true - /flexsearch@0.6.32: - resolution: {integrity: sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==} + /flexsearch@0.7.43: + resolution: {integrity: sha512-c5o/+Um8aqCSOXGcZoqZOm+NqtVwNsvVpWv6lfmSclU954O3wvQKxxK8zj74fPaSJbXpSLTs4PRhh+wnoCXnKg==} dev: false /follow-redirects@1.15.6(debug@2.6.9):