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.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +2 -2
- data/.rubocop.yml +9 -1
- data/Gemfile +3 -7
- data/README.md +42 -0
- data/docker-compose.yml +1 -1
- data/flake.lock +61 -0
- data/flake.nix +56 -0
- data/lib/typesense/alias.rb +12 -0
- data/lib/typesense/aliases.rb +20 -0
- data/lib/typesense/analytics.rb +23 -0
- data/lib/typesense/analytics_events.rb +31 -0
- data/lib/typesense/analytics_events_v1.rb +27 -0
- data/lib/typesense/analytics_rule.rb +23 -0
- data/lib/typesense/analytics_rule_v1.rb +36 -0
- data/lib/typesense/analytics_rules.rb +37 -12
- data/lib/typesense/analytics_rules_v1.rb +50 -0
- data/lib/typesense/analytics_v1.rb +39 -0
- data/lib/typesense/api_call.rb +72 -5
- data/lib/typesense/client.rb +182 -2
- data/lib/typesense/collection.rb +56 -1
- data/lib/typesense/collections.rb +20 -0
- data/lib/typesense/configuration.rb +18 -4
- data/lib/typesense/curation_set.rb +60 -0
- data/lib/typesense/curation_set_item.rb +47 -0
- data/lib/typesense/curation_set_items.rb +39 -0
- data/lib/typesense/curation_sets.rb +43 -0
- data/lib/typesense/debug.rb +6 -0
- data/lib/typesense/document.rb +18 -0
- data/lib/typesense/documents.rb +62 -0
- data/lib/typesense/health.rb +6 -0
- data/lib/typesense/key.rb +12 -0
- data/lib/typesense/keys.rb +26 -0
- data/lib/typesense/metrics.rb +6 -0
- data/lib/typesense/multi_search.rb +8 -0
- data/lib/typesense/nl_search_model.rb +18 -0
- data/lib/typesense/nl_search_models.rb +20 -0
- data/lib/typesense/operations.rb +6 -0
- data/lib/typesense/override.rb +12 -0
- data/lib/typesense/overrides.rb +20 -0
- data/lib/typesense/preset.rb +12 -0
- data/lib/typesense/presets.rb +20 -0
- data/lib/typesense/stats.rb +6 -0
- data/lib/typesense/stemming.rb +11 -0
- data/lib/typesense/stemming_dictionaries.rb +20 -0
- data/lib/typesense/stemming_dictionary.rb +6 -0
- data/lib/typesense/synonym.rb +13 -0
- data/lib/typesense/synonym_set.rb +60 -0
- data/lib/typesense/synonym_set_item.rb +47 -0
- data/lib/typesense/synonym_set_items.rb +39 -0
- data/lib/typesense/synonym_sets.rb +50 -0
- data/lib/typesense/synonyms.rb +21 -0
- data/lib/typesense/version.rb +1 -1
- data/lib/typesense.rb +13 -0
- data/typesense.gemspec +5 -5
- metadata +57 -11
data/lib/typesense/api_call.rb
CHANGED
|
@@ -19,9 +19,18 @@ module Typesense
|
|
|
19
19
|
@healthcheck_interval_seconds = @configuration.healthcheck_interval_seconds
|
|
20
20
|
@num_retries_per_request = @configuration.num_retries
|
|
21
21
|
@retry_interval_seconds = @configuration.retry_interval_seconds
|
|
22
|
+
@keep_alive_connections = @configuration.keep_alive_connections
|
|
23
|
+
@keep_alive_idle_timeout_seconds = @configuration.keep_alive_idle_timeout_seconds
|
|
24
|
+
@keep_alive_pool_size = @configuration.keep_alive_pool_size
|
|
22
25
|
|
|
23
26
|
@logger = @configuration.logger
|
|
24
27
|
|
|
28
|
+
# Per-instance key for the thread-local connection cache so multiple
|
|
29
|
+
# Typesense::Client instances in the same process do not share sockets.
|
|
30
|
+
@thread_connections_key = :"_typesense_api_call_connections_#{object_id}"
|
|
31
|
+
|
|
32
|
+
require 'faraday/net_http_persistent' if @keep_alive_connections
|
|
33
|
+
|
|
25
34
|
initialize_metadata_for_nodes
|
|
26
35
|
@current_node_index = -1
|
|
27
36
|
end
|
|
@@ -69,14 +78,14 @@ module Typesense
|
|
|
69
78
|
@logger.debug "Attempting #{method.to_s.upcase} request Try ##{num_tries} to Node #{node[:index]}"
|
|
70
79
|
|
|
71
80
|
begin
|
|
72
|
-
conn =
|
|
73
|
-
f.options.timeout = @connection_timeout_seconds
|
|
74
|
-
f.options.open_timeout = @connection_timeout_seconds
|
|
75
|
-
end
|
|
81
|
+
conn, request_url = connection_and_url_for(endpoint, node)
|
|
76
82
|
|
|
77
83
|
headers = default_headers.merge(additional_headers)
|
|
78
84
|
|
|
85
|
+
# The URL is set on the request instead of being passed to the verb. Faraday
|
|
86
|
+
# looks up proxy settings again for every absolute URL passed to the verb.
|
|
79
87
|
response = conn.send(method) do |req|
|
|
88
|
+
req.url(request_url) unless request_url.nil?
|
|
80
89
|
req.headers = headers
|
|
81
90
|
req.params = query_parameters unless query_parameters.nil?
|
|
82
91
|
unless body_parameters.nil?
|
|
@@ -108,6 +117,9 @@ module Typesense
|
|
|
108
117
|
# Rescue network layer exceptions and HTTP 5xx errors, so the loop can continue.
|
|
109
118
|
# Using loops for retries instead of rescue...retry to maintain consistency with client libraries in
|
|
110
119
|
# other languages that might not support the same construct.
|
|
120
|
+
# Drop the cached keep-alive connection (if any): the underlying socket is likely
|
|
121
|
+
# half-closed and reusing it would just fail again on retry.
|
|
122
|
+
discard_connection(node) if @keep_alive_connections
|
|
111
123
|
set_node_healthcheck(node, is_healthy: false)
|
|
112
124
|
last_exception = e
|
|
113
125
|
@logger.warn "Request #{method}:#{uri_for(endpoint, node)} to Node #{node[:index]} failed due to \"#{e.class}: #{e.message}\""
|
|
@@ -121,8 +133,63 @@ module Typesense
|
|
|
121
133
|
|
|
122
134
|
private
|
|
123
135
|
|
|
136
|
+
# Faraday 2.9.2 and later only treat a request URL as absolute when it starts
|
|
137
|
+
# with a lower-case "http://" or "https://".
|
|
124
138
|
def uri_for(endpoint, node)
|
|
125
|
-
"#{node[:protocol]}://#{node[:host]}:#{node[:port]}#{endpoint}"
|
|
139
|
+
"#{node[:protocol].to_s.downcase}://#{node[:host]}:#{node[:port]}#{endpoint}"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Returns [connection, request_url]. When keep-alive is enabled, the connection
|
|
143
|
+
# is cached per (thread, node) and each request gets the full URL. Faraday merges
|
|
144
|
+
# a relative path into the base URL, and that merge drops "." and ".." segments.
|
|
145
|
+
# A delete of document ".." would then delete its whole collection. When keep-alive
|
|
146
|
+
# is disabled, the original behaviour is preserved: a fresh Faraday is built for
|
|
147
|
+
# the full per-request URL, so existing callers and stubs see no change.
|
|
148
|
+
def connection_and_url_for(endpoint, node)
|
|
149
|
+
if @keep_alive_connections
|
|
150
|
+
[connection_for(node), uri_for(endpoint, node)]
|
|
151
|
+
else
|
|
152
|
+
[build_one_shot_connection(endpoint, node), nil]
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def build_one_shot_connection(endpoint, node)
|
|
157
|
+
Faraday.new(uri_for(endpoint, node)) do |f|
|
|
158
|
+
f.options.timeout = @connection_timeout_seconds
|
|
159
|
+
f.options.open_timeout = @connection_timeout_seconds
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Net::HTTP is not thread-safe, so connections are cached per (thread, node)
|
|
164
|
+
# rather than shared across threads.
|
|
165
|
+
def connection_for(node)
|
|
166
|
+
thread_connections[connection_key(node)] ||= build_keep_alive_connection(node)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def discard_connection(node)
|
|
170
|
+
conn = thread_connections.delete(connection_key(node))
|
|
171
|
+
conn&.close if conn.respond_to?(:close)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def thread_connections
|
|
175
|
+
Thread.current[@thread_connections_key] ||= {}
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def connection_key(node)
|
|
179
|
+
"#{node[:protocol]}://#{node[:host]}:#{node[:port]}"
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def build_keep_alive_connection(node)
|
|
183
|
+
Faraday.new(url: connection_key(node)) do |f|
|
|
184
|
+
f.options.timeout = @connection_timeout_seconds
|
|
185
|
+
f.options.open_timeout = @connection_timeout_seconds
|
|
186
|
+
# pool_size defaults to 1: we already cache one Faraday connection per
|
|
187
|
+
# (thread, node), and Net::HTTP is not thread-safe — so a single socket
|
|
188
|
+
# per pool is the safe default. Override only with a specific reason.
|
|
189
|
+
f.adapter :net_http_persistent, pool_size: @keep_alive_pool_size do |http|
|
|
190
|
+
http.idle_timeout = @keep_alive_idle_timeout_seconds
|
|
191
|
+
end
|
|
192
|
+
end
|
|
126
193
|
end
|
|
127
194
|
|
|
128
195
|
## Attempts to find the next healthy node, looping through the list of nodes once.
|
data/lib/typesense/client.rb
CHANGED
|
@@ -1,9 +1,186 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Typesense
|
|
4
|
+
# Typesense client for indexing, searching, and managing collections.
|
|
5
|
+
#
|
|
6
|
+
# @see https://typesense.org/docs/latest/api/
|
|
4
7
|
class Client
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
# @return [Configuration]
|
|
9
|
+
attr_reader :configuration
|
|
10
|
+
|
|
11
|
+
# Access the collections resource. Use it to list or create collections, or as an array to access a single collection by name.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# client.collections.create('name' => 'products', 'fields' => [{ 'name' => 'title', 'type' => 'string' }])
|
|
15
|
+
# @example
|
|
16
|
+
# client.collections['products'].retrieve
|
|
17
|
+
#
|
|
18
|
+
# @see https://typesense.org/docs/latest/api/collections.html
|
|
19
|
+
#
|
|
20
|
+
# @return [Collections]
|
|
21
|
+
attr_reader :collections
|
|
22
|
+
|
|
23
|
+
# Access the aliases resource. Use it to upsert or list aliases, or as an array to access a single alias by name.
|
|
24
|
+
#
|
|
25
|
+
# @example
|
|
26
|
+
# client.aliases.upsert('my-alias', 'collection_name' => 'products')
|
|
27
|
+
# @example
|
|
28
|
+
# client.aliases['my-alias'].retrieve
|
|
29
|
+
#
|
|
30
|
+
# @see https://typesense.org/docs/latest/api/collection-alias.html
|
|
31
|
+
#
|
|
32
|
+
# @return [Aliases]
|
|
33
|
+
attr_reader :aliases
|
|
34
|
+
|
|
35
|
+
# Access the API keys resource. Use it to create or list keys, or as an array to access a single key by ID.
|
|
36
|
+
#
|
|
37
|
+
# @example
|
|
38
|
+
# client.keys.create('description' => 'Search-only key', 'actions' => ['documents:search'], 'collections' => ['*'])
|
|
39
|
+
# @example
|
|
40
|
+
# client.keys[1].retrieve
|
|
41
|
+
#
|
|
42
|
+
# @see https://typesense.org/docs/latest/api/api-keys.html
|
|
43
|
+
#
|
|
44
|
+
# @return [Keys]
|
|
45
|
+
attr_reader :keys
|
|
46
|
+
|
|
47
|
+
# Retrieve server version and state information.
|
|
48
|
+
#
|
|
49
|
+
# @example
|
|
50
|
+
# client.debug.retrieve
|
|
51
|
+
#
|
|
52
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html#debug
|
|
53
|
+
#
|
|
54
|
+
# @return [Debug]
|
|
55
|
+
attr_reader :debug
|
|
56
|
+
|
|
57
|
+
# Checks if Typesense server is ready to accept requests.
|
|
58
|
+
#
|
|
59
|
+
# @example
|
|
60
|
+
# client.health.retrieve
|
|
61
|
+
#
|
|
62
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html#health
|
|
63
|
+
#
|
|
64
|
+
# @return [Health]
|
|
65
|
+
attr_reader :health
|
|
66
|
+
|
|
67
|
+
# Get current RAM, CPU, Disk & Network usage metrics.
|
|
68
|
+
#
|
|
69
|
+
# @example
|
|
70
|
+
# client.metrics.retrieve
|
|
71
|
+
#
|
|
72
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html
|
|
73
|
+
#
|
|
74
|
+
# @return [Metrics]
|
|
75
|
+
attr_reader :metrics
|
|
76
|
+
|
|
77
|
+
# Get stats about API endpoints.
|
|
78
|
+
#
|
|
79
|
+
# @example
|
|
80
|
+
# client.stats.retrieve
|
|
81
|
+
#
|
|
82
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html
|
|
83
|
+
#
|
|
84
|
+
# @return [Stats]
|
|
85
|
+
attr_reader :stats
|
|
86
|
+
|
|
87
|
+
# Cluster operations: snapshots, voting, cache, on-disk compaction, slow request log.
|
|
88
|
+
#
|
|
89
|
+
# @example
|
|
90
|
+
# client.operations.perform('snapshot', 'snapshot_path' => '/tmp/snap')
|
|
91
|
+
#
|
|
92
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html
|
|
93
|
+
#
|
|
94
|
+
# @return [Operations]
|
|
95
|
+
attr_reader :operations
|
|
96
|
+
|
|
97
|
+
# Send multiple search requests in a single HTTP request.
|
|
98
|
+
#
|
|
99
|
+
# @example
|
|
100
|
+
# client.multi_search.perform('searches' => [{ 'collection' => 'products', 'q' => '*' }])
|
|
101
|
+
#
|
|
102
|
+
# @see https://typesense.org/docs/latest/api/documents.html#federated-multi-search
|
|
103
|
+
#
|
|
104
|
+
# @return [MultiSearch]
|
|
105
|
+
attr_reader :multi_search
|
|
106
|
+
|
|
107
|
+
# Manage analytics rules and events.
|
|
108
|
+
#
|
|
109
|
+
# @example
|
|
110
|
+
# client.analytics.rules.retrieve
|
|
111
|
+
#
|
|
112
|
+
# @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
|
|
113
|
+
#
|
|
114
|
+
# @return [Analytics]
|
|
115
|
+
attr_reader :analytics
|
|
116
|
+
|
|
117
|
+
# Legacy v1 analytics API for rules and events.
|
|
118
|
+
#
|
|
119
|
+
# @example
|
|
120
|
+
# client.analytics_v1.rules.retrieve
|
|
121
|
+
#
|
|
122
|
+
# @see https://typesense.org/docs/latest/api/analytics-query-suggestions.html
|
|
123
|
+
#
|
|
124
|
+
# @return [AnalyticsV1]
|
|
125
|
+
attr_reader :analytics_v1
|
|
126
|
+
|
|
127
|
+
# Access the presets resource. Use it to upsert or list presets, or as an array to access a single preset by name.
|
|
128
|
+
#
|
|
129
|
+
# @example
|
|
130
|
+
# client.presets.upsert('listing_view', 'value' => { 'q' => '*' })
|
|
131
|
+
# @example
|
|
132
|
+
# client.presets['listing_view'].retrieve
|
|
133
|
+
#
|
|
134
|
+
# @see https://typesense.org/docs/latest/api/search.html#presets
|
|
135
|
+
#
|
|
136
|
+
# @return [Presets]
|
|
137
|
+
attr_reader :presets
|
|
138
|
+
|
|
139
|
+
# Manage stemming dictionaries.
|
|
140
|
+
#
|
|
141
|
+
# @example
|
|
142
|
+
# client.stemming.dictionaries.retrieve
|
|
143
|
+
#
|
|
144
|
+
# @see https://typesense.org/docs/latest/api/stemming.html
|
|
145
|
+
#
|
|
146
|
+
# @return [Stemming]
|
|
147
|
+
attr_reader :stemming
|
|
148
|
+
|
|
149
|
+
# Access the NL search models resource. Use it to create or list models, or as an array to access a single model by ID.
|
|
150
|
+
#
|
|
151
|
+
# @example
|
|
152
|
+
# client.nl_search_models.create('model_name' => 'openai/gpt-4')
|
|
153
|
+
# @example
|
|
154
|
+
# client.nl_search_models['model-1'].retrieve
|
|
155
|
+
#
|
|
156
|
+
# @see https://typesense.org/docs/latest/api/natural-language-search.html
|
|
157
|
+
#
|
|
158
|
+
# @return [NlSearchModels]
|
|
159
|
+
attr_reader :nl_search_models
|
|
160
|
+
|
|
161
|
+
# Access the synonym sets resource. Use it to upsert or list sets, or as an array to access a single set by name.
|
|
162
|
+
#
|
|
163
|
+
# @example
|
|
164
|
+
# client.synonym_sets.retrieve
|
|
165
|
+
# @example
|
|
166
|
+
# client.synonym_sets['my-set'].retrieve
|
|
167
|
+
#
|
|
168
|
+
# @see https://typesense.org/docs/latest/api/synonyms.html
|
|
169
|
+
#
|
|
170
|
+
# @return [SynonymSets]
|
|
171
|
+
attr_reader :synonym_sets
|
|
172
|
+
|
|
173
|
+
# Access the curation sets resource. Use it to upsert or list sets, or as an array to access a single set by name.
|
|
174
|
+
#
|
|
175
|
+
# @example
|
|
176
|
+
# client.curation_sets.retrieve
|
|
177
|
+
# @example
|
|
178
|
+
# client.curation_sets['my-set'].retrieve
|
|
179
|
+
#
|
|
180
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
181
|
+
#
|
|
182
|
+
# @return [CurationSets]
|
|
183
|
+
attr_reader :curation_sets
|
|
7
184
|
|
|
8
185
|
def initialize(options = {})
|
|
9
186
|
@configuration = Configuration.new(options)
|
|
@@ -18,9 +195,12 @@ module Typesense
|
|
|
18
195
|
@stats = Stats.new(@api_call)
|
|
19
196
|
@operations = Operations.new(@api_call)
|
|
20
197
|
@analytics = Analytics.new(@api_call)
|
|
198
|
+
@analytics_v1 = AnalyticsV1.new(@api_call)
|
|
21
199
|
@stemming = Stemming.new(@api_call)
|
|
22
200
|
@presets = Presets.new(@api_call)
|
|
23
201
|
@nl_search_models = NlSearchModels.new(@api_call)
|
|
202
|
+
@synonym_sets = SynonymSets.new(@api_call)
|
|
203
|
+
@curation_sets = CurationSets.new(@api_call)
|
|
24
204
|
end
|
|
25
205
|
end
|
|
26
206
|
end
|
data/lib/typesense/collection.rb
CHANGED
|
@@ -2,7 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
module Typesense
|
|
4
4
|
class Collection
|
|
5
|
-
|
|
5
|
+
# Access the documents resource for this collection. Use it to list, index, search, import, or export documents,
|
|
6
|
+
# or as an array to access a single document by ID.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# client.collections['products'].documents.create('id' => '1', 'title' => 'Hat')
|
|
10
|
+
# @example
|
|
11
|
+
# client.collections['products'].documents['1'].retrieve
|
|
12
|
+
#
|
|
13
|
+
# @see https://typesense.org/docs/latest/api/documents.html
|
|
14
|
+
#
|
|
15
|
+
# @return [Documents]
|
|
16
|
+
attr_reader :documents
|
|
17
|
+
|
|
18
|
+
# Access the legacy overrides (curation) resource for this collection. Use it to list or upsert overrides,
|
|
19
|
+
# or as an array to access a single override by ID.
|
|
20
|
+
#
|
|
21
|
+
# @example
|
|
22
|
+
# client.collections['products'].overrides.upsert('promote-hat', 'rule' => { 'query' => 'hat', 'match' => 'exact' }, 'includes' => [])
|
|
23
|
+
# @example
|
|
24
|
+
# client.collections['products'].overrides['promote-hat'].retrieve
|
|
25
|
+
#
|
|
26
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
27
|
+
#
|
|
28
|
+
# @return [Overrides]
|
|
29
|
+
attr_reader :overrides
|
|
30
|
+
|
|
31
|
+
# Access the legacy synonyms resource for this collection. Use it to list or upsert synonyms,
|
|
32
|
+
# or as an array to access a single synonym by ID.
|
|
33
|
+
#
|
|
34
|
+
# @example
|
|
35
|
+
# client.collections['products'].synonyms.upsert('syn-1', 'synonyms' => ['nyc', 'new york'])
|
|
36
|
+
# @example
|
|
37
|
+
# client.collections['products'].synonyms['syn-1'].retrieve
|
|
38
|
+
#
|
|
39
|
+
# @see https://typesense.org/docs/latest/api/synonyms.html
|
|
40
|
+
#
|
|
41
|
+
# @return [Synonyms]
|
|
42
|
+
attr_reader :synonyms
|
|
6
43
|
|
|
7
44
|
def initialize(name, api_call)
|
|
8
45
|
@name = name
|
|
@@ -12,14 +49,32 @@ module Typesense
|
|
|
12
49
|
@synonyms = Synonyms.new(@name, @api_call)
|
|
13
50
|
end
|
|
14
51
|
|
|
52
|
+
# Retrieve the details of a collection, given its name.
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
55
|
+
# client.collections['products'].retrieve
|
|
56
|
+
#
|
|
57
|
+
# @see https://typesense.org/docs/latest/api/collections.html#retrieve-a-collection
|
|
15
58
|
def retrieve
|
|
16
59
|
@api_call.get(endpoint_path)
|
|
17
60
|
end
|
|
18
61
|
|
|
62
|
+
# Update a collection's schema to modify the fields and their types.
|
|
63
|
+
#
|
|
64
|
+
# @example
|
|
65
|
+
# client.collections['products'].update('fields' => [{ 'name' => 'tags', 'type' => 'string[]' }])
|
|
66
|
+
#
|
|
67
|
+
# @see https://typesense.org/docs/latest/api/collections.html#update-or-alter-a-collection
|
|
19
68
|
def update(update_schema)
|
|
20
69
|
@api_call.patch(endpoint_path, update_schema)
|
|
21
70
|
end
|
|
22
71
|
|
|
72
|
+
# Permanently drops a collection. This action cannot be undone. For large collections, this might have an impact on read latencies.
|
|
73
|
+
#
|
|
74
|
+
# @example
|
|
75
|
+
# client.collections['products'].delete
|
|
76
|
+
#
|
|
77
|
+
# @see https://typesense.org/docs/latest/api/collections.html#drop-a-collection
|
|
23
78
|
def delete
|
|
24
79
|
@api_call.delete(endpoint_path)
|
|
25
80
|
end
|
|
@@ -9,14 +9,34 @@ module Typesense
|
|
|
9
9
|
@collections = {}
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
# When a collection is created, we give it a name and describe the fields that will be indexed from the documents added to the collection.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# client.collections.create('name' => 'products', 'fields' => [{ 'name' => 'title', 'type' => 'string' }])
|
|
16
|
+
#
|
|
17
|
+
# @see https://typesense.org/docs/latest/api/collections.html#create-a-collection
|
|
12
18
|
def create(schema)
|
|
13
19
|
@api_call.post(RESOURCE_PATH, schema)
|
|
14
20
|
end
|
|
15
21
|
|
|
22
|
+
# Returns a summary of all your collections. The collections are returned sorted by creation date, with the most recent collections appearing first.
|
|
23
|
+
#
|
|
24
|
+
# @example
|
|
25
|
+
# client.collections.retrieve
|
|
26
|
+
#
|
|
27
|
+
# @see https://typesense.org/docs/latest/api/collections.html#list-all-collections
|
|
16
28
|
def retrieve(options = {})
|
|
17
29
|
@api_call.get(RESOURCE_PATH, options)
|
|
18
30
|
end
|
|
19
31
|
|
|
32
|
+
# Access an individual collection by name.
|
|
33
|
+
#
|
|
34
|
+
# @example
|
|
35
|
+
# client.collections['products'].retrieve
|
|
36
|
+
#
|
|
37
|
+
# @see https://typesense.org/docs/latest/api/collections.html
|
|
38
|
+
#
|
|
39
|
+
# @return [Collection]
|
|
20
40
|
def [](collection_name)
|
|
21
41
|
@collections[collection_name] ||= Collection.new(collection_name, @api_call)
|
|
22
42
|
end
|
|
@@ -4,7 +4,7 @@ require 'logger'
|
|
|
4
4
|
|
|
5
5
|
module Typesense
|
|
6
6
|
class Configuration
|
|
7
|
-
attr_accessor :nodes, :nearest_node, :connection_timeout_seconds, :healthcheck_interval_seconds, :num_retries, :retry_interval_seconds, :api_key, :logger, :log_level
|
|
7
|
+
attr_accessor :nodes, :nearest_node, :connection_timeout_seconds, :healthcheck_interval_seconds, :num_retries, :retry_interval_seconds, :api_key, :logger, :log_level, :keep_alive_connections, :keep_alive_idle_timeout_seconds, :keep_alive_pool_size
|
|
8
8
|
|
|
9
9
|
def initialize(options = {})
|
|
10
10
|
@nodes = options[:nodes] || []
|
|
@@ -14,16 +14,19 @@ module Typesense
|
|
|
14
14
|
@num_retries = options[:num_retries] || (@nodes.length + (@nearest_node.nil? ? 0 : 1)) || 3
|
|
15
15
|
@retry_interval_seconds = options[:retry_interval_seconds] || 0.1
|
|
16
16
|
@api_key = options[:api_key]
|
|
17
|
+
@keep_alive_connections = options.fetch(:keep_alive_connections, false)
|
|
18
|
+
@keep_alive_idle_timeout_seconds = options[:keep_alive_idle_timeout_seconds] || 30
|
|
19
|
+
@keep_alive_pool_size = options[:keep_alive_pool_size] || 1
|
|
17
20
|
|
|
18
21
|
@logger = options[:logger] || Logger.new($stdout)
|
|
19
22
|
@log_level = options[:log_level] || Logger::WARN
|
|
20
23
|
@logger.level = @log_level
|
|
21
24
|
|
|
22
25
|
show_deprecation_warnings(options)
|
|
23
|
-
validate!
|
|
26
|
+
validate!(options)
|
|
24
27
|
end
|
|
25
28
|
|
|
26
|
-
def validate!
|
|
29
|
+
def validate!(options = {})
|
|
27
30
|
if @nodes.nil? ||
|
|
28
31
|
@nodes.empty? ||
|
|
29
32
|
@nodes.any? { |node| node_missing_parameters?(node) }
|
|
@@ -31,12 +34,23 @@ module Typesense
|
|
|
31
34
|
end
|
|
32
35
|
|
|
33
36
|
raise Error::MissingConfiguration, 'Missing required configuration. Ensure that api_key is set.' if @api_key.nil?
|
|
37
|
+
|
|
38
|
+
validate_keep_alive_options!(options)
|
|
34
39
|
end
|
|
35
40
|
|
|
36
41
|
private
|
|
37
42
|
|
|
38
43
|
def node_missing_parameters?(node)
|
|
39
|
-
%i[protocol host port].any? { |attr| node
|
|
44
|
+
%i[protocol host port].any? { |attr| node[attr].nil? }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def validate_keep_alive_options!(options)
|
|
48
|
+
return if @keep_alive_connections
|
|
49
|
+
|
|
50
|
+
dependent_keys = %i[keep_alive_idle_timeout_seconds keep_alive_pool_size].select { |k| options.key?(k) }
|
|
51
|
+
return if dependent_keys.empty?
|
|
52
|
+
|
|
53
|
+
raise Error::MissingConfiguration, "#{dependent_keys.join(' and ')} require keep_alive_connections: true."
|
|
40
54
|
end
|
|
41
55
|
|
|
42
56
|
def show_deprecation_warnings(options)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Typesense
|
|
4
|
+
class CurationSet
|
|
5
|
+
# Access the items in this curation set. Use it to list items, or as an array
|
|
6
|
+
# to access a single item by ID.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# client.curation_sets['my-set'].items.retrieve
|
|
10
|
+
# @example
|
|
11
|
+
# client.curation_sets['my-set'].items['promote-hat'].retrieve
|
|
12
|
+
#
|
|
13
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
14
|
+
#
|
|
15
|
+
# @return [CurationSetItems]
|
|
16
|
+
attr_reader :items
|
|
17
|
+
|
|
18
|
+
def initialize(curation_set_name, api_call)
|
|
19
|
+
@curation_set_name = curation_set_name
|
|
20
|
+
@api_call = api_call
|
|
21
|
+
@items = CurationSetItems.new(@curation_set_name, @api_call)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Create or update a curation set with the given name
|
|
25
|
+
#
|
|
26
|
+
# @example
|
|
27
|
+
# client.curation_sets['my-set'].upsert('items' => [{ 'id' => 'promote-hat', 'rule' => { 'query' => 'hat', 'match' => 'exact' } }])
|
|
28
|
+
#
|
|
29
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
30
|
+
def upsert(curation_set_data)
|
|
31
|
+
@api_call.put(endpoint_path, curation_set_data)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Retrieve a specific curation set by its name
|
|
35
|
+
#
|
|
36
|
+
# @example
|
|
37
|
+
# client.curation_sets['my-set'].retrieve
|
|
38
|
+
#
|
|
39
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
40
|
+
def retrieve
|
|
41
|
+
@api_call.get(endpoint_path)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Delete a specific curation set by its name
|
|
45
|
+
#
|
|
46
|
+
# @example
|
|
47
|
+
# client.curation_sets['my-set'].delete
|
|
48
|
+
#
|
|
49
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
50
|
+
def delete
|
|
51
|
+
@api_call.delete(endpoint_path)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def endpoint_path
|
|
57
|
+
"#{CurationSets::RESOURCE_PATH}/#{URI.encode_www_form_component(@curation_set_name)}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Typesense
|
|
4
|
+
class CurationSetItem
|
|
5
|
+
def initialize(curation_set_name, item_id, api_call)
|
|
6
|
+
@curation_set_name = curation_set_name
|
|
7
|
+
@item_id = item_id
|
|
8
|
+
@api_call = api_call
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Retrieve a specific curation item by its id
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# client.curation_sets['my-set'].items['promote-hat'].retrieve
|
|
15
|
+
#
|
|
16
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
17
|
+
def retrieve
|
|
18
|
+
@api_call.get(endpoint_path)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Create or update a curation set item with the given id
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# client.curation_sets['my-set'].items['promote-hat'].upsert('rule' => { 'query' => 'hat', 'match' => 'exact' }, 'includes' => [])
|
|
25
|
+
#
|
|
26
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
27
|
+
def upsert(params)
|
|
28
|
+
@api_call.put(endpoint_path, params)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Delete a specific curation item by its id
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# client.curation_sets['my-set'].items['promote-hat'].delete
|
|
35
|
+
#
|
|
36
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
37
|
+
def delete
|
|
38
|
+
@api_call.delete(endpoint_path)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def endpoint_path
|
|
44
|
+
"#{CurationSets::RESOURCE_PATH}/#{URI.encode_www_form_component(@curation_set_name)}/items/#{URI.encode_www_form_component(@item_id)}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Typesense
|
|
4
|
+
class CurationSetItems
|
|
5
|
+
def initialize(curation_set_name, api_call)
|
|
6
|
+
@curation_set_name = curation_set_name
|
|
7
|
+
@api_call = api_call
|
|
8
|
+
@items = {}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Retrieve all curation items in a set
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# client.curation_sets['my-set'].items.retrieve
|
|
15
|
+
#
|
|
16
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
17
|
+
def retrieve
|
|
18
|
+
@api_call.get(endpoint_path)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Access an individual curation item by ID within this curation set.
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# client.curation_sets['my-set'].items['promote-hat'].retrieve
|
|
25
|
+
#
|
|
26
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
27
|
+
#
|
|
28
|
+
# @return [CurationSetItem]
|
|
29
|
+
def [](item_id)
|
|
30
|
+
@items[item_id] ||= CurationSetItem.new(@curation_set_name, item_id, @api_call)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def endpoint_path(operation = nil)
|
|
36
|
+
"#{CurationSets::RESOURCE_PATH}/#{URI.encode_www_form_component(@curation_set_name)}/items#{"/#{operation}" unless operation.nil?}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Typesense
|
|
4
|
+
class CurationSets
|
|
5
|
+
RESOURCE_PATH = '/curation_sets'
|
|
6
|
+
|
|
7
|
+
def initialize(api_call)
|
|
8
|
+
@api_call = api_call
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Create or update a curation set with the given name
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# client.curation_sets.upsert('my-set', 'items' => [{ 'id' => 'promote-hat', 'rule' => { 'query' => 'hat', 'match' => 'exact' } }])
|
|
15
|
+
#
|
|
16
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
17
|
+
def upsert(curation_set_name, curation_set_data)
|
|
18
|
+
@api_call.put("#{self.class::RESOURCE_PATH}/#{URI.encode_www_form_component(curation_set_name)}", curation_set_data)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Retrieve all curation sets
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# client.curation_sets.retrieve
|
|
25
|
+
#
|
|
26
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
27
|
+
def retrieve
|
|
28
|
+
@api_call.get(self.class::RESOURCE_PATH)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Access an individual curation set by name.
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# client.curation_sets['my-set'].retrieve
|
|
35
|
+
#
|
|
36
|
+
# @see https://typesense.org/docs/latest/api/curation.html
|
|
37
|
+
#
|
|
38
|
+
# @return [CurationSet]
|
|
39
|
+
def [](curation_set_name)
|
|
40
|
+
CurationSet.new(curation_set_name, @api_call)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
data/lib/typesense/debug.rb
CHANGED
|
@@ -8,6 +8,12 @@ module Typesense
|
|
|
8
8
|
@api_call = api_call
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
# Retrieve server version and state information.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# client.debug.retrieve
|
|
15
|
+
#
|
|
16
|
+
# @see https://typesense.org/docs/latest/api/cluster-operations.html#debug
|
|
11
17
|
def retrieve
|
|
12
18
|
@api_call.get(RESOURCE_PATH)
|
|
13
19
|
end
|