-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfield.dart
295 lines (263 loc) · 7.53 KB
/
field.dart
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
part of 'models.dart';
class Field {
/// [name] of the field.
final String name;
/// [Type] of field.
final Type? type;
/// If this field is an array containing multiple values.
///
/// Applicable to the field with the [type]:
/// [Type.string], [Type.int32], [Type.int64],
/// [Type.float], [Type.bool] or [Type.geopoint]
final bool isMultivalued;
/// Used in case of a vector field. Represents the number of dimensions
/// (length of the float array) that your embeddings contain.
final int dimensions;
/// If this field can be ommited in a document.
final bool isOptional;
/// If this field will be used in faceted search.
final bool isFacetable;
/// Declaring a field as non-indexable
///
/// Setting this field to `false` ensures it is not indexed. This is useful
/// when used along with auto schema detection.
final bool shouldIndex;
final String? locale;
/// Enable sorting on a string field.
///
/// Sorting is enabled by default on numerical and boolean fields.
final bool sort;
/// Enable infix search on a string field.
///
/// Since infix searching requires an additional data structure it has to
/// be enabled on a per-field basis.
final bool enableInfixSearch;
/// Connects a document to a field in another collection.
///
/// Example value: `ReferencedCollectionName.fieldName`.
final String? reference;
Field(
this.name, {
this.type,
this.isMultivalued = false,
this.dimensions = 0,
this.isOptional = false,
this.isFacetable = false,
this.shouldIndex = true,
this.locale,
this.sort = false,
this.enableInfixSearch = false,
this.reference,
}) {
if (name.isEmpty) {
throw ArgumentError('Ensure Field.name is not empty');
}
}
factory Field.fromMap(Map<String, dynamic> map) {
final isMultivalued =
map['type']?.contains(_multivaluedExpression) ?? false;
return Field(
map['name'],
type: map['type'] != null
? _Type.fromValue(map['type'], isMultivalued)
: null,
isMultivalued: isMultivalued,
dimensions: map['num_dim'] ?? 0,
isOptional: map['optional'] ?? false,
isFacetable: map['facet'] ?? false,
shouldIndex: map['index'] ?? true,
locale: map['locale'],
sort: map['sort'] ?? false,
enableInfixSearch: map['infix'] ?? false,
reference: map['reference'],
);
}
Map<String, dynamic> toMap() {
final map = <String, dynamic>{};
map['name'] = name;
if (type != null) {
map['type'] = type!.value(isMultivalued);
}
if (dimensions > 0) {
map['num_dim'] = dimensions;
}
if (isOptional) {
map['optional'] = true;
}
if (isFacetable) {
map['facet'] = true;
}
if (!shouldIndex) {
map['index'] = false;
}
if (locale != null) {
map['locale'] = locale;
}
if (sort) {
map['sort'] = true;
}
if (enableInfixSearch) {
map['infix'] = true;
}
if (reference != null) {
map['reference'] = reference;
}
return map;
}
@override
String toString() {
return '$name(${type?.value(isMultivalued) ?? ''})';
}
@override
int get hashCode =>
name.hashCode ^
type.hashCode ^
isMultivalued.hashCode ^
dimensions.hashCode ^
isOptional.hashCode ^
isFacetable.hashCode ^
shouldIndex.hashCode ^
locale.hashCode ^
sort.hashCode ^
enableInfixSearch.hashCode ^
reference.hashCode;
@override
bool operator ==(Object other) {
return other is Field &&
other.runtimeType == runtimeType &&
other.name == name &&
other.type == type &&
other.isMultivalued == isMultivalued &&
other.dimensions == dimensions &&
other.isOptional == isOptional &&
other.isFacetable == isFacetable &&
other.shouldIndex == shouldIndex &&
other.locale == locale &&
other.sort == sort &&
other.enableInfixSearch == enableInfixSearch &&
other.reference == reference;
}
}
/// Used to update a colletion's fields.
///
/// Since Typesense currently only supports adding/deleting a field,
/// any modifications to an existing field should be expressed as a drop + add
/// operation.
class UpdateField extends Field {
/// If this field should be dropped during collection update operation.
final bool shouldDrop;
UpdateField(
super.name, {
super.type,
super.isMultivalued,
super.dimensions,
super.isOptional,
super.isFacetable,
super.shouldIndex,
super.locale,
super.sort,
super.enableInfixSearch,
this.shouldDrop = false,
});
@override
Map<String, dynamic> toMap() {
final map = super.toMap();
if (shouldDrop) {
map['drop'] = true;
}
return map;
}
factory UpdateField.fromMap(Map<String, dynamic> map) {
final field = Field.fromMap(map);
return UpdateField(
field.name,
type: field.type,
isMultivalued: field.isMultivalued,
dimensions: field.dimensions,
isOptional: field.isOptional,
isFacetable: field.isFacetable,
shouldIndex: field.shouldIndex,
locale: field.locale,
sort: field.sort,
enableInfixSearch: field.enableInfixSearch,
shouldDrop: map['drop'] ?? false,
);
}
@override
int get hashCode =>
name.hashCode ^
type.hashCode ^
isMultivalued.hashCode ^
dimensions.hashCode ^
isOptional.hashCode ^
isFacetable.hashCode ^
shouldIndex.hashCode ^
locale.hashCode ^
sort.hashCode ^
enableInfixSearch.hashCode ^
shouldDrop.hashCode;
@override
bool operator ==(Object other) {
return other is UpdateField &&
other.runtimeType == runtimeType &&
other.name == name &&
other.type == type &&
other.isMultivalued == isMultivalued &&
other.dimensions == dimensions &&
other.isOptional == isOptional &&
other.isFacetable == isFacetable &&
other.shouldIndex == shouldIndex &&
other.locale == locale &&
other.sort == sort &&
other.enableInfixSearch == enableInfixSearch &&
other.shouldDrop == shouldDrop;
}
}
/// Enumerates the allowed field types.
///
/// [Type.auto] and [Type.stringify] are special field types that are used for
/// handling data sources with varying schema via automatic schema detection.
///
/// - [Type.auto] is used to let Typesense detect the type of the fields
/// automatically.
///
/// - [Type.stringify] (`string*`) is a way to store the field value (both
/// singular and multi-value/array values) as string.
///
/// [Type.geopoint] is used to index locations, filter and sort on them.
enum Type {
string,
int32,
int64,
float,
bool,
auto,
stringify,
geopoint,
object,
}
extension _Type on Type {
String value(bool isMultivalued) {
switch (this) {
case Type.string:
case Type.int32:
case Type.int64:
case Type.float:
case Type.bool:
case Type.geopoint:
case Type.object:
final description = toString(),
indexOfDot = description.indexOf('.'),
value = description.substring(indexOfDot + 1);
return isMultivalued ? '$value[]' : value;
case Type.auto:
return 'auto';
case Type.stringify:
return 'string*';
}
}
static Type fromValue(String value, bool isMultiValued) =>
Type.values.firstWhere((type) => value == type.value(isMultiValued),
orElse: () => throw ArgumentError('$value is not a defined Type.'));
}
final _multivaluedExpression = RegExp(r'\[\]$');