Skip to content

Commit 665b03d

Browse files
authored
Merge pull request #154 from tharropoulos/v30.0
feat(schema): add missing field and collection schema properties
2 parents 0ca05f6 + de38f1f commit 665b03d

File tree

2 files changed

+201
-3
lines changed

2 files changed

+201
-3
lines changed

lib/src/models/field.dart

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,39 @@ class Field {
4848
/// Example value: `ReferencedCollectionName.fieldName`.
4949
final String? reference;
5050

51+
/// Enable stemming for this string field.
52+
final bool stem;
53+
54+
/// Whether to store the field value on disk.
55+
///
56+
/// When set to `false`, the field value won't be stored on disk.
57+
final bool store;
58+
59+
/// Enable range index for this numerical field.
60+
///
61+
/// Useful for efficient range queries on numerical fields.
62+
final bool rangeIndex;
63+
64+
/// List of characters to use as token separators for this field.
65+
///
66+
/// Overrides the collection-level token_separators setting for this field.
67+
final List<String>? tokenSeparators;
68+
69+
/// List of special characters to index as part of tokens for this field.
70+
///
71+
/// Overrides the collection-level symbols_to_index setting for this field.
72+
final List<String>? symbolsToIndex;
73+
74+
/// Custom stem dictionary for this field.
75+
///
76+
/// Allows specifying custom stemming rules for this field.
77+
final String? stemDictionary;
78+
79+
/// Maximum length to truncate the field value to.
80+
///
81+
/// Token values longer than this will be truncated.
82+
final int? truncateLen;
83+
5184
Field(
5285
this.name, {
5386
this.type,
@@ -60,6 +93,13 @@ class Field {
6093
this.sort = false,
6194
this.enableInfixSearch = false,
6295
this.reference,
96+
this.stem = false,
97+
this.store = true,
98+
this.rangeIndex = false,
99+
this.tokenSeparators,
100+
this.symbolsToIndex,
101+
this.stemDictionary,
102+
this.truncateLen,
63103
}) {
64104
if (name.isEmpty) {
65105
throw ArgumentError('Ensure Field.name is not empty');
@@ -84,6 +124,13 @@ class Field {
84124
sort: map['sort'] ?? false,
85125
enableInfixSearch: map['infix'] ?? false,
86126
reference: map['reference'],
127+
stem: map['stem'] ?? false,
128+
store: map['store'] ?? true,
129+
rangeIndex: map['range_index'] ?? false,
130+
tokenSeparators: (map['token_separators'] as List?)?.cast<String>(),
131+
symbolsToIndex: (map['symbols_to_index'] as List?)?.cast<String>(),
132+
stemDictionary: map['stem_dictionary'],
133+
truncateLen: map['truncate_len'],
87134
);
88135
}
89136

@@ -117,6 +164,27 @@ class Field {
117164
if (reference != null) {
118165
map['reference'] = reference;
119166
}
167+
if (stem) {
168+
map['stem'] = true;
169+
}
170+
if (!store) {
171+
map['store'] = false;
172+
}
173+
if (rangeIndex) {
174+
map['range_index'] = true;
175+
}
176+
if (tokenSeparators != null) {
177+
map['token_separators'] = tokenSeparators;
178+
}
179+
if (symbolsToIndex != null) {
180+
map['symbols_to_index'] = symbolsToIndex;
181+
}
182+
if (stemDictionary != null && stemDictionary!.isNotEmpty) {
183+
map['stem_dictionary'] = stemDictionary;
184+
}
185+
if (truncateLen != null) {
186+
map['truncate_len'] = truncateLen;
187+
}
120188
return map;
121189
}
122190

@@ -137,7 +205,14 @@ class Field {
137205
locale.hashCode ^
138206
sort.hashCode ^
139207
enableInfixSearch.hashCode ^
140-
reference.hashCode;
208+
reference.hashCode ^
209+
stem.hashCode ^
210+
store.hashCode ^
211+
rangeIndex.hashCode ^
212+
tokenSeparators.hashCode ^
213+
symbolsToIndex.hashCode ^
214+
stemDictionary.hashCode ^
215+
truncateLen.hashCode;
141216

142217
@override
143218
bool operator ==(Object other) {
@@ -153,8 +228,25 @@ class Field {
153228
other.locale == locale &&
154229
other.sort == sort &&
155230
other.enableInfixSearch == enableInfixSearch &&
156-
other.reference == reference;
231+
other.reference == reference &&
232+
other.stem == stem &&
233+
other.store == store &&
234+
other.rangeIndex == rangeIndex &&
235+
_listEquals(other.tokenSeparators, tokenSeparators) &&
236+
_listEquals(other.symbolsToIndex, symbolsToIndex) &&
237+
other.stemDictionary == stemDictionary &&
238+
other.truncateLen == truncateLen;
239+
}
240+
}
241+
242+
bool _listEquals<T>(List<T>? a, List<T>? b) {
243+
if (a == null && b == null) return true;
244+
if (a == null || b == null) return false;
245+
if (a.length != b.length) return false;
246+
for (int i = 0; i < a.length; i++) {
247+
if (a[i] != b[i]) return false;
157248
}
249+
return true;
158250
}
159251

160252
/// Used to update a colletion's fields.
@@ -177,6 +269,14 @@ class UpdateField extends Field {
177269
super.locale,
178270
super.sort,
179271
super.enableInfixSearch,
272+
super.reference,
273+
super.stem,
274+
super.store,
275+
super.rangeIndex,
276+
super.tokenSeparators,
277+
super.symbolsToIndex,
278+
super.stemDictionary,
279+
super.truncateLen,
180280
this.shouldDrop = false,
181281
});
182282

@@ -203,6 +303,14 @@ class UpdateField extends Field {
203303
locale: field.locale,
204304
sort: field.sort,
205305
enableInfixSearch: field.enableInfixSearch,
306+
reference: field.reference,
307+
stem: field.stem,
308+
store: field.store,
309+
rangeIndex: field.rangeIndex,
310+
tokenSeparators: field.tokenSeparators,
311+
symbolsToIndex: field.symbolsToIndex,
312+
stemDictionary: field.stemDictionary,
313+
truncateLen: field.truncateLen,
206314
shouldDrop: map['drop'] ?? false,
207315
);
208316
}
@@ -263,6 +371,7 @@ enum Type {
263371
geopoint,
264372
geopolygon,
265373
object,
374+
image,
266375
}
267376

268377
extension _Type on Type {
@@ -276,6 +385,7 @@ extension _Type on Type {
276385
case Type.geopoint:
277386
case Type.geopolygon:
278387
case Type.object:
388+
case Type.image:
279389
final description = toString(),
280390
indexOfDot = description.indexOf('.'),
281391
value = description.substring(indexOfDot + 1);

lib/src/models/schema.dart

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,36 @@ class Schema extends BaseSchema {
3131
/// Boolean to enable nested fields on the schema, only available for typesense 0.24 or more
3232
final bool? enableNestedFields;
3333

34+
/// Timestamp when the collection was created.
35+
final int? createdAt;
36+
37+
/// Number of memory shards used by the collection.
38+
final int? numMemoryShards;
39+
40+
/// List of curation set names associated with this collection.
41+
final List<String>? curationSets;
42+
43+
/// List of synonym set names associated with this collection.
44+
final List<String>? synonymSets;
45+
46+
/// List of special characters to index at the collection level.
47+
final List<String>? symbolsToIndex;
48+
49+
/// List of characters to use as token separators at the collection level.
50+
final List<String>? tokenSeparators;
51+
3452
Schema(
3553
this.name,
3654
super.fields, {
3755
this.defaultSortingField,
3856
this.documentCount,
3957
this.enableNestedFields,
58+
this.createdAt,
59+
this.numMemoryShards,
60+
this.curationSets,
61+
this.synonymSets,
62+
this.symbolsToIndex,
63+
this.tokenSeparators,
4064
});
4165

4266
factory Schema.fromMap(Map<String, dynamic> map) {
@@ -66,6 +90,12 @@ class Schema extends BaseSchema {
6690
documentCount: map['num_documents'] ?? 0,
6791
defaultSortingField: defaultSortingField,
6892
enableNestedFields: enableNestedFields,
93+
createdAt: map['created_at'],
94+
numMemoryShards: map['num_memory_shards'],
95+
curationSets: (map['curation_sets'] as List?)?.cast<String>(),
96+
synonymSets: (map['synonym_sets'] as List?)?.cast<String>(),
97+
symbolsToIndex: (map['symbols_to_index'] as List?)?.cast<String>(),
98+
tokenSeparators: (map['token_separators'] as List?)?.cast<String>(),
6999
);
70100
}
71101

@@ -82,12 +112,48 @@ class Schema extends BaseSchema {
82112
if (enableNestedFields != null) {
83113
map['enable_nested_fields'] = enableNestedFields;
84114
}
115+
if (createdAt != null) {
116+
map['created_at'] = createdAt;
117+
}
118+
if (numMemoryShards != null) {
119+
map['num_memory_shards'] = numMemoryShards;
120+
}
121+
if (curationSets != null) {
122+
map['curation_sets'] = curationSets;
123+
}
124+
if (synonymSets != null) {
125+
map['synonym_sets'] = synonymSets;
126+
}
127+
if (symbolsToIndex != null) {
128+
map['symbols_to_index'] = symbolsToIndex;
129+
}
130+
if (tokenSeparators != null) {
131+
map['token_separators'] = tokenSeparators;
132+
}
85133
return map;
86134
}
87135
}
88136

89137
class UpdateSchema extends BaseSchema {
90-
UpdateSchema(Set<UpdateField> super.fields);
138+
/// List of synonym set IDs to associate with the collection.
139+
final List<String>? synonymSets;
140+
141+
/// List of curation set IDs to associate with the collection.
142+
final List<String>? curationSets;
143+
144+
/// List of special characters to index at the collection level.
145+
final List<String>? symbolsToIndex;
146+
147+
/// List of characters to use as token separators at the collection level.
148+
final List<String>? tokenSeparators;
149+
150+
UpdateSchema(
151+
Set<UpdateField> super.fields, {
152+
this.synonymSets,
153+
this.curationSets,
154+
this.symbolsToIndex,
155+
this.tokenSeparators,
156+
});
91157

92158
factory UpdateSchema.fromMap(Map<String, dynamic> map) {
93159
final Set<UpdateField> fields = (map['fields'] != null)
@@ -98,8 +164,30 @@ class UpdateSchema extends BaseSchema {
98164

99165
return UpdateSchema(
100166
fields,
167+
synonymSets: (map['synonym_sets'] as List?)?.cast<String>(),
168+
curationSets: (map['curation_sets'] as List?)?.cast<String>(),
169+
symbolsToIndex: (map['symbols_to_index'] as List?)?.cast<String>(),
170+
tokenSeparators: (map['token_separators'] as List?)?.cast<String>(),
101171
);
102172
}
173+
174+
@override
175+
Map<String, dynamic> toMap() {
176+
final map = super.toMap();
177+
if (synonymSets != null) {
178+
map['synonym_sets'] = synonymSets;
179+
}
180+
if (curationSets != null) {
181+
map['curation_sets'] = curationSets;
182+
}
183+
if (symbolsToIndex != null) {
184+
map['symbols_to_index'] = symbolsToIndex;
185+
}
186+
if (tokenSeparators != null) {
187+
map['token_separators'] = tokenSeparators;
188+
}
189+
return map;
190+
}
103191
}
104192

105193
ArgumentError _defaultSortingFieldNotInSchema(String name) =>

0 commit comments

Comments
 (0)