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

sqlite alternative backend support #130

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:16.16.0-alpine3.15

RUN touch /yarn-error.log && chown -R node:node yarn-error.log

RUN apk add git
RUN yarn install
18 changes: 18 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/docker-existing-dockerfile
{
"name": "docusauris-search-local",
"dockerFile": "Dockerfile",
"containerEnv": {
"PROJECT_DIR": "${containerWorkspaceFolder}"
},

"userEnvProbe": "loginShell",
//"updateRemoteUserUID": false,

// build development environment on creation
"onCreateCommand": "echo 'yarn install executed' || true",

// Add the IDs of extensions you want installed when the container is created.
"extensions": []
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ dist/
tmp/
temp/

# End of https://www.gitignore.io/api/node
# End of https://www.gitignore.io/api/node

# Mac OS X
.DS_Store
4 changes: 3 additions & 1 deletion packages/docusaurus-search-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"cheerio": "^1.0.0-rc.9",
"clsx": "^1.1.1",
"lunr-languages": "^1.4.0",
"mark.js": "^8.11.1"
"mark.js": "^8.11.1",
"sql.js": "^1.7.0",
"sql.js-httpvfs": "^0.8.11"
},
"peerDependencies": {
"@docusaurus/core": "^v2.0.0-beta.21",
Expand Down
196 changes: 142 additions & 54 deletions packages/docusaurus-search-local/src/client/theme/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,50 @@ const SearchBar = () => {
parentCategoriesBoost,
indexDocSidebarParentCategories,
maxSearchResults,
searchEngine,
} = usePluginData("@cmfcmf/docusaurus-search-local") as DSLAPluginData;

const workerRef: any = useRef(null);

useEffect(() => {
(async () => {
if (!SEARCH_INDEX_AVAILABLE || searchEngine !== "sqlite") {
return;
}

// sadly there's no good way to package workers and wasm directly so you need
// a way to get these two URLs from your bundler. This is the webpack5 way to
// create a asset bundle of the worker and wasm:
const workerUrl = new URL(
"sql.js-httpvfs/dist/sqlite.worker.js",
import.meta.url
);
const wasmUrl = new URL(
"sql.js-httpvfs/dist/sql-wasm.wasm",
import.meta.url
);
const dbPath = `${baseUrl}search-index.sqlite`;

const { createDbWorker } = require("sql.js-httpvfs");
workerRef.current = await createDbWorker(
[
{
from: "inline",
config: {
serverMode: "full", // file is just a plain old full sqlite database
url: dbPath, // url to the database (relative or full)
requestChunkSize: 4096,
},
},
],
workerUrl.toString(),
wasmUrl.toString()
);

return () => workerRef.current.terminate();
})();
}, []);

const history = useHistory<DSLALocationState>();

const { tags } = useContextualSearchFilters();
Expand Down Expand Up @@ -304,68 +346,114 @@ const SearchBar = () => {
return getItemUrl(item);
},
async getItems() {
const tags = tagsRef.current;
const indexes = await Promise.all(
tags.map((tag) => getIndex(tag))
);

const terms = tokenize(input);

return indexes
.flatMap(({ index, documents }) =>
index
.query((query) => {
query.term(terms, {
fields: ["title"],
boost: titleBoost,
});
query.term(terms, {
fields: ["title"],
boost: titleBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
query.term(terms, {
fields: ["content"],
boost: contentBoost,
});
query.term(terms, {
fields: ["content"],
boost: contentBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
query.term(terms, {
fields: ["tags"],
boost: tagsBoost,
});
query.term(terms, {
fields: ["tags"],
boost: tagsBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
if (searchEngine === "sqlite") {
// workerRef.current is a SQL.js instance except that all
// functions return Promises.
const results = await workerRef.current.db.exec(
`
SELECT
documents.documentId,
documents.pageTitle,
documents.sectionTitle,
documents.sectionRoute,
documents.type
FROM
sections
LEFT JOIN documents ON documents.documentId = sections.documentId
WHERE
sections MATCH '${input}'
LIMIT ${maxSearchResults} OFFSET 0
`
);
return (
(results.length > 0 &&
results[0].values.map(
([
documentId,
pageTitle,
sectionTitle,
sectionRoute,
type,
]: any) => {
return {
document: {
id: documentId,
pageTitle,
sectionTitle,
sectionRoute,
type,
},
score: 1,
terms,
};
}
)) ||
[]
);
} else {
const tags = tagsRef.current;
const indexes = await Promise.all(
tags.map((tag) => getIndex(tag))
);

if (indexDocSidebarParentCategories) {
return indexes
.flatMap(({ index, documents }) =>
index
.query((query) => {
query.term(terms, {
fields: ["sidebarParentCategories"],
boost: parentCategoriesBoost,
fields: ["title"],
boost: titleBoost,
});
query.term(terms, {
fields: ["sidebarParentCategories"],
boost: parentCategoriesBoost,
fields: ["title"],
boost: titleBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
}
})
.slice(0, maxSearchResults)
.map((result) => ({
document: documents.find(
(document) => document.id.toString() === result.ref
)!,
score: result.score,
terms,
}))
)
.sort((a, b) => b.score - a.score)
.slice(0, maxSearchResults);
query.term(terms, {
fields: ["content"],
boost: contentBoost,
});
query.term(terms, {
fields: ["content"],
boost: contentBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
query.term(terms, {
fields: ["tags"],
boost: tagsBoost,
});
query.term(terms, {
fields: ["tags"],
boost: tagsBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});

if (indexDocSidebarParentCategories) {
query.term(terms, {
fields: ["sidebarParentCategories"],
boost: parentCategoriesBoost,
});
query.term(terms, {
fields: ["sidebarParentCategories"],
boost: parentCategoriesBoost,
wildcard: mylunr.Query.wildcard.TRAILING,
});
}
})
.slice(0, maxSearchResults)
.map((result) => ({
document: documents.find(
(document) => document.id.toString() === result.ref
)!,
score: result.score,
terms,
}))
)
.sort((a, b) => b.score - a.score)
.slice(0, maxSearchResults);
}
},
},
];
Expand Down
Loading