From 53794b6cdc71f0ae9334ce873a8f08b1ea969e11 Mon Sep 17 00:00:00 2001 From: Harpreet Sangar Date: Sat, 31 Jan 2026 16:54:50 +0530 Subject: [PATCH] Fix synonyms example. --- example/console-simple/bin/synonyms.dart | 76 +++++++++++++------- example/console-simple/pubspec.lock | 14 ++-- example/console-simple/pubspec.yaml | 2 +- lib/src/models/schema.dart | 92 ++++++++++-------------- lib/src/models/synonym_sets.dart | 14 ++-- pubspec.yaml | 12 ++-- 6 files changed, 109 insertions(+), 101 deletions(-) diff --git a/example/console-simple/bin/synonyms.dart b/example/console-simple/bin/synonyms.dart index 410c83e..ad52f40 100644 --- a/example/console-simple/bin/synonyms.dart +++ b/example/console-simple/bin/synonyms.dart @@ -63,15 +63,26 @@ Future init(Client client) async { Future 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); @@ -99,15 +110,18 @@ Future search(Client client, String query) async { Future 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) { @@ -119,8 +133,17 @@ Future createOneWay(Client client) async { Future 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) { @@ -131,10 +154,9 @@ Future retrieveAll(Client client) async { Future 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) { @@ -145,10 +167,10 @@ Future retrieve(Client client) async { Future 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) { diff --git a/example/console-simple/pubspec.lock b/example/console-simple/pubspec.lock index 212cefc..1f60d52 100644 --- a/example/console-simple/pubspec.lock +++ b/example/console-simple/pubspec.lock @@ -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: @@ -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: @@ -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" diff --git a/example/console-simple/pubspec.yaml b/example/console-simple/pubspec.yaml index 6a4d779..54cfe1b 100644 --- a/example/console-simple/pubspec.yaml +++ b/example/console-simple/pubspec.yaml @@ -12,4 +12,4 @@ dependencies: logging: ^1.3.0 dev_dependencies: - lints: ^5.1.1 + lints: ^6.0.0 diff --git a/lib/src/models/schema.dart b/lib/src/models/schema.dart index 2e8cd94..28f30cc 100644 --- a/lib/src/models/schema.dart +++ b/lib/src/models/schema.dart @@ -4,11 +4,32 @@ abstract class BaseSchema { /// [fields] used for querying, filtering and faceting. final Set fields; - BaseSchema(this.fields); + /// The synonym sets feature allows you to define search terms that should + /// be considered equivalent. + final Set? synonymSets; + + /// List of curation set names associated with this collection. + final Set? curationSets; + + /// The fields within the metadata object are persisted and returned in the + /// GET /collections end-point. + final Map? metadata; + + BaseSchema( + this.fields, { + this.synonymSets, + this.curationSets, + this.metadata, + }); Map toMap() { final map = {}; - 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; } @@ -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? curationSets; - - /// List of synonym set names associated with this collection. - final List? synonymSets; - /// List of special characters to index at the collection level. final List? symbolsToIndex; @@ -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 map) { @@ -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(), - synonymSets: (map['synonym_sets'] as List?)?.cast(), symbolsToIndex: (map['symbols_to_index'] as List?)?.cast(), tokenSeparators: (map['token_separators'] as List?)?.cast(), + curationSets: (map['curation_sets'] as List?)?.cast().toSet(), + synonymSets: (map['synonym_sets'] as List?)?.cast().toSet(), + metadata: (map['metadata'] as Map?)?.cast(), ); } @@ -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; } @@ -135,25 +146,8 @@ class Schema extends BaseSchema { } class UpdateSchema extends BaseSchema { - /// List of synonym set IDs to associate with the collection. - final List? synonymSets; - - /// List of curation set IDs to associate with the collection. - final List? curationSets; - - /// List of special characters to index at the collection level. - final List? symbolsToIndex; - - /// List of characters to use as token separators at the collection level. - final List? tokenSeparators; - - UpdateSchema( - Set super.fields, { - this.synonymSets, - this.curationSets, - this.symbolsToIndex, - this.tokenSeparators, - }); + UpdateSchema(Set super.fields, + {super.synonymSets, super.curationSets, super.metadata}); factory UpdateSchema.fromMap(Map map) { final Set fields = (map['fields'] != null) @@ -164,30 +158,20 @@ class UpdateSchema extends BaseSchema { return UpdateSchema( fields, - synonymSets: (map['synonym_sets'] as List?)?.cast(), - curationSets: (map['curation_sets'] as List?)?.cast(), - symbolsToIndex: (map['symbols_to_index'] as List?)?.cast(), - tokenSeparators: (map['token_separators'] as List?)?.cast(), + synonymSets: (map['synonym_sets'] as List?)?.cast().toSet(), + curationSets: (map['curation_sets'] as List?)?.cast().toSet(), + metadata: (map['metadata'] as Map?)?.cast(), ); } @override Map 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) => diff --git a/lib/src/models/synonym_sets.dart b/lib/src/models/synonym_sets.dart index f2f3371..6c799df 100644 --- a/lib/src/models/synonym_sets.dart +++ b/lib/src/models/synonym_sets.dart @@ -42,6 +42,8 @@ class SynonymItemDeleteSchema { SynonymItemDeleteSchema( id: json['id'] as String, ); + + Map toJson() => {'id': id}; } class SynonymSetCreateSchema { @@ -56,8 +58,8 @@ class SynonymSetCreateSchema { factory SynonymSetCreateSchema.fromJson(Map json) => SynonymSetCreateSchema( items: (json['items'] as List? ?? []) - .map((item) => - SynonymItemSchema.fromJson(Map.from(item as Map))) + .map((item) => SynonymItemSchema.fromJson( + Map.from(item as Map))) .toList(), ); } @@ -74,8 +76,8 @@ class SynonymSetSchema extends SynonymSetCreateSchema { SynonymSetSchema( name: json['name'] as String, items: (json['items'] as List? ?? []) - .map((item) => - SynonymItemSchema.fromJson(Map.from(item as Map))) + .map((item) => SynonymItemSchema.fromJson( + Map.from(item as Map))) .toList(), ); } @@ -86,8 +88,8 @@ class SynonymSetRetrieveSchema extends SynonymSetCreateSchema { factory SynonymSetRetrieveSchema.fromJson(Map json) => SynonymSetRetrieveSchema( items: (json['items'] as List? ?? []) - .map((item) => - SynonymItemSchema.fromJson(Map.from(item as Map))) + .map((item) => SynonymItemSchema.fromJson( + Map.from(item as Map))) .toList(), ); } diff --git a/pubspec.yaml b/pubspec.yaml index 6a626a2..e53001d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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