typesense 4.1.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests.yml +2 -2
  3. data/.rubocop.yml +9 -1
  4. data/Gemfile +3 -7
  5. data/README.md +42 -0
  6. data/docker-compose.yml +1 -1
  7. data/flake.lock +61 -0
  8. data/flake.nix +56 -0
  9. data/lib/typesense/alias.rb +12 -0
  10. data/lib/typesense/aliases.rb +20 -0
  11. data/lib/typesense/analytics.rb +23 -0
  12. data/lib/typesense/analytics_events.rb +31 -0
  13. data/lib/typesense/analytics_events_v1.rb +27 -0
  14. data/lib/typesense/analytics_rule.rb +23 -0
  15. data/lib/typesense/analytics_rule_v1.rb +36 -0
  16. data/lib/typesense/analytics_rules.rb +37 -12
  17. data/lib/typesense/analytics_rules_v1.rb +50 -0
  18. data/lib/typesense/analytics_v1.rb +39 -0
  19. data/lib/typesense/api_call.rb +72 -5
  20. data/lib/typesense/client.rb +182 -2
  21. data/lib/typesense/collection.rb +56 -1
  22. data/lib/typesense/collections.rb +20 -0
  23. data/lib/typesense/configuration.rb +18 -4
  24. data/lib/typesense/curation_set.rb +60 -0
  25. data/lib/typesense/curation_set_item.rb +47 -0
  26. data/lib/typesense/curation_set_items.rb +39 -0
  27. data/lib/typesense/curation_sets.rb +43 -0
  28. data/lib/typesense/debug.rb +6 -0
  29. data/lib/typesense/document.rb +18 -0
  30. data/lib/typesense/documents.rb +62 -0
  31. data/lib/typesense/health.rb +6 -0
  32. data/lib/typesense/key.rb +12 -0
  33. data/lib/typesense/keys.rb +26 -0
  34. data/lib/typesense/metrics.rb +6 -0
  35. data/lib/typesense/multi_search.rb +8 -0
  36. data/lib/typesense/nl_search_model.rb +18 -0
  37. data/lib/typesense/nl_search_models.rb +20 -0
  38. data/lib/typesense/operations.rb +6 -0
  39. data/lib/typesense/override.rb +12 -0
  40. data/lib/typesense/overrides.rb +20 -0
  41. data/lib/typesense/preset.rb +12 -0
  42. data/lib/typesense/presets.rb +20 -0
  43. data/lib/typesense/stats.rb +6 -0
  44. data/lib/typesense/stemming.rb +11 -0
  45. data/lib/typesense/stemming_dictionaries.rb +20 -0
  46. data/lib/typesense/stemming_dictionary.rb +6 -0
  47. data/lib/typesense/synonym.rb +13 -0
  48. data/lib/typesense/synonym_set.rb +60 -0
  49. data/lib/typesense/synonym_set_item.rb +47 -0
  50. data/lib/typesense/synonym_set_items.rb +39 -0
  51. data/lib/typesense/synonym_sets.rb +50 -0
  52. data/lib/typesense/synonyms.rb +21 -0
  53. data/lib/typesense/version.rb +1 -1
  54. data/lib/typesense.rb +13 -0
  55. data/typesense.gemspec +5 -5
  56. metadata +57 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a062220aa04eb79fee094b740800c3d44a37e561719c1b116d9bea78aaab23f3
4
- data.tar.gz: 8ea4367d99be582cbb65e939444fdd1cf0ef51ebb824cf685b70248d67da2877
3
+ metadata.gz: ac5977e88e32a761db4a539399269a8408f1466c4cfb03be86c41709b1c1963a
4
+ data.tar.gz: 12fcd5d973d1f1b01c12b755ff0bbb8240c61074b9d7b629ab8b68e3f27da00e
5
5
  SHA512:
