mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-26 13:26:22 +00:00
feat(search): add search by title/content index and tag at the same time (#978)
* feat(search): add search by title/content index and tag at the same time * fix(search): set search type to basic and remove tag from term for proper highlightning and scroll when searched by tag and title/content index * fix(search): use indexOf to find space so it is easier to read * fix(search): trim trailing whitespaces before splitting * fix(search): set limit to 10000 for combined search mode (to make filter by tag more accurate)
This commit is contained in:
parent
253497cad4
commit
38d9d52137
1 changed files with 28 additions and 5 deletions
|
@ -21,6 +21,7 @@ let index = new FlexSearch.Document<Item>({
|
||||||
encode: encoder,
|
encode: encoder,
|
||||||
document: {
|
document: {
|
||||||
id: "id",
|
id: "id",
|
||||||
|
tag: "tags",
|
||||||
index: [
|
index: [
|
||||||
{
|
{
|
||||||
field: "title",
|
field: "title",
|
||||||
|
@ -405,11 +406,33 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
|
||||||
|
|
||||||
let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[]
|
let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[]
|
||||||
if (searchType === "tags") {
|
if (searchType === "tags") {
|
||||||
searchResults = await index.searchAsync({
|
currentSearchTerm = currentSearchTerm.substring(1).trim()
|
||||||
query: currentSearchTerm.substring(1),
|
const separatorIndex = currentSearchTerm.indexOf(" ")
|
||||||
limit: numSearchResults,
|
if (separatorIndex != -1) {
|
||||||
index: ["tags"],
|
// search by title and content index and then filter by tag (implemented in flexsearch)
|
||||||
})
|
const tag = currentSearchTerm.substring(0, separatorIndex)
|
||||||
|
const query = currentSearchTerm.substring(separatorIndex + 1).trim()
|
||||||
|
searchResults = await index.searchAsync({
|
||||||
|
query: query,
|
||||||
|
// return at least 10000 documents, so it is enough to filter them by tag (implemented in flexsearch)
|
||||||
|
limit: Math.max(numSearchResults, 10000),
|
||||||
|
index: ["title", "content"],
|
||||||
|
tag: tag,
|
||||||
|
})
|
||||||
|
for (let searchResult of searchResults) {
|
||||||
|
searchResult.result = searchResult.result.slice(0, numSearchResults)
|
||||||
|
}
|
||||||
|
// set search type to basic and remove tag from term for proper highlightning and scroll
|
||||||
|
searchType = "basic"
|
||||||
|
currentSearchTerm = query
|
||||||
|
} else {
|
||||||
|
// default search by tags index
|
||||||
|
searchResults = await index.searchAsync({
|
||||||
|
query: currentSearchTerm,
|
||||||
|
limit: numSearchResults,
|
||||||
|
index: ["tags"],
|
||||||
|
})
|
||||||
|
}
|
||||||
} else if (searchType === "basic") {
|
} else if (searchType === "basic") {
|
||||||
searchResults = await index.searchAsync({
|
searchResults = await index.searchAsync({
|
||||||
query: currentSearchTerm,
|
query: currentSearchTerm,
|
||||||
|
|
Loading…
Reference in a new issue