Class: Meilisearch::Client

Inherits:
HTTPRequest show all
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, options)

Constant Summary

Constants included from TenantToken

TenantToken::HEADER

Constants inherited from HTTPRequest

HTTPRequest::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from HTTPRequest

#headers, #options

Instance Method Summary collapse

Methods included from MultiSearch

#multi_search

Methods included from TenantToken

#generate_tenant_token

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.

Parameters:

  • batch_uid (String)

    the uid of the request batch

Returns:

  • (Hash{String => Object})

    a batch object, see above

See Also:



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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    task search options as snake cased symbols, see the API reference

Returns:

  • (Hash{String => Object})

    results of the batches search, see API reference

See Also:



451
452
453
# File 'lib/meilisearch/client.rb', line 451

def batches(options = {})
  http_get '/batches', options
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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    task search options as snake cased symbols, see the API reference

Returns:

  • (Hash{String => Object})

    a Meilisearch task that is canceling other tasks

See Also:



372
373
374
# File 'lib/meilisearch/client.rb', line 372

def cancel_tasks(options = {})
  task_endpoint.cancel_tasks(options)
end

#create_dumpModels::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({})

Parameters:

  • index_uid (String)

    the uid of the new index

  • options (Hash{Symbol => Object}, nil) (defaults to: {})

    snake_cased options of the endpoint

Returns:

Raises:

See Also:



90
91
92
93
94
95
96
# File 'lib/meilisearch/client.rb', line 90

def create_index(index_uid, options = {})
  body = Utils.transform_attributes(options.merge(uid: index_uid))

  response = http_post '/indexes', body

  Models::Task.new(response, task_endpoint)
end

#create_index!(index_uid, options = {}) ⇒ Object

Deprecated.

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, options = {})
  Utils.soft_deprecate(
    'Client#create_index!',
    "client.create_index('#{index_uid}').await"
  )

  create_index(index_uid, options).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.

Parameters:

  • key_options (Hash{Symbol => Object})

    the key options of which the required are

    • :actions Array of API actions allowed for key, [“*”] for all

    • :indexes Array of indexes key can act on, [“*”] for all

    • :expires_at expiration datetime in RFC 3339 format, nil if the key never expires

Returns:

See Also:



221
222
223
224
225
# File 'lib/meilisearch/client.rb', line 221

def create_key(key_options)
  body = Utils.transform_attributes(key_options)

  http_post '/keys', body
end

#create_snapshotModels::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.

Parameters:

  • index_uid (String)

    the uid of the index to be deleted

Returns:



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.

Parameters:

  • uid_or_key (String)

    either the uuidv4 that is the key’s uid or a hash of the uid and the master key that is the key’s key field

See Also:



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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    task search options as snake cased symbols, see the API reference

Returns:

  • (Hash{String => Object})

    a Meilisearch task that is canceling other tasks

See Also:



392
393
394
# File 'lib/meilisearch/client.rb', line 392

def delete_tasks(options = {})
  task_endpoint.delete_tasks(options)
end

#experimental_featuresObject

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

Parameters:

  • index_uid (String)

    uid of the index

See Also:



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

Parameters:

  • index_uid (String)

    uid of the index

See Also:



150
151
152
# File 'lib/meilisearch/client.rb', line 150

def fetch_raw_index(index_uid)
  index_object(index_uid).fetch_raw_info
end

#healthHash{String => Object}

Check health of Meilisearch instance.

Returns:

  • (Hash{String => Object})

    the health report from the Meilisearch instance

See Also:



286
287
288
# File 'lib/meilisearch/client.rb', line 286

def health
  http_get '/health'
end

#healthy?bool

Check if Meilisearch instance is healthy.

Returns:

  • (bool)

    whether or not the /health endpoint raises any errors

See Also:



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

Parameters:

  • index_uid (String)

    the uid of the index to get

Returns:

See Also:



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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    limit and offset options

Returns:

See Also:



63
64
65
66
67
68
69
70
71
# File 'lib/meilisearch/client.rb', line 63

def indexes(options = {})
  response = raw_indexes(options)

  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.

Parameters:

  • uid_or_key (String)

    either the uuidv4 that is the key’s uid or a hash of the uid and the master key that is the key’s key field

Returns:

See Also:



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.

Parameters:

  • limit (String, Integer, nil) (defaults to: nil)

    limit the number of returned keys

  • offset (String, Integer, nil) (defaults to: nil)

    skip the first offset keys, useful for paging.

Returns:

See Also:



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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    limit and offset options

Returns:

See Also:



29
30
31
32
33
# File 'lib/meilisearch/client.rb', line 29

def raw_indexes(options = {})
  body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(:limit, :offset))

  http_get('/indexes', body)
end

#statsHash{String => Object}

Get stats of all indexes in instance.

Returns:

See Also:



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'])

Parameters:

  • options (Array<Array(String, String)>)

    the indexes to swap

Returns:

Raises:

See Also:



49
50
51
52
53
54
# File 'lib/meilisearch/client.rb', line 49

def swap_indexes(*options)
  mapped_array = options.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.

Parameters:

  • task_uid (String)

    uid of the requested task

Returns:

  • (Hash{String => Object})

    a Meilisearch task object (see above)

See Also:



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.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    task search options as snake cased symbols, see the API reference

Returns:

  • (Hash{String => Object})

    results of the task search, see API reference

See Also:



408
409
410
# File 'lib/meilisearch/client.rb', line 408

def tasks(options = {})
  task_endpoint.task_list(options)
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.

Parameters:

  • key_options (Hash{Symbol => Object})

Returns:

See Also:



240
241
242
243
244
245
# File 'lib/meilisearch/client.rb', line 240

def update_key(uid_or_key, key_options)
  body = Utils.transform_attributes(key_options)
  body = body.slice('description', 'name')

  http_patch "/keys/#{uid_or_key}", body
end

#versionHash{String => String}

Check version of Meilisearch server

Returns:

  • (Hash{String => String})

    package version and last commit of Meilisearch server, see version object

See Also:



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.

See Also:



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