6
- metadata.gz: 564395bc7f23080983a6f263687594138917bd99c01cf42fe1fb1f1e998060d0eae721c391211ad43470b7b71e892b89b1e00ecbeb1f67410ae6a00f066b31de
7
- data.tar.gz: 8b2b7b00c3f011b5b3bd8e77eaed719fe0be05bb3243d6443ee80fbcee60a0cefb32fef47595728c5baaa09cbd11507c655200c94ef02ebc2a7ac25e224a44ca
6
+ metadata.gz: c45c98f88c8a751f7b75c36217c8c5c1246a0c4279a71d055f19b018545b73cb95a82f8970802659368f8c783dbaf55d62bd127a6da8501b4133c50b686e99d8
7
+ data.tar.gz: 4ee328734704c4dfaf6401954ac4ece90e72291f1995114739cdeadd7436befdc0692f33a32a5e8a6e3bc0210221ad7a2171122bbc3f5d9287babb2f042b8614
@@ -10,10 +10,10 @@ jobs:
10
10
  runs-on: ubuntu-latest
11
11
  strategy:
12
12
  matrix:
13
- ruby-version: ['3.0', '3.2', '3.3', '3.4']
13
+ ruby-version: ['3.0', '3.2', '3.3', '3.4', '4.0']
14
14
  services:
15
15
  typesense:
16
- image: typesense/typesense:29.0
16
+ image: typesense/typesense:30.2
17
17
  ports:
18
18
  - 8108:8108
19
19
  volumes:
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ plugins: rubocop-rspec
2
2
 
3
3
  AllCops:
4
4
  NewCops: enable
5
- TargetRubyVersion: 2.7
5
+ TargetRubyVersion: 3.0
6
6
 
7
7
  Style/Documentation:
8
8
  Enabled: false
@@ -47,5 +47,13 @@ RSpec/ExampleLength:
47
47
  RSpec/MultipleExpectations:
48
48
  Enabled: false
49
49
 
50
+ RSpec/Output:
51
+ Exclude:
52
+ - spec/spec_helper.rb
53
+
54
+ Naming/PredicateMethod:
55
+ Exclude:
56
+ - spec/spec_helper.rb
57
+
50
58
  Style/HashSyntax:
51
59
  Enabled: false # We still want to support older versions of Ruby
data/Gemfile CHANGED
@@ -6,17 +6,13 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  # Dev dependencies
8
8
  gem 'awesome_print', '~> 1.8'
9
- gem 'bundler', '~> 2.0'
10
- gem 'codecov', '~> 0.1'
11
- gem 'erb'
9
+ gem 'bundler', '>= 2.0'
12
10
  gem 'guard', '~> 2.16'
13
11
  gem 'guard-rubocop', '~> 1.3'
14
12
  gem 'rake', '~> 13.0'
15
13
  gem 'rspec', '~> 3.9'
16
- gem 'rspec_junit_formatter', '~> 0.4'
17
- gem 'rspec-legacy_formatters', '~> 1.0' # For codecov formatter
18
- gem 'rubocop', '1.72.2'
19
- gem 'rubocop-rspec', '~> 3.6', require: false
14
+ gem 'rubocop', '~> 1.90'
15
+ gem 'rubocop-rspec', '~> 3.9', require: false
20
16
  gem 'simplecov', '~> 0.18'
21
17
  gem 'timecop', '~> 0.9'
22
18
  gem 'webmock', '~> 3.8'
data/README.md CHANGED
@@ -29,10 +29,52 @@ Here are some examples with inline comments that walk you through how to use the
29
29
 
30
30
  Tests are also a good place to know how the the library works internally: [spec](spec)
31
31
 
