forked from skiff-org/trawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
151 lines (137 loc) · 4.42 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
export class Index {
readonly id: string;
readonly index: string;
readonly length: number;
constructor(options?: CreateOptions);
info(): {
id: any;
items: any;
cache: any;
matcher: number;
worker: any;
threshold: any;
depth: any;
resolution: any;
contextual: boolean;
};
add(id: any, o: string): this;
// Result without pagination -> T[]
search(query: string, options?: number | SearchOptions): any[];
searchAsync(query: string, options?: number | SearchOptions): Promise<any[]>;
update(id: any, o: string): this;
updateAsync(id: any, o: string): Promise<this>;
remove(id: any): this;
removeAsync(id: any): this;
contain(id: any): boolean;
clear(): this;
destroy(): this;
addMatcher(matcher: Matcher): this;
encode(str: string): string;
serialize(): Record<string, any>;
static deserialize(obj: Record<string, any>, params: Record<string, any>): Index;
}
export class Document<T> {
constructor(options?: CreateOptions | CreateDocumentOptions);
add(o: T): this;
add(id: any, o: T): this;
update(o: T): this;
update(id: any, o: T): this;
remove(id: any): this;
search(
query: string,
options?: SearchOptions
): any // The shape of the resulting object can vary widely,
// so we will put off typing it for now
contain(id: any): boolean;
// TODO add async methods
// TODO add more methods
serialize(): Record<string, any>;
static deserialize(obj: Record<string, any>, params: Record<string, any>): Document<any>;
}
interface SearchOptions {
limit?: number;
suggest?: boolean;
where?: { [key: string]: string };
field?: string | string[];
bool?: "and" | "or" | "not";
query?: string;
enrich?: boolean;
context?: boolean;
//TODO: Sorting
}
interface SearchResults<T> {
page?: Cursor;
next?: Cursor;
result: T[];
}
/**
* These are the options necessary for initializing a Document
* A document needs to know two things: what is the 'primary key' to index by (id)
* and what fields should be indexed (index). The `index` parameter can also
* contain much more complicated information, as described in the FlexSearch
* README. Therefore, we give it the any time to allow multiple different ways
* of creating it
*/
interface CreateDocumentOptions {
id: any;
index: any;
store: string[];
}
export type CreateOptions = {
profile?: IndexProfile;
tokenize?: DefaultTokenizer | TokenizerFn;
split?: RegExp;
encode?: DefaultEncoder | EncoderFn | false;
cache?: boolean | number;
async?: boolean;
worker?: false | number;
depth?: false | number;
threshold?: false | number;
resolution?: number;
stemmer?: Stemmer | string | false;
filter?: FilterFn | string | false;
rtl?: boolean;
document?: CreateDocumentOptions;
context?: {
bidirectional?: boolean;
resolution?: number;
depth?: number
}
minlength?: number;
};
// limit number Sets the limit of results.
// suggest true, false Enables suggestions in results.
// where object Use a where-clause for non-indexed fields.
// field string, Array<string> Sets the document fields which should be searched. When no field is set, all fields will be searched. Custom options per field are also supported.
// bool "and", "or" Sets the used logical operator when searching through multiple fields.
// page true, false, cursor Enables paginated results.
type IndexProfile =
| "memory"
| "speed"
| "match"
| "score"
| "balance"
| "fast";
type DefaultTokenizer = "strict" | "forward" | "reverse" | "full";
type TokenizerFn = (str: string) => string[];
type DefaultEncoder = "icase" | "simple" | "advanced" | "extra" | "balance";
type EncoderFn = (str: string) => string;
type Stemmer = { [key: string]: string };
type Matcher = { [key: string]: string };
type FilterFn = (str: string) => boolean;
type Cursor = string;
// FlexSearch.create(<options>)
// FlexSearch.registerMatcher({KEY: VALUE})
// FlexSearch.registerEncoder(name, encoder)
// FlexSearch.registerLanguage(lang, {stemmer:{}, filter:[]})
// FlexSearch.encode(name, string)
// declare class FlexSearch {
// static create<T>(options?: CreateOptions): FlexSearch.Index<T>;
// static registerMatcher(matcher: Matcher): typeof FlexSearch;
// static registerEncoder(name: string, encoder: EncoderFn): typeof FlexSearch;
// static registerLanguage(
// lang: string,
// options: { stemmer?: Stemmer; filter?: string[] }
// ): typeof FlexSearch;
// static encode(name: string, str: string): string;
// }