-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.ts
136 lines (113 loc) · 3.69 KB
/
interfaces.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
/**
* The analyzer is a function for applying preprocessing steps, such as
* stemming or lowercasing, to a single token.
*/
export type Analyzer = (token: string) => string
/**
* The tokenizer is a function that splits a text into its tokens.
*/
export type Tokenizer = (text: string) => string[]
/**
* The 'TrieNode' data structure is used to index text in a prefix-tree manner,
* for more efficient storage usage and fuzzy-search algorithms with better a
* better runtime than when using a simple map.
*/
export interface TrieNode<T> {
/** Internal reference for the children of the current node. */
_c: { [char: string]: TrieNode<T> };
/** Internal reference for the data items of the current node. */
_d: T[];
}
/**
* Data structur for a range of integers with potentially missing numbers.
*/
export type CompactRange = {
min: number,
max: number,
missing: number[]
}
/**
* A query field describes options specific to one field
* in the mapping when querying.
*/
export interface QueryField {
/** The weight of this field. Values lower than 1 will decrease importance, values above 1 introduce a boosting. */
weight?: number,
}
export interface QueryOptions {
/** A list of text fields to use for searching or an object specifying more specific options for each field using the QueryField options. */
queryFields?: string[] | { [field: string]: QueryField };
/** Filters to exclude/include specific documents based on fields that are not of type 'text'. */
filter?: {
[field: string]: any;
};
/** The number of hits to return. */
size: number;
/** The number of hits to skip before returning, e.g. for doing pagination. */
offset: number;
/** A mapping of synonyms of analyzed tokens. */
synonyms?: { [token: string]: string[] };
getDocument?: (documentId: string) => Promise<{ [key: string]: any } | null> | null;
/** Object for specifying custom BM25 parameters. */
bm25: {
/**
* The `b` value.
* @defaultValue 1.2
*/
k1: number
/**
* The `k1` value.
* @defaultValue 0.75
*/
b: number
}
}
/**
* The search index returned after building the index and used for running search queries.
* For normal use cases, the semantics of the fields of this data structure do not matter
* to the user.
*/
export type TextFieldIndex = {
/**
* A mapping of tokens to an array, in which the first value
* is the document ID this token is present and the second value
* equals to the frequency of that token in the document.
*/
docFreqsByToken: TrieNode<[number, number]>;
/** A mapping for retrieving the length, i.e. the count of words, of a document by its ID. */
docLengths: { [key: string]: number };
/** The sum of lengths of all documents. */
totalDocLengths: number;
/** The count of documents. */
docCount: number;
}
// [num, [docId2, docId2]]
export type NumberFieldIndex = [number, number[]][]
export type KeywordFieldIndex = TrieNode<number>;
export type MappingType = 'text' | 'keyword' | 'number' | 'date'
export interface Mappings {
[field: string]: MappingType
}
export interface Index {
length: number;
mappings: Mappings;
internalIds: CompactRange;
originalIds: string[];
fields: {
[key: string]: TextFieldIndex | NumberFieldIndex | KeywordFieldIndex
};
}
export interface SearchResultsHit {
_id: string;
_score: number;
_source?: { [key: string]: any } | null;
}
/** Data structure for a search result. */
export interface SearchResults {
total: number;
maxScore: number;
hits: SearchResultsHit[];
}
export interface IndexField {
indexDocument(fieldIndex: any, document: { [key: string]: unknown }, documentFieldValue: any): { [key: string]: any };
}