32
+ ### Error handling
33
+
34
+ Error responses from Typesense raise a subclass of `Typesense::Error`. The exception's `message` is the `message` from the API response, and the underlying Faraday response is available as `e.data[:response]` if you need the HTTP status, headers, or raw body:
35
+
36
+ ```ruby
37
+ begin
38
+ client.collections['unknown'].retrieve
39
+ rescue Typesense::Error::ObjectNotFound => e
40
+ e.message # => "Not Found"
41
+ e.data[:response].status # => 404
42
+ e.data[:response].body # => raw response body
43
+ end
44
+ ```
45
+
46
+ Status codes map to these classes: `RequestMalformed` (400), `RequestUnauthorized` (401), `ObjectNotFound` (404), `ObjectAlreadyExists` (409), `ObjectUnprocessable` (422), `ServerError` (5xx), `HTTPStatus0Error` (status 0), and `HTTPError` (any other non-2xx status).
47
+
48
+ 5xx responses and network errors are retried first. Once retries run out, the last error is re-raised, so a network failure surfaces as the original Faraday or `Errno` exception rather than a `Typesense::Error`.
49
+
50
+ ### Keep-alive connections
51
+
52
+ By default, the client opens a fresh HTTP connection (and TLS handshake) for every request. For high-traffic applications this can dominate request latency. Setting `keep_alive_connections: true` enables persistent connections via the `:net_http_persistent` Faraday adapter:
53
+
54
+ ```ruby
55
+ Typesense::Client.new(
56
+ api_key: ENV['TYPESENSE_API_KEY'],
57
+ nodes: [{ host: 'localhost', port: 8108, protocol: 'https' }],
58
+ connection_timeout_seconds: 3,
59
+ num_retries: 1,
60
+ keep_alive_connections: true
61
+ )
62
+ ```
63
+
64
+ Notes:
65
+
66
+ - Connections are cached per `(thread, node)`. `Net::HTTP` is not thread-safe, so each thread maintains its own keep-alive socket to each Typesense node, and the existing node round-robin still works.
67
+ - A cached connection is dropped automatically when a network error occurs, so retries open a fresh socket. We recommend setting `num_retries` to at least `1` so the gem can recover from a server- or load-balancer-side idle timeout transparently.
68
+ - Idle sockets are closed after 30 seconds by default. Override with `keep_alive_idle_timeout_seconds` to match or stay under your load balancer's idle timeout.
69
+ - The underlying `net_http_persistent` adapter holds at most `keep_alive_pool_size` sockets per origin (default `1`, which matches the per-`(thread, node)` cache above). The default of `1` is the safe choice for the vast majority of users — because we already cache one Faraday connection per `(thread, node)` and `Net::HTTP` is not thread-safe, a single socket per pool is all the adapter needs. Only raise this if you have a specific reason to keep additional sockets warm per origin (e.g. a non-standard concurrency model layered on top of this client); a larger pool will not increase request throughput on its own.
70
+ - `keep_alive_idle_timeout_seconds` and `keep_alive_pool_size` are only valid when `keep_alive_connections: true`; setting them otherwise raises `Typesense::Error::MissingConfiguration`.
71
+ - The option defaults to `false`, so upgrading the gem does not change behaviour until you opt in.
72
+
32
73
  ## Compatibility
33
74
 
34
75
  | Typesense Server | typesense-ruby |
35
76
  |------------------|----------------|
77
+ | \>= v30.0 | \>= v5.0.0 |
36
78
  | \>= v28.0 | \>= v3.0.0 |
37
79
  | \>= v0.25.0 | \>= v1.0.0 |
38
80
  | \>= v0.23.0 | \>= v0.14.0 |
data/docker-compose.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  services:
2
2
  typesense:
3
- image: typesense/typesense:29.0
3
+ image: typesense/typesense:30.2
4
4
  ports:
5
5
  - "127.0.0.1:8108:8108"
6
6
  volumes:
