From e6793093b80557521ba293ecb7fbe6c78f5f735e Mon Sep 17 00:00:00 2001 From: Harpreet Sangar Date: Mon, 24 Jul 2023 13:04:59 +0530 Subject: [PATCH] Support `Presets` operations. --- CHANGELOG.md | 6 + README.md | 2 +- example/console-simple/bin/main.dart | 2 + example/console-simple/bin/presets.dart | 145 ++++++++++++++++++++++++ example/console-simple/pubspec.lock | 53 +++++---- lib/src/client.dart | 15 ++- lib/src/preset.dart | 21 ++++ lib/src/presets.dart | 22 ++++ pubspec.yaml | 8 +- 9 files changed, 249 insertions(+), 25 deletions(-) create mode 100644 example/console-simple/bin/presets.dart create mode 100644 lib/src/preset.dart create mode 100644 lib/src/presets.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd4f05..4b47b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.4.0 + +* Requires Dart 3.0 or later. +* Added `Presets` support. +* Updated dependencies. + # 0.3.1 * Added `dimensions` field to support creating a vector field in schema. diff --git a/README.md b/README.md index e2e469b..2f234aa 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Add `typesense` as a [dependency in your pubspec.yaml file](https://flutter.dev/ ```@yaml dependencies: - typesense: ^0.3.1 + typesense: ^0.4.0 ``` ## Usage diff --git a/example/console-simple/bin/main.dart b/example/console-simple/bin/main.dart index a123341..d0600f9 100644 --- a/example/console-simple/bin/main.dart +++ b/example/console-simple/bin/main.dart @@ -10,6 +10,7 @@ import 'keys.dart' as keys; import 'overrides.dart' as overrides; import 'synonyms.dart' as synonyms; import 'aliases.dart' as aliases; +import 'presets.dart' as presets; import 'cluster_operations.dart' as cluster_operations; import 'miscellaneous.dart' as miscellaneous; @@ -57,6 +58,7 @@ void main() async { await overrides.runExample(client); await synonyms.runExample(client); await aliases.runExample(client); + await presets.runExample(client); await cluster_operations.runExample(client); await miscellaneous.runExample(client); } diff --git a/example/console-simple/bin/presets.dart b/example/console-simple/bin/presets.dart new file mode 100644 index 0000000..b600bd4 --- /dev/null +++ b/example/console-simple/bin/presets.dart @@ -0,0 +1,145 @@ +import 'package:typesense/typesense.dart'; +import 'package:logging/logging.dart'; + +import 'util.dart'; +import 'collections.dart' as collections; +import 'documents.dart' as documents; + +final log = Logger('Presets'); + +Future runExample(Client client) async { + logInfoln(log, '--Presets example--'); + await init(client); + await create(client); + await search(client); + await retrieveAll(client); + await retrieve(client); + await delete(client); + await collections.delete(client); +} + +final _documents = [ + { + 'id': '124', + 'company_name': 'Stark Industries', + 'num_employees': 5215, + 'country': 'USA' + }, + { + 'id': '125', + 'company_name': 'Acme Corp', + 'num_employees': 1002, + 'country': 'France' + }, + { + 'id': '127', + 'company_name': 'Stark Corp', + 'num_employees': 1031, + 'country': 'USA' + }, + { + 'id': '126', + 'company_name': 'Doofenshmirtz Inc', + 'num_employees': 2, + 'country': 'Tri-State Area' + } +]; + +Future init(Client client) async { + await documents.init(client); + await documents.importDocs(client, 'companies', _documents); +} + +Future create(Client client) async { + try { + logInfoln(log, 'Creating preset "country_filter_preset".'); + log.fine( + await client.presets.upsert('country_filter_preset', { + 'value': { + 'filter_by': 'country:=USA', + } + }), + ); + + await writePropagationDelay(); + } on RequestException catch (e, stackTrace) { + log.severe(e.message, e, stackTrace); + } catch (e, stackTrace) { + log.severe(e, stackTrace); + } +} + +Future search(Client client) async { + try { + logInfoln(log, 'Simple search.'); + log.fine( + await client.collection('companies').documents.search( + { + 'q': 'Corp', + 'query_by': 'company_name', + }, + ), + ); + + logInfoln(log, 'Search with filter_by.'); + log.fine( + await client.collection('companies').documents.search( + { + 'q': 'Corp', + 'query_by': 'company_name', + 'filter_by': 'country:=USA', + }, + ), + ); + + logInfoln(log, 'Search using preset.'); + log.fine( + await client.collection('companies').documents.search( + { + 'q': 'Corp', + 'query_by': 'company_name', + 'preset': 'country_filter_preset', + }, + ), + ); + } on RequestException catch (e, stackTrace) { + log.severe(e.message, e, stackTrace); + } catch (e, stackTrace) { + log.severe(e, stackTrace); + } +} + +Future retrieveAll(Client client) async { + try { + logInfoln(log, 'Retrieving all presets.'); + log.fine(await client.presets.retrieve()); + } on RequestException catch (e, stackTrace) { + log.severe(e.message, e, stackTrace); + } catch (e, stackTrace) { + log.severe(e, stackTrace); + } +} + +Future retrieve(Client client) async { + try { + logInfoln(log, 'Retrieving preset "country_filter_preset".'); + log.fine(await client.preset('country_filter_preset').retrieve()); + } on RequestException catch (e, stackTrace) { + log.severe(e.message, e, stackTrace); + } catch (e, stackTrace) { + log.severe(e, stackTrace); + } +} + +Future delete(Client client) async { + try { + logInfoln(log, 'Deleting preset "country_filter_preset".'); + log.fine(await client.preset('country_filter_preset').delete()); + + await writePropagationDelay(); + } on RequestException catch (e, stackTrace) { + log.severe(e.message, e, stackTrace); + } catch (e, stackTrace) { + log.severe(e, stackTrace); + } +} diff --git a/example/console-simple/pubspec.lock b/example/console-simple/pubspec.lock index ff90a31..7f0c152 100644 --- a/example/console-simple/pubspec.lock +++ b/example/console-simple/pubspec.lock @@ -5,105 +5,120 @@ packages: dependency: transitive description: name: async - url: "https://pub.dartlang.org" + sha256: db4766341bd8ecb66556f31ab891a5d596ef829221993531bd64a8e6342f0cda + url: "https://pub.dev" source: hosted version: "2.8.2" charcode: dependency: transitive description: name: charcode - url: "https://pub.dartlang.org" + sha256: fb98c0f6d12c920a02ee2d998da788bca066ca5f148492b7085ee23372b12306 + url: "https://pub.dev" source: hosted version: "1.3.1" collection: dependency: transitive description: name: collection - url: "https://pub.dartlang.org" + sha256: "6d4193120997ecfd09acf0e313f13dc122b119e5eca87ef57a7d065ec9183762" + url: "https://pub.dev" source: hosted version: "1.15.0" crypto: dependency: transitive description: name: crypto - url: "https://pub.dartlang.org" + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "3.0.3" flutter_lints: dependency: "direct dev" description: name: flutter_lints - url: "https://pub.dartlang.org" + sha256: b543301ad291598523947dc534aaddc5aaad597b709d2426d3a0e0d44c5cb493 + url: "https://pub.dev" source: hosted version: "1.0.4" http: dependency: transitive description: name: http - url: "https://pub.dartlang.org" + sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525" + url: "https://pub.dev" source: hosted - version: "0.13.4" + version: "1.1.0" http_parser: dependency: transitive description: name: http_parser - url: "https://pub.dartlang.org" + sha256: e362d639ba3bc07d5a71faebb98cde68c05bfbcfbbb444b60b6f60bb67719185 + url: "https://pub.dev" source: hosted version: "4.0.0" lints: dependency: transitive description: name: lints - url: "https://pub.dartlang.org" + sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c + url: "https://pub.dev" source: hosted version: "1.0.1" logging: dependency: "direct main" description: name: logging - url: "https://pub.dartlang.org" + sha256: "0520a4826042a8a5d09ddd4755623a50d37ee536d79a70452aff8c8ad7bb6c27" + url: "https://pub.dev" source: hosted version: "1.0.1" meta: dependency: transitive description: name: meta - url: "https://pub.dartlang.org" + sha256: "5202fdd37b4da5fd14a237ed0a01cad6c1efd4c99b5b5a0d3c9237f3728c9485" + url: "https://pub.dev" source: hosted version: "1.7.0" path: dependency: transitive description: name: path - url: "https://pub.dartlang.org" + sha256: "2ad4cddff7f5cc0e2d13069f2a3f7a73ca18f66abd6f5ecf215219cdb3638edb" + url: "https://pub.dev" source: hosted version: "1.8.0" source_span: dependency: transitive description: name: source_span - url: "https://pub.dartlang.org" + sha256: d5f89a9e52b36240a80282b3dc0667dd36e53459717bb17b8fb102d30496606a + url: "https://pub.dev" source: hosted version: "1.8.1" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.dartlang.org" + sha256: dd11571b8a03f7cadcf91ec26a77e02bfbd6bbba2a512924d3116646b4198fc4 + url: "https://pub.dev" source: hosted version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.dartlang.org" + sha256: a88162591b02c1f3a3db3af8ce1ea2b374bd75a7bb8d5e353bcfbdc79d719830 + url: "https://pub.dev" source: hosted version: "1.2.0" typed_data: dependency: transitive description: name: typed_data - url: "https://pub.dartlang.org" + sha256: "53bdf7e979cfbf3e28987552fd72f637e63f3c8724c9e56d9246942dc2fa36ee" + url: "https://pub.dev" source: hosted version: "1.3.0" typesense: @@ -112,6 +127,6 @@ packages: path: "../.." relative: true source: path - version: "0.3.1" + version: "0.3.2" sdks: - dart: ">=2.17.0 <3.0.0" + dart: ">=3.0.0 <4.0.0" diff --git a/lib/src/client.dart b/lib/src/client.dart index f79a5e3..bb8928d 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -15,6 +15,8 @@ import 'key.dart'; import 'debug.dart'; import 'stats.dart'; import 'health.dart'; +import 'preset.dart'; +import 'presets.dart'; import 'metrics.dart'; import 'operations.dart'; import 'multi_search.dart'; @@ -26,6 +28,7 @@ class Client { final Collections collections; final Aliases aliases; final Keys keys; + final Presets presets; final Debug debug; final Stats stats; final Health health; @@ -34,7 +37,8 @@ class Client { final MultiSearch multiSearch; final _individualCollections = HashMap(), _individualAliases = HashMap(), - _individualKeys = HashMap(); + _individualKeys = HashMap(), + _individualPresets = HashMap(); Client._( this.config, @@ -43,6 +47,7 @@ class Client { this.collections, this.aliases, this.keys, + this.presets, this.debug, this.stats, this.health, @@ -68,6 +73,7 @@ class Client { Collections(apiCall, CollectionsApiCall(config, nodePool)), Aliases(apiCall), Keys(apiCall), + Presets(apiCall), Debug(apiCall), Stats(apiCall), Health(apiCall), @@ -100,4 +106,11 @@ class Client { } return _individualKeys[id]!; } + + Preset preset(String presetName) { + if (!_individualPresets.containsKey(presetName)) { + _individualPresets[presetName] = Preset(presetName, _apiCall); + } + return _individualPresets[presetName]!; + } } diff --git a/lib/src/preset.dart b/lib/src/preset.dart new file mode 100644 index 0000000..21a93f4 --- /dev/null +++ b/lib/src/preset.dart @@ -0,0 +1,21 @@ +import 'presets.dart'; +import 'services/api_call.dart'; + +class Preset { + final String _presetName; + final ApiCall _apiCall; + + Preset(String presetName, ApiCall apiCall) + : _presetName = presetName, + _apiCall = apiCall; + + Future> retrieve() async { + return await _apiCall.get(_endpointPath); + } + + Future> delete() async { + return await _apiCall.delete(_endpointPath); + } + + String get _endpointPath => '${Presets.resourcepath}/$_presetName'; +} diff --git a/lib/src/presets.dart b/lib/src/presets.dart new file mode 100644 index 0000000..3349845 --- /dev/null +++ b/lib/src/presets.dart @@ -0,0 +1,22 @@ +import 'services/api_call.dart'; + +class Presets { + final ApiCall _apiCall; + static const resourcepath = '/presets'; + + Presets(ApiCall apiCall) : _apiCall = apiCall; + + /// Creates/updates a preset corresponding to [presetName]. + Future> upsert( + String presetName, Map mapping) async { + return await _apiCall.put( + '$resourcepath/$presetName', + bodyParameters: mapping, + ); + } + + /// Retrieves all aliases and the corresponding collections that they map to. + Future> retrieve() async { + return await _apiCall.get(resourcepath); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 101435d..00d3196 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,15 +1,15 @@ name: typesense description: Dart client library for accessing the HTTP API of Typesense search engine. -version: 0.3.1 +version: 0.4.0 homepage: https://typesense.org/ repository: https://github.com/typesense/typesense-dart environment: - sdk: ">=2.17.0 <3.0.0" + sdk: ^3.0.0 dependencies: - http: ^0.13.4 - crypto: ^3.0.2 + http: ^1.1.0 + crypto: ^3.0.3 dev_dependencies: test: ^1.21.4