Class: Meilisearch::Client
- Inherits:
-
HTTPRequest
- Object
- HTTPRequest
- Meilisearch::Client
- Includes:
- MultiSearch, TenantToken
- Defined in:
- lib/meilisearch/client.rb
Overview
Manages a connection to a Meilisearch server.
client = Meilisearch::Client.new(MEILISEARCH_URL, MASTER_KEY, )
Constant Summary
Constants included from TenantToken
Constants inherited from HTTPRequest
Instance Attribute Summary
Attributes inherited from HTTPRequest
Instance Method Summary collapse
-
#batch(batch_uid) ⇒ Hash{String => Object}
Get a single Meilisearch task batch matching
batch_uid. -
#batches(options = {}) ⇒ Hash{String => Object}
Get Meilisearch task batches matching the filters.
-
#cancel_tasks(options = {}) ⇒ Hash{String => Object}
Cancel tasks matching the filter.
-
#create_dump ⇒ Models::Task
Create a database dump.
-
#create_index(index_uid, options = {}) ⇒ Models::Task
Create a new empty index.
-
#create_index!(index_uid, options = {}) ⇒ Object
deprecated
Deprecated.
use Models::Task#await on task returned from #create_index
client.create_index('foo').await -
#create_key(key_options) ⇒ Hash{String => Object}
Create a new API key.
-
#create_snapshot ⇒ Models::Task
Create a database snapshot.
-
#delete_index(index_uid) ⇒ Models::Task
Delete an index.
-
#delete_key(uid_or_key) ⇒ Object
Delete an API key.
-
#delete_tasks(options = {}) ⇒ Hash{String => Object}
Cancel tasks matching the filter.
-
#experimental_features ⇒ Object
EXPERIMENTAL FEATURES.
-
#fetch_index(index_uid) ⇒ Object
Shorthand for client.index(index_uid).fetch_info.
-
#fetch_raw_index(index_uid) ⇒ Object
Shorthand for client.index(index_uid).fetch_raw_info.
-
#health ⇒ Hash{String => Object}
Check health of Meilisearch instance.
-
#healthy? ⇒ bool
Check if Meilisearch instance is healthy.
-
#index(index_uid) ⇒ Index
Get index with given uid.
-
#indexes(options = {}) ⇒ Hash{String => Object}
Fetch indexes in instance.
-
#key(uid_or_key) ⇒ Hash{String => Object}
Get a specific API key.
-
#keys(limit: nil, offset: nil) ⇒ Hash{String => Object}
Get all API keys.
-
#raw_indexes(options = {}) ⇒ Hash{String => Object}
Fetch indexes in instance, returning the raw server response.
-
#stats ⇒ Hash{String => Object}
Get stats of all indexes in instance.
-
#swap_indexes(*options) ⇒ Models::Task
Swap two indexes.
-
#task(task_uid) ⇒ Hash{String => Object}
Get one task.
-
#tasks(options = {}) ⇒ Hash{String => Object}
Get Meilisearch tasks matching the filters.
- #update_experimental_features(expe_feat_changes) ⇒ Object
-
#update_key(uid_or_key, key_options) ⇒ Hash{String => Object}
Update an existing API key.
-
#version ⇒ Hash{String => String}
Check version of Meilisearch server.
-
#wait_for_task(task_uid, timeout_in_ms = Models::Task.default_timeout_ms, interval_in_ms = Models::Task.default_interval_ms) ⇒ Object
Wait for a task in a busy loop.
Methods included from MultiSearch
Methods included from TenantToken
Methods inherited from HTTPRequest
#http_delete, #http_get, #http_patch, #http_post, #http_put, #initialize
Constructor Details
This class inherits a constructor from Meilisearch::HTTPRequest
Instance Method Details
#batch(batch_uid) ⇒ Hash{String => Object}
Get a single Meilisearch task batch matching batch_uid.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks are run in batches.
464 465 466 |
# File 'lib/meilisearch/client.rb', line 464 def batch(batch_uid) http_get "/batches/#{batch_uid}" end |
#batches(options = {}) ⇒ Hash{String => Object}
Get Meilisearch task batches matching the filters.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks are run in batches.
451 452 453 |
# File 'lib/meilisearch/client.rb', line 451 def batches( = {}) http_get '/batches', end |
#cancel_tasks(options = {}) ⇒ Hash{String => Object}
Cancel tasks matching the filter.
This route is meant to be used with options, please see the API reference.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks report their progress and status.
Warning: This does not return instances of Models::Task. This is a raw call to the Meilisearch API and the return is not modified.
372 373 374 |
# File 'lib/meilisearch/client.rb', line 372 def cancel_tasks( = {}) task_endpoint.cancel_tasks() end |
#create_dump ⇒ Models::Task
Create a database dump.
Dumps are “blueprints” which can be used to restore your database. Restoring a dump requires reindexing all documents and is therefore inefficient.
Dumps are created by the Meilisearch server in the directory where the server is started under dumps/ by default.
327 328 329 330 |
# File 'lib/meilisearch/client.rb', line 327 def create_dump response = http_post '/dumps' Models::Task.new(response, task_endpoint) end |
#create_index(index_uid, options = {}) ⇒ Models::Task
Create a new empty index.
client.create_index('indexUID')
client.create_index('indexUID', primary_key: 'id')
Indexes are also created when accessed:
client.index('new_index').add_documents({})
90 91 92 93 94 95 96 |
# File 'lib/meilisearch/client.rb', line 90 def create_index(index_uid, = {}) body = Utils.transform_attributes(.merge(uid: index_uid)) response = http_post '/indexes', body Models::Task.new(response, task_endpoint) end |
#create_index!(index_uid, options = {}) ⇒ Object
use Models::Task#await on task returned from #create_index
client.create_index('foo').await
Synchronous version of #create_index.
Waits for the task to be achieved with a busy loop, be careful when using it.
106 107 108 109 110 111 112 113 |
# File 'lib/meilisearch/client.rb', line 106 def create_index!(index_uid, = {}) Utils.soft_deprecate( 'Client#create_index!', "client.create_index('#{index_uid}').await" ) create_index(index_uid, ).await end |
#create_key(key_options) ⇒ Hash{String => Object}
Create a new API key.
require 'date_core'
ten_days_later = (DateTime.now + 10).rfc3339
client.create_key(actions: ['*'], indexes: ['*'], expires_at: ten_days_later)
This and other key methods require that the Meilisearch instance have a master key set.
221 222 223 224 225 |
# File 'lib/meilisearch/client.rb', line 221 def create_key() body = Utils.transform_attributes() http_post '/keys', body end |
#create_snapshot ⇒ Models::Task
Create a database snapshot.
Snapshots are exact copies of the Meilisearch database. As such they are pre-indexed and restoring one is a very efficient operation.
Snapshots are not compatible between Meilisearch versions. Snapshot creation takes priority over other tasks.
Snapshots are created by the Meilisearch server in the directory where the server is started under snapshots/ by default.
352 353 354 |
# File 'lib/meilisearch/client.rb', line 352 def create_snapshot http_post '/snapshots' end |
#delete_index(index_uid) ⇒ Models::Task
Delete an index.
119 120 121 |
# File 'lib/meilisearch/client.rb', line 119 def delete_index(index_uid) index_object(index_uid).delete end |
#delete_key(uid_or_key) ⇒ Object
Delete an API key.
# obviously this example uid will not correspond to a key on your server
# please replace it with your own key's uid
uid = '6062abda-a5aa-4414-ac91-ecd7944c0f8d'
client.delete_key(uid)
This and other key methods require that the Meilisearch instance have a master key set.
265 266 267 |
# File 'lib/meilisearch/client.rb', line 265 def delete_key(uid_or_key) http_delete "/keys/#{uid_or_key}" end |
#delete_tasks(options = {}) ⇒ Hash{String => Object}
Cancel tasks matching the filter.
This route is meant to be used with options, please see the API reference.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks report their progress and status.
Warning: This does not return instances of Models::Task. This is a raw call to the Meilisearch API and the return is not modified.
Tasks are run in batches, see #batches.
392 393 394 |
# File 'lib/meilisearch/client.rb', line 392 def delete_tasks( = {}) task_endpoint.delete_tasks() end |
#experimental_features ⇒ Object
EXPERIMENTAL FEATURES
470 471 472 |
# File 'lib/meilisearch/client.rb', line 470 def experimental_features http_get '/experimental-features' end |
#fetch_index(index_uid) ⇒ Object
Shorthand for
client.index(index_uid).fetch_info
141 142 143 |
# File 'lib/meilisearch/client.rb', line 141 def fetch_index(index_uid) index_object(index_uid).fetch_info end |
#fetch_raw_index(index_uid) ⇒ Object
Shorthand for
client.index(index_uid).fetch_raw_info
150 151 152 |
# File 'lib/meilisearch/client.rb', line 150 def fetch_raw_index(index_uid) index_object(index_uid).fetch_raw_info end |
#health ⇒ Hash{String => Object}
Check health of Meilisearch instance.
286 287 288 |
# File 'lib/meilisearch/client.rb', line 286 def health http_get '/health' end |
#healthy? ⇒ bool
Check if Meilisearch instance is healthy.
275 276 277 278 279 280 |
# File 'lib/meilisearch/client.rb', line 275 def healthy? http_get '/health' true rescue StandardError false end |
#index(index_uid) ⇒ Index
Get index with given uid.
Indexes that don’t exist are lazily created by Meilisearch.
index = client.index('index_uid')
index.add_documents({}) # index is created here if it did not exist
132 133 134 |
# File 'lib/meilisearch/client.rb', line 132 def index(index_uid) index_object(index_uid) end |
#indexes(options = {}) ⇒ Hash{String => Object}
Fetch indexes in instance.
63 64 65 66 67 68 69 70 71 |
# File 'lib/meilisearch/client.rb', line 63 def indexes( = {}) response = raw_indexes() response['results'].map! do |index_hash| index_object(index_hash['uid'], index_hash['primaryKey']) end response end |
#key(uid_or_key) ⇒ Hash{String => Object}
Get a specific API key.
# obviously this example uid will not correspond to a key on your server
# please replace it with your own key's uid
uid = '6062abda-a5aa-4414-ac91-ecd7944c0f8d'
client.key(uid)
This and other key methods require that the Meilisearch instance have a master key set.
196 197 198 |
# File 'lib/meilisearch/client.rb', line 196 def key(uid_or_key) http_get "/keys/#{uid_or_key}" end |
#keys(limit: nil, offset: nil) ⇒ Hash{String => Object}
Get all API keys
This and other key methods require that the Meilisearch instance have a master key set.
170 171 172 173 174 |
# File 'lib/meilisearch/client.rb', line 170 def keys(limit: nil, offset: nil) body = { limit: limit, offset: offset }.compact http_get '/keys', body end |
#raw_indexes(options = {}) ⇒ Hash{String => Object}
Fetch indexes in instance, returning the raw server response.
Unless you have a good reason to, #indexes should be used instead.
29 30 31 32 33 |
# File 'lib/meilisearch/client.rb', line 29 def raw_indexes( = {}) body = Utils.transform_attributes(.transform_keys(&:to_sym).slice(:limit, :offset)) http_get('/indexes', body) end |
#stats ⇒ Hash{String => Object}
Get stats of all indexes in instance.
306 307 308 |
# File 'lib/meilisearch/client.rb', line 306 def stats http_get '/stats' end |
#swap_indexes(*options) ⇒ Models::Task
Swap two indexes.
Can be used as a convenient way to rebuild an index while keeping it operational.
client.index('a_swap').add_documents({})
client.swap_indexes(['a', 'a_swap'])
Multiple swaps may be done with one request:
client.swap_indexes(['a', 'a_swap'], ['b', 'b_swap'])
49 50 51 52 53 54 |
# File 'lib/meilisearch/client.rb', line 49 def swap_indexes(*) mapped_array = .map { |arr| { indexes: arr } } response = http_post '/swap-indexes', mapped_array Models::Task.new(response, task_endpoint) end |
#task(task_uid) ⇒ Hash{String => Object}
Get one task.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks report their progress and status.
Warning: This does not return instances of Models::Task. This is a raw call to the Meilisearch API and the return is not modified.
424 425 426 |
# File 'lib/meilisearch/client.rb', line 424 def task(task_uid) task_endpoint.task(task_uid) end |
#tasks(options = {}) ⇒ Hash{String => Object}
Get Meilisearch tasks matching the filters.
Operations in Meilisearch are done asynchronously using “tasks”. Tasks report their progress and status.
Warning: This does not return instances of Models::Task. This is a raw call to the Meilisearch API and the return is not modified.
408 409 410 |
# File 'lib/meilisearch/client.rb', line 408 def tasks( = {}) task_endpoint.task_list() end |
#update_experimental_features(expe_feat_changes) ⇒ Object
474 475 476 477 |
# File 'lib/meilisearch/client.rb', line 474 def update_experimental_features(expe_feat_changes) expe_feat_changes = Utils.transform_attributes(expe_feat_changes) http_patch '/experimental-features', expe_feat_changes end |
#update_key(uid_or_key, key_options) ⇒ Hash{String => Object}
Update an existing API key.
This and other key methods require that the Meilisearch instance have a master key set.
240 241 242 243 244 245 |
# File 'lib/meilisearch/client.rb', line 240 def update_key(uid_or_key, ) body = Utils.transform_attributes() body = body.slice('description', 'name') http_patch "/keys/#{uid_or_key}", body end |
#version ⇒ Hash{String => String}
Check version of Meilisearch server
297 298 299 |
# File 'lib/meilisearch/client.rb', line 297 def version http_get '/version' end |
#wait_for_task(task_uid, timeout_in_ms = Models::Task.default_timeout_ms, interval_in_ms = Models::Task.default_interval_ms) ⇒ Object
Wait for a task in a busy loop.
Try to avoid using it. Wrapper around Task#wait_for_task.
432 433 434 435 436 437 438 |
# File 'lib/meilisearch/client.rb', line 432 def wait_for_task( task_uid, timeout_in_ms = Models::Task.default_timeout_ms, interval_in_ms = Models::Task.default_interval_ms ) task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms) end |