data/flake.lock ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "nodes": {
3
+ "flake-utils": {
4
+ "inputs": {
5
+ "systems": "systems"
6
+ },
7
+ "locked": {
8
+ "lastModified": 1731533236,
9
+ "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
10
+ "owner": "numtide",
11
+ "repo": "flake-utils",
12
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
13
+ "type": "github"
14
+ },
15
+ "original": {
16
+ "owner": "numtide",
17
+ "repo": "flake-utils",
18
+ "type": "github"
19
+ }
20
+ },
21
+ "nixpkgs": {
22
+ "locked": {
23
+ "lastModified": 1779560665,
24
+ "narHash": "sha256-tpyBcxPpcQb8ukyNF7DoCwfSY3VPsxHoYwj00Cayv5o=",
25
+ "owner": "NixOS",
26
+ "repo": "nixpkgs",
27
+ "rev": "64c08a7ca051951c8eae34e3e3cb1e202fe36786",
28
+ "type": "github"
29
+ },
30
+ "original": {
31
+ "owner": "NixOS",
32
+ "ref": "nixos-unstable",
33
+ "repo": "nixpkgs",
34
+ "type": "github"
35
+ }
36
+ },
37
+ "root": {
38
+ "inputs": {
39
+ "flake-utils": "flake-utils",
40
+ "nixpkgs": "nixpkgs"
41
+ }
42
+ },
43
+ "systems": {
44
+ "locked": {
45
+ "lastModified": 1681028828,
46
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
47
+ "owner": "nix-systems",
48
+ "repo": "default",
49
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
50
+ "type": "github"
51
+ },
52
+ "original": {
53
+ "owner": "nix-systems",
54
+ "repo": "default",
55
+ "type": "github"
56
+ }
57
+ }
58
+ },
59
+ "root": "root",
60
+ "version": 7
61
+ }
data/flake.nix ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ description = "Nix development environment for the Typesense Ruby client";
3
+
4
+ inputs = {
5
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6
+ flake-utils.url = "github:numtide/flake-utils";
7
+ };
8
+
9
+ outputs = { self, nixpkgs, flake-utils }:
10
+ flake-utils.lib.eachDefaultSystem (system:
11
+ let
12
+ pkgs = import nixpkgs {
13
+ inherit system;
14
+ };
15
+
16
+ ruby = pkgs.ruby_3_4;
17
+ in
18
+ {
19
+ devShells.default = pkgs.mkShell {
20
+ packages = with pkgs; [
21
+ ruby
22
+ bundler
23
+ git
24
+ curl
25
+ pkg-config
26
+ openssl
27
+ libyaml
28
+ zlib
29
+ libffi
30
+ docker
31
+ docker-compose
32
+ ];
33
+
34
+ shellHook = ''
35
+ export BUNDLE_PATH="$PWD/.bundle"
36
+ export BUNDLE_BIN="$PWD/.bundle/bin"
37
+ export BUNDLE_APP_CONFIG="$PWD/.bundle/config"
38
+ export GEM_HOME="$PWD/.bundle/ruby"
39
+ export PATH="$BUNDLE_BIN:$GEM_HOME/bin:$PATH"
40
+
41
+ cat <<'EOF'
42
+ Typesense Ruby dev shell
43
+
44
+ First-time setup:
45
+ bundle install
46
+
47
+ Common commands:
48
+ bundle exec rubocop
49
+ bundle exec rspec --format documentation
50
+
51
+ Integration tests expect Docker to be running.
52
+ EOF
53
+ '';
54
+ };
55
+ });
56
+ }
@@ -7,10 +7,22 @@ module Typesense
7
7
  @api_call = api_call
8
8
  end
9
9
 
10
+ # Find out which collection an alias points to by fetching it
11
+ #
12
+ # @example
13
+ # client.aliases['my-alias'].retrieve
14
+ #
15
+ # @see https://typesense.org/docs/latest/api/collection-alias.html#retrieve-an-alias
10
16
  def retrieve
11
17
  @api_call.get(endpoint_path)
12
18
  end
13
19
 
20
+ # Delete an alias
21
+ #
22
+ # @example
23
+ # client.aliases['my-alias'].delete
24
+ #
25
+ # @see https://typesense.org/docs/latest/api/collection-alias.html#delete-an-alias
14
26
  def delete
15
27
  @api_call.delete(endpoint_path)
16
28
  end
@@ -9,14 +9,34 @@ module Typesense
9
9
  @aliases = {}
10
10
  end
11
11
 
