Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nested fields #140

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/models/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ enum Type {
auto,
stringify,
geopoint,
object,
}

extension _Type on Type {
Expand All @@ -259,6 +260,7 @@ extension _Type on Type {
case Type.float:
case Type.bool:
case Type.geopoint:
case Type.object:
final description = toString(),
indexOfDot = description.indexOf('.'),
value = description.substring(indexOfDot + 1);
Expand Down
9 changes: 9 additions & 0 deletions lib/src/models/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class Schema extends BaseSchema {
/// searching.
final Field? defaultSortingField;

/// Boolean to enable nested fields on the schema, only available for typesense 0.24 or more
final bool? enableNestedFields;

Schema(
this.name,
super.fields, {
this.defaultSortingField,
this.documentCount,
this.enableNestedFields,
});

factory Schema.fromMap(Map<String, dynamic> map) {
Expand All @@ -54,12 +58,14 @@ class Schema extends BaseSchema {
),
)
: null;
final bool? enableNestedFields = map["enable_nested_fields"];

return Schema(
map['name'],
fields,
documentCount: map['num_documents'] ?? 0,
defaultSortingField: defaultSortingField,
enableNestedFields: enableNestedFields,
);
}

Expand All @@ -73,6 +79,9 @@ class Schema extends BaseSchema {
if (documentCount != null) {
map['num_documents'] = documentCount;
}
if (enableNestedFields != null) {
map['enable_nested_fields'] = enableNestedFields;
}
return map;
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/models/field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ void main() {
});
expect(field.type, equals(Type.geopoint));
expect(field.isMultivalued, isTrue);

field = Field.fromMap({
"name": "name",
"type": "object",
});
expect(field.type, equals(Type.object));
expect(field.isMultivalued, isFalse);

field = Field.fromMap({
"name": "name",
"type": "object[]",
});
expect(field.type, equals(Type.object));
expect(field.isMultivalued, isTrue);
});
test('sets default values to fields when null', () {
final field = Field.fromMap({"name": "num_employees", "type": "int32"});
Expand Down
33 changes: 33 additions & 0 deletions test/models/schema_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void main() {
},
defaultSortingField: Field('num_employees'),
documentCount: 0,
enableNestedFields: true,
);
s2 = Schema.fromMap({
"name": "companies",
Expand All @@ -25,6 +26,7 @@ void main() {
],
"default_sorting_field": "num_employees",
"num_documents": 0,
"enable_nested_fields": true,
});
});

Expand Down Expand Up @@ -54,6 +56,10 @@ void main() {
expect(s2.defaultSortingField,
equals(Field('num_employees', type: Type.int32)));
});
test('has a enableNestedFields field', () {
expect(s1.enableNestedFields, equals(true));
expect(s2.enableNestedFields, equals(true));
});
test('has a toMap method', () {
final map = {
"name": "companies",
Expand All @@ -64,6 +70,7 @@ void main() {
],
"default_sorting_field": "num_employees",
"num_documents": 0,
"enable_nested_fields": true,
};

expect(s1.toMap(), equals(map));
Expand Down Expand Up @@ -151,6 +158,32 @@ void main() {
),
);
});
test("with null/empty enableNestedFields is successful", () {
var schema = Schema.fromMap({
"name": "companies",
"fields": [
{"name": "company_name", "type": "string"},
{"name": "num_employees", "type": "int32"},
{"name": "country", "type": "string", "facet": true}
],
"default_sorting_field": "",
"num_documents": 0,
});
expect(schema.name, equals('companies'));
expect(schema.enableNestedFields, isNull);

schema = Schema.fromMap({
"name": "companies",
"fields": [
{"name": "company_name", "type": "string"},
{"name": "num_employees", "type": "int32"},
{"name": "country", "type": "string", "facet": true}
],
"num_documents": 0,
});
expect(schema.name, equals('companies'));
expect(schema.enableNestedFields, isNull);
});
});

group('UpdateSchema', () {
Expand Down
Loading