Skip to content

Commit

Permalink
Replace flexsearch with fusejs
Browse files Browse the repository at this point in the history
  • Loading branch information
张文兵 committed Mar 7, 2024
1 parent a111041 commit 5a35df9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 54 deletions.
31 changes: 20 additions & 11 deletions assets/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
(function () {
const searchDataURL = '{{ $searchData.RelPermalink }}';
const indexConfig = Object.assign({{ $searchConfig }}, {
doc: {
id: 'id',
field: ['title', 'content'],
store: ['title', 'href', 'section']
}
includeScore: true,
useExtendedSearch: true,
fieldNormWeight: 1.5,
threshold: 0.2,
ignoreLocation: true,
keys: [
{
name: 'title',
weight: 0.7
},
{
name: 'content',
weight: 0.3
}
]
});

const input = document.querySelector('#book-search-input');
Expand Down Expand Up @@ -63,8 +73,7 @@
fetch(searchDataURL)
.then(pages => pages.json())
.then(pages => {
window.bookSearchIndex = FlexSearch.create('balance', indexConfig);
window.bookSearchIndex.add(pages);
window.bookSearchIndex = new Fuse(pages, indexConfig);
})
.then(() => input.required = false)
.then(search);
Expand All @@ -79,14 +88,14 @@
return;
}

const searchHits = window.bookSearchIndex.search(input.value, 10);
const searchHits = window.bookSearchIndex.search(input.value).slice(0,10);
searchHits.forEach(function (page) {
const li = element('<li><a href></a><small></small></li>');
const a = li.querySelector('a'), small = li.querySelector('small');

a.href = page.href;
a.textContent = page.title;
small.textContent = page.section;
a.href = page.item.href;
a.textContent = page.item.title;
small.textContent = page.item.section;

results.appendChild(li);
});
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/docs/html-head.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{{- if default true .Site.Params.BookSearch -}}
{{- $searchJSFile := printf "%s.search.js" .Language.Lang }}
{{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify | resources.Fingerprint }}
<script defer src="{{ "flexsearch.min.js" | relURL }}"></script>
<script defer src="{{ "fuse.min.js" | relURL }}"></script>
<script defer src="{{ $searchJS.RelPermalink }}" {{ template "integrity" $searchJS }}></script>
{{ end -}}

Expand Down
42 changes: 0 additions & 42 deletions static/flexsearch.min.js

This file was deleted.

0 comments on commit 5a35df9

Please sign in to comment.