12
+ # Create or update a collection alias.
13
+ #
14
+ # @example
15
+ # client.aliases.upsert('my-alias', 'collection_name' => 'products')
16
+ #
17
+ # @see https://typesense.org/docs/latest/api/collection-alias.html#create-or-update-an-alias
12
18
  def upsert(alias_name, mapping)
13
19
  @api_call.put(endpoint_path(alias_name), mapping)
14
20
  end
15
21
 
22
+ # List all aliases and the corresponding collections that they map to.
23
+ #
24
+ # @example
25
+ # client.aliases.retrieve
26
+ #
27
+ # @see https://typesense.org/docs/latest/api/collection-alias.html#list-all-aliases
16
28
  def retrieve
17
29
  @api_call.get(RESOURCE_PATH)
18
30
  end
19
31
 
32
+ # Access an individual collection alias by name.
33
+ #
34
+ # @example
35
+ # client.aliases['my-alias'].retrieve
36
+ #
37
+ # @see https://typesense.org/docs/latest/api/collection-alias.html
38
+ #
39
+ # @return [Alias]
20
40
  def [](alias_name)
21
41
  @aliases[alias_name] ||= Alias.new(alias_name, @api_call)
22
42
  end
@@ -8,8 +8,31 @@ module Typesense
8
8
  @api_call = api_call
9
9
  end
10
10
 
11
+ # Access the analytics rules resource. Use it to list or create rules, or as an array
12
+ # to access a single rule by ID.
13
+ #
14
+ # @example
15
+ # client.analytics.rules.retrieve
16
+ # @example
17
+ # client.analytics.rules['rule-1'].retrieve
18
+ #
19
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
20
+ #
21
+ # @return [AnalyticsRules]
11
22
  def rules
12
23
  @rules ||= AnalyticsRules.new(@api_call)
13
24
  end
25
+
26
+ # Access the analytics events resource to send analytics events.
27
+ #
28
+ # @example
29
+ # client.analytics.events.create('type' => 'click', 'name' => 'products_click', 'data' => {})
30
+ #
31
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
32
+ #
33
+ # @return [AnalyticsEvents]
34
+ def events
35
+ @events ||= AnalyticsEvents.new(@api_call)
36
+ end
14
37
  end
15
38
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Typesense
4
+ class AnalyticsEvents
5
+ RESOURCE_PATH = '/analytics/events'
6
+
7
+ def initialize(api_call)
8
+ @api_call = api_call
9
+ end
10
+
11
+ # Submit a single analytics event. The event must correspond to an existing analytics rule by name.
12
+ #
13
+ # @example
14
+ # client.analytics.events.create('type' => 'click', 'name' => 'products_click', 'data' => {})
15
+ #
16
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
17
+ def create(params)
18
+ @api_call.post(self.class::RESOURCE_PATH, params)
19
+ end
20
+
21
+ # Retrieve the most recent events for a user and rule.
22
+ #
23
+ # @example
24
+ # client.analytics.events.retrieve('user_id' => 'u1', 'name' => 'products_click', 'n' => 10)
25
+ #
26
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
27
+ def retrieve(params = {})
28
+ @api_call.get(self.class::RESOURCE_PATH, params)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Typesense
4
+ class AnalyticsEventsV1
5
+ RESOURCE_PATH = '/analytics/events'
6
+
7
+ def initialize(api_call)
8
+ @api_call = api_call
9
+ end
10
+
11
+ # Submit a single legacy v1 analytics event. The event must correspond to an existing analytics rule by name.
12
+ #
13
+ # @example
14
+ # client.analytics_v1.events.create('type' => 'click', 'name' => 'products_click', 'data' => {})
15
+ #
16
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
17
+ def create(params)
18
+ @api_call.post(endpoint_path, params)
19
+ end
20
+
21
+ private
22
+
23
+ def endpoint_path(operation = nil)
24
+ "#{AnalyticsEventsV1::RESOURCE_PATH}#{"/#{URI.encode_www_form_component(operation)}" unless operation.nil?}"
25
+ end
26
+ end
27
+ end
@@ -7,14 +7,37 @@ module Typesense
7
7
  @api_call = api_call
