|
1 | | -import Flexsearch from "flexsearch"; |
| 1 | +import { Document, DocumentData, FieldName, Charset } from "flexsearch"; |
2 | 2 | import { MaybeRefOrGetter, toValue, watch } from "vue"; |
3 | 3 |
|
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>; |
6 | 9 |
|
7 | 10 | 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) |
11 | 13 | } |
12 | 14 |
|
13 | 15 | watch(() => toValue(list), (list) => { |
14 | 16 | if(list) { |
15 | | - index = new Flexsearch.Document<T, true>({ |
| 17 | + index = new Document<T>({ |
16 | 18 | document: { |
17 | 19 | id, |
18 | 20 | index: searchKeys, |
19 | 21 | store: true, |
20 | 22 | }, |
21 | 23 | tokenize: 'forward', |
22 | | - charset: 'latin:simple', |
| 24 | + encoder: Charset.Normalize, |
23 | 25 | }); |
24 | 26 | list.forEach(item => index.add(item)); |
25 | 27 | } |
|
0 commit comments