feat: use flexsearch for all local searches (#997)
Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/997 Reviewed-by: dpschen <dpschen@noreply.kolaente.de> Co-authored-by: konrad <k@knt.li> Co-committed-by: konrad <k@knt.li>
This commit is contained in:
52
src/indexes/index.ts
Normal file
52
src/indexes/index.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import {Document, SimpleDocumentSearchResultSetUnit} from 'flexsearch'
|
||||
|
||||
export interface withId {
|
||||
id: number,
|
||||
}
|
||||
|
||||
const indexes: { [k: string]: Document<withId> } = {}
|
||||
|
||||
export const createNewIndexer = (name: string, fieldsToIndex: string[]) => {
|
||||
if (typeof indexes[name] === 'undefined') {
|
||||
indexes[name] = new Document<withId>({
|
||||
tokenize: 'full',
|
||||
document: {
|
||||
id: 'id',
|
||||
index: fieldsToIndex,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const index = indexes[name]
|
||||
|
||||
function add(item: withId) {
|
||||
return index.add(item.id, item)
|
||||
}
|
||||
|
||||
function remove(item: withId) {
|
||||
return index.remove(item.id)
|
||||
}
|
||||
|
||||
function update(item: withId) {
|
||||
return index.update(item.id, item)
|
||||
}
|
||||
|
||||
function search(query: string | null): number[] | null {
|
||||
if (query === '' || query === null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
return index.search(query)
|
||||
?.flatMap(r => r.result)
|
||||
.filter((value, index, self) => self.indexOf(value) === index)
|
||||
|| null
|
||||
}
|
||||
|
||||
return {
|
||||
add,
|
||||
remove,
|
||||
update,
|
||||
search,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user