8
8
  end
9
9
 
10
+ # Retrieve the details of an analytics rule, given it's name
11
+ #
12
+ # @example
13
+ # client.analytics.rules['rule-1'].retrieve
14
+ #
15
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
10
16
  def retrieve
11
17
  @api_call.get(endpoint_path)
12
18
  end
13
19
 
20
+ # Permanently deletes an analytics rule, given it's name
21
+ #
22
+ # @example
23
+ # client.analytics.rules['rule-1'].delete
24
+ #
25
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
14
26
  def delete
15
27
  @api_call.delete(endpoint_path)
16
28
  end
17
29
 
30
+ # Create or update an analytics rule with the given name.
31
+ #
32
+ # @example
33
+ # client.analytics.rules['products_click_counter'].upsert('type' => 'counter', 'collection' => 'products', 'event_type' => 'click', 'params' => { 'counter_field' => 'popularity', 'weight' => 1 })
34
+ #
35
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
36
+ def upsert(params)
37
+ @api_call.put(endpoint_path, params)
38
+ end
39
+ alias update upsert
40
+
18
41
  private
19
42
 
20
43
  def endpoint_path
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Typesense
4
+ class AnalyticsRuleV1
5
+ def initialize(rule_name, api_call)
6
+ @rule_name = rule_name
7
+ @api_call = api_call
8
+ end
9
+
10
+ # Retrieve a legacy v1 analytics rule by name.
11
+ #
12
+ # @example
13
+ # client.analytics_v1.rules['rule-1'].retrieve
14
+ #
15
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
16
+ def retrieve
17
+ @api_call.get(endpoint_path)
18
+ end
19
+
20
+ # Delete a legacy v1 analytics rule by name.
21
+ #
22
+ # @example
23
+ # client.analytics_v1.rules['rule-1'].delete
24
+ #
25
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
26
+ def delete
27
+ @api_call.delete(endpoint_path)
28
+ end
29
+
30
+ private
31
+
32
+ def endpoint_path
33
+ "#{AnalyticsRulesV1::RESOURCE_PATH}/#{URI.encode_www_form_component(@rule_name)}"
34
+ end
35
+ end
36
+ end
@@ -5,26 +5,51 @@ module Typesense
5
5
  RESOURCE_PATH = '/analytics/rules'
6
6
 
7
7
  def initialize(api_call)
8
- @api_call = api_call
9
- @analytics_rules = {}
8
+ @api_call = api_call
10
9
  end
11
10
 
12
- def upsert(rule_name, params)
13
- @api_call.put(endpoint_path(rule_name), params)
11
+ # Create one or more analytics rules. You can send a single rule object or an array of rule objects.
12
+ #
13
+ # @example
14
+ # client.analytics.rules.create('name' => 'products_query_hits', 'type' => 'popular_queries', 'params' => {})
15
+ #
16
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
17
+ def create(rules)
18
+ @api_call.post(self.class::RESOURCE_PATH, rules)
14
19
  end
15
20
 
16
- def retrieve
17
- @api_call.get(endpoint_path)
21
+ # Create or update an analytics rule with the given name.
22
+ #
23
+ # @example
24
+ # client.analytics.rules.upsert('products_click_counter', 'type' => 'counter', 'collection' => 'products', 'event_type' => 'click', 'params' => { 'counter_field' => 'popularity', 'weight' => 1 })
25
+ #
26
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
27
+ def upsert(rule_name, params)
28
+ @api_call.put("#{self.class::RESOURCE_PATH}/#{URI.encode_www_form_component(rule_name)}", params)
18
29
  end
19
30
 
