Skip to content
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
76 changes: 49 additions & 27 deletions example/console-simple/bin/synonyms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,26 @@ Future<void> init(Client client) async {
Future<void> createMultiWay(Client client) async {
try {
logInfoln(log, 'Creating multi-way synonym "synonyms-doofenshmirtz".');
log.fine(
await client.collection('companies').synonyms.upsert(
'synonyms-doofenshmirtz',
{
'synonyms': ['Doofenshmirtz', 'Heinz', 'Evil']
},
),
);
final response = await client.synonymSet('set_name').upsert(
SynonymSetCreateSchema(
items: [
SynonymItemSchema(
id: 'synonyms-doofenshmirtz',
synonyms: ['Doofenshmirtz', 'Heinz', 'Evil'])
],
),
);
log.fine(response.toJson());
await writePropagationDelay();

logInfoln(log, 'Adding "set_name" to "companies" collection.');
final updateResponse = await client.collection('companies').update(
UpdateSchema(
{},
synonymSets: {"set_name"},
),
);
log.fine(updateResponse);
await writePropagationDelay();
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
Expand Down Expand Up @@ -99,15 +110,18 @@ Future<void> search(Client client, String query) async {
Future<void> createOneWay(Client client) async {
try {
logInfoln(log, 'Upserting synonym "synonyms-doofenshmirtz" to be one-way.');
log.fine(
await client.collection('companies').synonyms.upsert(
'synonyms-doofenshmirtz',
{
'root': 'Evil',
'synonyms': ['Doofenshmirtz', 'Heinz']
},
),
);
final response = await client.synonymSet('set_name').upsert(
SynonymSetCreateSchema(
items: [
SynonymItemSchema(
id: 'synonyms-doofenshmirtz',
synonyms: ['Doofenshmirtz', 'Heinz'],
root: 'Evil',
)
],
),
);
log.fine(response.toJson());

await writePropagationDelay();
} on RequestException catch (e, stackTrace) {
Expand All @@ -119,8 +133,17 @@ Future<void> createOneWay(Client client) async {

Future<void> retrieveAll(Client client) async {
try {
logInfoln(log, 'Retrieving all synonyms.');
log.fine(await client.collection('companies').synonyms.retrieve());
logInfoln(log, 'Retrieving all synonyms of `set_name` synonym set.');
final response = await client.synonymSet('set_name').upsert(
SynonymSetCreateSchema(
items: [
SynonymItemSchema(
id: 'synonyms-doofenshmirtz',
synonyms: ['Doofenshmirtz', 'Heinz', 'Evil'])
],
),
);
log.fine(response.toJson());
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
Expand All @@ -131,10 +154,9 @@ Future<void> retrieveAll(Client client) async {
Future<void> retrieve(Client client) async {
try {
logInfoln(log, 'Retrieving synonym "synonyms-doofenshmirtz".');
log.fine(await client
.collection('companies')
.synonym('synonyms-doofenshmirtz')
.retrieve());
final response =
await client.synonymSet('set_name').getItem('synonyms-doofenshmirtz');
log.fine(response.toJson());
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
Expand All @@ -145,10 +167,10 @@ Future<void> retrieve(Client client) async {
Future<void> delete(Client client) async {
try {
logInfoln(log, 'Deleting synonym "synonyms-doofenshmirtz".');
log.fine(await client
.collection('companies')
.synonym('synonyms-doofenshmirtz')
.delete());
final response = await client
.synonymSet('set_name')
.deleteItem('synonyms-doofenshmirtz');
log.fine(response.toJson());

await writePropagationDelay();
} on RequestException catch (e, stackTrace) {
Expand Down
14 changes: 7 additions & 7 deletions example/console-simple/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ packages:
dependency: transitive
description:
name: crypto
sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855"
sha256: c8ea0233063ba03258fbcf2ca4d6dadfefe14f02fab57702265467a19f27fadf
url: "https://pub.dev"
source: hosted
version: "3.0.6"
version: "3.0.7"
http:
dependency: transitive
description:
name: http
sha256: fe7ab022b76f3034adc518fb6ea04a82387620e19977665ea18d30a1cf43442f
sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.6.0"
http_parser:
dependency: transitive
description:
Expand All @@ -45,10 +45,10 @@ packages:
dependency: "direct dev"
description:
name: lints
sha256: c35bb79562d980e9a453fc715854e1ed39e24e7d0297a880ef54e17f9874a9d7
sha256: a5e2b223cb7c9c8efdc663ef484fdd95bb243bff242ef5b13e26883547fce9a0
url: "https://pub.dev"
source: hosted
version: "5.1.1"
version: "6.0.0"
logging:
dependency: "direct main"
description:
Expand Down Expand Up @@ -121,4 +121,4 @@ packages:
source: hosted
version: "1.1.0"
sdks:
dart: ">=3.6.1 <4.0.0"
dart: ">=3.8.0-0 <4.0.0"
2 changes: 1 addition & 1 deletion example/console-simple/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dependencies:
logging: ^1.3.0

dev_dependencies:
lints: ^5.1.1
lints: ^6.0.0
92 changes: 38 additions & 54 deletions lib/src/models/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ abstract class BaseSchema {
/// [fields] used for querying, filtering and faceting.
final Set<Field> fields;

BaseSchema(this.fields);
/// The synonym sets feature allows you to define search terms that should
/// be considered equivalent.
final Set<String>? synonymSets;

/// List of curation set names associated with this collection.
final Set<String>? curationSets;

/// The fields within the metadata object are persisted and returned in the
/// GET /collections end-point.
final Map<String, String>? metadata;

BaseSchema(
this.fields, {
this.synonymSets,
this.curationSets,
this.metadata,
});

Map<String, dynamic> toMap() {
final map = <String, dynamic>{};
map['fields'] = fields.map((field) => field.toMap()).toList();
if (fields.isNotEmpty) {
map['fields'] = fields.map((field) => field.toMap()).toList();
}
map['synonym_sets'] = synonymSets?.toList() ?? [];
map['curation_sets'] = curationSets?.toList() ?? [];
map['metadata'] = metadata ?? {};
return map;
}

Expand Down Expand Up @@ -37,12 +58,6 @@ class Schema extends BaseSchema {
/// Number of memory shards used by the collection.
final int? numMemoryShards;

/// List of curation set names associated with this collection.
final List<String>? curationSets;

/// List of synonym set names associated with this collection.
final List<String>? synonymSets;

/// List of special characters to index at the collection level.
final List<String>? symbolsToIndex;

Expand All @@ -57,10 +72,11 @@ class Schema extends BaseSchema {
this.enableNestedFields,
this.createdAt,
this.numMemoryShards,
this.curationSets,
this.synonymSets,
this.symbolsToIndex,
this.tokenSeparators,
super.curationSets,
super.synonymSets,
super.metadata,
});

factory Schema.fromMap(Map<String, dynamic> map) {
Expand All @@ -87,15 +103,16 @@ class Schema extends BaseSchema {
return Schema(
map['name'],
fields,
documentCount: map['num_documents'] ?? 0,
defaultSortingField: defaultSortingField,
documentCount: map['num_documents'] ?? 0,
enableNestedFields: enableNestedFields,
createdAt: map['created_at'],
numMemoryShards: map['num_memory_shards'],
curationSets: (map['curation_sets'] as List?)?.cast<String>(),
synonymSets: (map['synonym_sets'] as List?)?.cast<String>(),
symbolsToIndex: (map['symbols_to_index'] as List?)?.cast<String>(),
tokenSeparators: (map['token_separators'] as List?)?.cast<String>(),
curationSets: (map['curation_sets'] as List?)?.cast<String>().toSet(),
synonymSets: (map['synonym_sets'] as List?)?.cast<String>().toSet(),
metadata: (map['metadata'] as Map?)?.cast<String, String>(),
);
}

Expand All @@ -118,12 +135,6 @@ class Schema extends BaseSchema {
if (numMemoryShards != null) {
map['num_memory_shards'] = numMemoryShards;
}
if (curationSets != null) {
map['curation_sets'] = curationSets;
}
if (synonymSets != null) {
map['synonym_sets'] = synonymSets;
}
if (symbolsToIndex != null) {
map['symbols_to_index'] = symbolsToIndex;
}
Expand All @@ -135,25 +146,8 @@ class Schema extends BaseSchema {
}

class UpdateSchema extends BaseSchema {
/// List of synonym set IDs to associate with the collection.
final List<String>? synonymSets;

/// List of curation set IDs to associate with the collection.
final List<String>? curationSets;

/// List of special characters to index at the collection level.
final List<String>? symbolsToIndex;

/// List of characters to use as token separators at the collection level.
final List<String>? tokenSeparators;

UpdateSchema(
Set<UpdateField> super.fields, {
this.synonymSets,
this.curationSets,
this.symbolsToIndex,
this.tokenSeparators,
});
UpdateSchema(Set<UpdateField> super.fields,
{super.synonymSets, super.curationSets, super.metadata});

factory UpdateSchema.fromMap(Map<String, dynamic> map) {
final Set<UpdateField> fields = (map['fields'] != null)
Expand All @@ -164,30 +158,20 @@ class UpdateSchema extends BaseSchema {

return UpdateSchema(
fields,
synonymSets: (map['synonym_sets'] as List?)?.cast<String>(),
curationSets: (map['curation_sets'] as List?)?.cast<String>(),
symbolsToIndex: (map['symbols_to_index'] as List?)?.cast<String>(),
tokenSeparators: (map['token_separators'] as List?)?.cast<String>(),
synonymSets: (map['synonym_sets'] as List?)?.cast<String>().toSet(),
curationSets: (map['curation_sets'] as List?)?.cast<String>().toSet(),
metadata: (map['metadata'] as Map?)?.cast<String, String>(),
);
}

@override
Map<String, dynamic> toMap() {
final map = super.toMap();
if (synonymSets != null) {
map['synonym_sets'] = synonymSets;
}
if (curationSets != null) {
map['curation_sets'] = curationSets;
}
if (symbolsToIndex != null) {
map['symbols_to_index'] = symbolsToIndex;
}
if (tokenSeparators != null) {
map['token_separators'] = tokenSeparators;
}
return map;
}

@override
String toString() => toMap().toString();
}

ArgumentError _defaultSortingFieldNotInSchema(String name) =>
Expand Down
14 changes: 8 additions & 6 deletions lib/src/models/synonym_sets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SynonymItemDeleteSchema {
SynonymItemDeleteSchema(
id: json['id'] as String,
);

Map<String, dynamic> toJson() => {'id': id};
}

class SynonymSetCreateSchema {
Expand All @@ -56,8 +58,8 @@ class SynonymSetCreateSchema {
factory SynonymSetCreateSchema.fromJson(Map<String, dynamic> json) =>
SynonymSetCreateSchema(
items: (json['items'] as List? ?? [])
.map((item) =>
SynonymItemSchema.fromJson(Map<String, dynamic>.from(item as Map)))
.map((item) => SynonymItemSchema.fromJson(
Map<String, dynamic>.from(item as Map)))
.toList(),
);
}
Expand All @@ -74,8 +76,8 @@ class SynonymSetSchema extends SynonymSetCreateSchema {
SynonymSetSchema(
name: json['name'] as String,
items: (json['items'] as List? ?? [])
.map((item) =>
SynonymItemSchema.fromJson(Map<String, dynamic>.from(item as Map)))
.map((item) => SynonymItemSchema.fromJson(
Map<String, dynamic>.from(item as Map)))
.toList(),
);
}
Expand All @@ -86,8 +88,8 @@ class SynonymSetRetrieveSchema extends SynonymSetCreateSchema {
factory SynonymSetRetrieveSchema.fromJson(Map<String, dynamic> json) =>
SynonymSetRetrieveSchema(
items: (json['items'] as List? ?? [])
.map((item) =>
SynonymItemSchema.fromJson(Map<String, dynamic>.from(item as Map)))
.map((item) => SynonymItemSchema.fromJson(
Map<String, dynamic>.from(item as Map)))
.toList(),
);
}
Expand Down
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ environment:
sdk: ^3.6.1

dependencies:
http: ^1.3.0
crypto: ^3.0.6
http: ^1.6.0
crypto: ^3.0.7

dev_dependencies:
test: ^1.25.14
mockito: ^5.4.5
test: ^1.29.0
mockito: ^5.6.3
lints: ^6.0.0
build_runner: ^2.4.14
analyzer: ^10.0.0
build_runner: ^2.10.5
analyzer: ^10.0.1
Loading