Skip to content

Commit 9a1bca6

Browse files
authored
Merge pull request #624 from code16/fix-autocomplete-local-search
Fix autocomplete local search
2 parents ba3f227 + 211e845 commit 9a1bca6

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

package-lock.json

Lines changed: 34 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"cropperjs": "^1.5.12",
7171
"dompurify": "^3.2.6",
7272
"filesize": "^10.1.0",
73-
"flexsearch": "^0.7.43",
73+
"flexsearch": "^0.8.205",
7474
"leaflet": "^1.9.4",
7575
"lodash": "^4.17.21",
7676
"lucide-vue-next": "^0.511.0",

resources/js/composables/useFullTextSearch.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
import Flexsearch from "flexsearch";
1+
import { Document, DocumentData, FieldName, Charset } from "flexsearch";
22
import { MaybeRefOrGetter, toValue, watch } from "vue";
33

4-
export function useFullTextSearch<T>(list: MaybeRefOrGetter<T[] | null>, { id, searchKeys }: { id: string, searchKeys: string[] }) {
5-
let index: Flexsearch.Document<T, true>;
4+
export function useFullTextSearch<T extends DocumentData>(
5+
list: MaybeRefOrGetter<T[] | null>,
6+
{ id, searchKeys }: { id: string, searchKeys: FieldName<T>[] }
7+
) {
8+
let index: Document<T>;
69

710
function fullTextSearch(query: string) {
8-
return index.search(query, undefined, { enrich: true })
9-
.map(result => result.result.map(r => r.doc))
10-
.flat()
11+
return index.search(query, { enrich: true, merge: true })
12+
.map(result => result.doc)
1113
}
1214

1315
watch(() => toValue(list), (list) => {
1416
if(list) {
15-
index = new Flexsearch.Document<T, true>({
17+
index = new Document<T>({
1618
document: {
1719
id,
1820
index: searchKeys,
1921
store: true,
2022
},
2123
tokenize: 'forward',
22-
charset: 'latin:simple',
24+
encoder: Charset.Normalize,
2325
});
2426
list.forEach(item => index.add(item));
2527
}

0 commit comments

Comments
 (0)