20
- def [](rule_name)
21
- @analytics_rules[rule_name] ||= AnalyticsRule.new(rule_name, @api_call)
31
+ # Retrieve all analytics rules. Pass `rule_tag` to only retrieve the rules with that tag.
32
+ #
33
+ # @example
34
+ # client.analytics.rules.retrieve
35
+ # @example
36
+ # client.analytics.rules.retrieve(rule_tag: 'homepage')
37
+ #
38
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
39
+ def retrieve(params = {})
40
+ @api_call.get(self.class::RESOURCE_PATH, params)
22
41
  end
23
42
 
24
- private
25
-
26
- def endpoint_path(operation = nil)
27
- "#{AnalyticsRules::RESOURCE_PATH}#{"/#{URI.encode_www_form_component(operation)}" unless operation.nil?}"
43
+ # Access an individual analytics rule by name.
44
+ #
45
+ # @example
46
+ # client.analytics.rules['rule-1'].retrieve
47
+ #
48
+ # @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
49
+ #
50
+ # @return [AnalyticsRule]
51
+ def [](rule_name)
52
+ AnalyticsRule.new(rule_name, @api_call)
28
53
  end
29
54
  end
30
55
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Typesense
4
+ class AnalyticsRulesV1
5
+ RESOURCE_PATH = '/analytics/rules'
6
+
7
+ def initialize(api_call)
8
+ @api_call = api_call
9
+ @analytics_rules = {}
10
+ end
11
+
12
+ # Upsert a legacy v1 analytics rule by name.
13
+ #
14
+ # @example
15
+ # client.analytics_v1.rules.upsert('products_query_hits', 'type' => 'popular_queries', 'params' => {})
16
+ #
17
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
18
+ def upsert(rule_name, params)
19
+ @api_call.put(endpoint_path(rule_name), params)
20
+ end
21
+
22
+ # Retrieve all legacy v1 analytics rules.
23
+ #
24
+ # @example
25
+ # client.analytics_v1.rules.retrieve
26
+ #
27
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
28
+ def retrieve
29
+ @api_call.get(endpoint_path)
30
+ end
31
+
32
+ # Access an individual legacy v1 analytics rule by name.
33
+ #
34
+ # @example
35
+ # client.analytics_v1.rules['rule-1'].retrieve
36
+ #
37
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
38
+ #
39
+ # @return [AnalyticsRuleV1]
40
+ def [](rule_name)
41
+ @analytics_rules[rule_name] ||= AnalyticsRuleV1.new(rule_name, @api_call)
42
+ end
43
+
44
+ private
45
+
46
+ def endpoint_path(operation = nil)
47
+ "#{AnalyticsRulesV1::RESOURCE_PATH}#{"/#{URI.encode_www_form_component(operation)}" unless operation.nil?}"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Typesense
4
+ # @deprecated Deprecated starting with Typesense Server v30. Please migrate to `client.analytics` (new Analytics APIs).
5
+ class AnalyticsV1
6
+ RESOURCE_PATH = '/analytics'
7
+
8
+ def initialize(api_call)
9
+ @api_call = api_call
10
+ end
11
+
12
+ # Access the legacy v1 analytics rules resource. Use it to list or upsert rules, or as an array
13
+ # to access a single rule by ID.
14
+ #
15
+ # @example
16
+ # client.analytics_v1.rules.retrieve
17
+ # @example
18
+ # client.analytics_v1.rules['rule-1'].retrieve
19
+ #
20
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
21
+ #
22
+ # @return [AnalyticsRulesV1]
23
+ def rules
24
+ @rules ||= AnalyticsRulesV1.new(@api_call)
25
+ end
26
+
27
+ # Access the legacy v1 analytics events resource to send analytics events.
28
+ #
29
+ # @example
30
+ # client.analytics_v1.events.create('type' => 'click', 'name' => 'products_click', 'data' => {})
31
+ #
32
+ # @see https://typesense.org/docs/29.0/api/analytics-query-suggestions.html
33
+ #
34
+ # @return [AnalyticsEventsV1]
35
+ def events
36
+ @events ||= AnalyticsEventsV1.new(@api_call)
37
+ end
38
+ end
39
+ end