Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): upgrade flexsearch 0.7 and adopt the new API design #1294

Merged
merged 7 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
registry = 'https://registry.npmjs.org/'
strict-peer-dependencies=false
link-workspace-packages=true
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"body-scroll-lock": "4.0.0-beta.0",
"copy-to-clipboard": "^3.3.3",
"enhanced-resolve": "5.17.0",
"flexsearch": "0.6.32",
"github-slugger": "^2.0.0",
"hast-util-from-html": "^1.0.0",
"hast-util-heading-rank": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-api-docgen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"peerDependencies": {
"react": ">=17.0.0",
"react-router-dom": "^6.8.1",
"@rspress/core": "^1.0.2",
"@rspress/core": "workspace:*",
zoolsher marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^5.5.3"
},
"peerDependenciesMeta": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-client-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"vitest": "0.34.1"
},
"peerDependencies": {
"@rspress/runtime": "^1.0.2"
"@rspress/runtime": "workspace:*"
},
"sideEffects": [
"*.css",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-medium-zoom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"vitest": "0.34.1"
},
"peerDependencies": {
"@rspress/runtime": "^1.0.2"
"@rspress/runtime": "workspace:*"
},
"sideEffects": [
"*.css",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"unist-util-visit": "^4.1.1"
},
"peerDependencies": {
"@rspress/core": "^1.0.2",
"@rspress/core": "workspace:*",
"react": ">=17.0.0",
"react-router-dom": "^6.8.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"peerDependencies": {
"react": ">=17.0.0",
"react-router-dom": "^6.8.1",
"@rspress/core": "^1.0.2"
"@rspress/core": "workspace:*"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-rss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"peerDependencies": {
"react": ">=17.0.0",
"rspress": "^1.17.0"
"rspress": "workspace:*"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-typedoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"vitest": "0.34.1"
},
"peerDependencies": {
"rspress": "^1.0.2"
"rspress": "workspace:*"
},
"sideEffects": [
"*.css",
Expand Down
2 changes: 1 addition & 1 deletion packages/theme-default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@rspress/shared": "workspace:*",
"body-scroll-lock": "4.0.0-beta.0",
"copy-to-clipboard": "^3.3.3",
"flexsearch": "0.6.32",
"flexsearch": "0.7.43",
"github-slugger": "^2.0.0",
"hast-util-from-html": "^1.0.0",
"html-to-text": "^9.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
* Powered by FlexSearch. https://github.com/nextapps-de/flexsearch
*/

import type { CreateOptions, Index as SearchIndex } from 'flexsearch';
import FlexSearch from 'flexsearch';
import type {
Document,
EnrichedDocumentSearchResultSetUnit,
IndexOptionsForDocumentSearch,
} from 'flexsearch';
import FlexSearchDocument from 'flexsearch/dist/module/document';
import searchIndexHash from 'virtual-search-index-hash';
import { type PageIndexInfo, SEARCH_INDEX_NAME } from '@rspress/shared';
import type { SearchOptions } from '../types';
import { LOCAL_INDEX, type Provider, type SearchQuery } from '../Provider';
import { normalizeTextCase } from '../util';

type TFlexSearchDocumentWithType = Document<PageIndexInfo, true>;
zoolsher marked this conversation as resolved.
Show resolved Hide resolved

interface PageIndexForFlexSearch extends PageIndexInfo {
normalizedContent: string;
headers: string;
Expand All @@ -34,11 +40,11 @@ function tokenize(str: string, regex) {
}

export class LocalProvider implements Provider {
#index?: SearchIndex<PageIndexInfo[]>;
#index?: TFlexSearchDocumentWithType;

#cjkIndex?: SearchIndex<PageIndexInfo[]>;
#cjkIndex?: TFlexSearchDocumentWithType;

#cyrilicIndex?: SearchIndex<PageIndexInfo[]>;
#cyrilicIndex?: TFlexSearchDocumentWithType;

async #getPages(lang: string, version: string): Promise<PageIndexInfo[]> {
const searchIndexGroupID = `${version}###${lang}`;
Expand All @@ -63,61 +69,83 @@ export class LocalProvider implements Provider {
headers: page.toc.map(header => normalizeTextCase(header.text)).join(' '),
normalizedTitle: normalizeTextCase(page.title),
}));
const createOptions: CreateOptions = {
tokenize: 'full',
async: true,
doc: {
id: 'routePath',
field: ['normalizedTitle', 'headers', 'normalizedContent'],
},
cache: 100,
split: /\W+/,
};
const createOptions: IndexOptionsForDocumentSearch<PageIndexInfo[], true> =
{
tokenize: 'full',
document: {
id: 'id',
store: true,
index: ['normalizedTitle', 'headers', 'normalizedContent'],
},
cache: 100,
// charset: {
// split: /\W+/,
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
// },
};
// Init Search Indexes
// English Index
this.#index = FlexSearch.create(createOptions);
this.#index = new FlexSearchDocument(createOptions);
// CJK: Chinese, Japanese, Korean
this.#cjkIndex = FlexSearch.create({
this.#cjkIndex = new FlexSearchDocument({
...createOptions,
tokenize: (str: string) => tokenize(str, cjkRegex),
});
// Cyrilic Index
this.#cyrilicIndex = FlexSearch.create({
this.#cyrilicIndex = new FlexSearchDocument({
...createOptions,
tokenize: (str: string) => tokenize(str, cyrillicRegex),
});
this.#index.add(pagesForSearch);
this.#cjkIndex.add(pagesForSearch);
this.#cyrilicIndex.add(pagesForSearch);
for (const item of pagesForSearch) {
this.#index.add(item);
this.#cjkIndex.add(item);
this.#cyrilicIndex.add(item);
}
}

async search(query: SearchQuery) {
const { keyword, limit } = query;
const searchParams = {
query: keyword,

const options = {
enrich: true as const,
limit,
field: ['normalizedTitle', 'headers', 'normalizedContent'],
index: ['normalizedTitle', 'headers', 'normalizedContent'],
};

const searchResult = await Promise.all([
this.#index?.search(searchParams),
this.#cjkIndex?.search(searchParams),
this.#cyrilicIndex.search(searchParams),
this.#index?.search<true>(keyword, limit, options),
this.#cjkIndex?.search<true>(keyword, limit, options),
this.#cyrilicIndex.search<true>(keyword, limit, options),
]);

const flattenSearchResult = searchResult.flat(2).filter(Boolean);
const commbindSeachResult: PageIndexInfo[] = [];
const pushedId: Set<string> = new Set();

function insertCommbindSearchResult(
resultFromOneSearchIndex: EnrichedDocumentSearchResultSetUnit<PageIndexInfo>[],
) {
for (const item of resultFromOneSearchIndex) {
// item.field; // ignored
item.result.forEach(resultItem => {
// type of resultItem doesn't match with runtime
const id = resultItem.id as unknown as string;
if (pushedId.has(id)) {
return;
}
// mark the doc is in the searched results
pushedId.add(id);
commbindSeachResult.push(resultItem.doc);
});
}
}

// There may be duplicate search results when there are multiple languages ​​in the search keyword
const uniqueSearchResult = Array.from(
new Set(flattenSearchResult.map(item => item.id)),
).map(id => {
return flattenSearchResult.find(item => item.id === id);
searchResult.forEach(searchResultItem => {
insertCommbindSearchResult(searchResultItem);
});

return [
{
index: LOCAL_INDEX,
hits: uniqueSearchResult,
hits: commbindSeachResult,
},
];
}
Expand Down
25 changes: 11 additions & 14 deletions pnpm-lock.yaml

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