Class: Algolia::Search::Client

Inherits:
Object
  • Object
show all
Includes:
CallType, Helpers
Defined in:
lib/algolia/search_client.rb

Overview

Class Client

Constant Summary

Constants included from CallType

CallType::READ, CallType::WRITE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#check_array, #check_object, #chunk, #deserialize_settings, #get_object_id, #get_option, #handle_params, #hash_includes_subset?, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json, #to_query_string

Constructor Details

#initialize(search_config, opts = {}) ⇒ Client

Initialize a client to connect to Algolia

Parameters:

  • search_config (Search::Config)

    a Search::Config object which contains your APP_ID and API_KEY

  • adapter (Hash)

    a customizable set of options

  • logger (Hash)

    a customizable set of options

  • http_requester (Hash)

    a customizable set of options



19
20
21
22
23
24
25
# File 'lib/algolia/search_client.rb', line 19

def initialize(search_config, opts = {})
  @config      = search_config
  adapter      = opts[:adapter] || Defaults::ADAPTER
  @logger      = opts[:logger] || LoggerHelper.create
  requester    = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, @logger)
  @transporter = Transport::Transport.new(@config, requester)
end

Class Method Details

.create(app_id, api_key) ⇒ Object

Create a new client providing only app ID and API key

Parameters:

  • app_id (String)

    Algolia application ID

  • api_key (String)

    Algolia API key

Returns:

  • self



34
35
36
37
# File 'lib/algolia/search_client.rb', line 34

def self.create(app_id, api_key)
  config = Search::Config.new(application_id: app_id, api_key: api_key)
  create_with_config(config)
end

.create_with_config(config) ⇒ Object

Create a new client providing only the search config

Parameters:

Returns:

  • self



45
46
47
# File 'lib/algolia/search_client.rb', line 45

def self.create_with_config(config)
  new(config)
end

.generate_secured_api_key(parent_key, restrictions) ⇒ String

Generate a secured API key from the given parent key with the given restrictions

Parameters:

  • parent_key (String)

    Parent API key used the generate the secured key

  • restrictions (Hash)

    Restrictions to apply on the secured key

Returns:

  • (String)


405
406
407
408
409
# File 'lib/algolia/search_client.rb', line 405

def self.generate_secured_api_key(parent_key, restrictions)
  url_encoded_restrictions = to_query_string(symbolize_hash(restrictions))
  hmac                     = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), parent_key, url_encoded_restrictions)
  Base64.encode64("#{hmac}#{url_encoded_restrictions}").gsub("\n", '')
end

.get_secured_api_key_remaining_validity(secured_api_key) ⇒ Integer

Returns the time the given securedAPIKey remains valid in seconds

Parameters:

  • secured_api_key (String)

Returns:

  • (Integer)


417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/algolia/search_client.rb', line 417

def self.get_secured_api_key_remaining_validity(secured_api_key)
  now         = Time.now.to_i
  decoded_key = Base64.decode64(secured_api_key)
  regex       = 'validUntil=(\d+)'
  matches     = decoded_key.match(regex)

  if matches.nil?
    raise AlgoliaError, 'The SecuredAPIKey doesn\'t have a validUntil parameter.'
  end

  valid_until = matches[1].to_i

  valid_until - now
end

Instance Method Details

#add_api_key(acl, opts = {}) ⇒ AddApiKeyResponse

Add an API key with the given ACL

Parameters:

  • acl (Array)

    API key to retrieve

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query used for the key

Returns:



289
290
291
292
293
# File 'lib/algolia/search_client.rb', line 289

def add_api_key(acl, opts = {})
  response = @transporter.write(:POST, '/1/keys', { acl: acl }, opts)

  AddApiKeyResponse.new(self, response)
end

#add_api_key!(acl, opts = {}) ⇒ AddApiKeyResponse

Add an API key with the given ACL and wait for the task to complete

Parameters:

  • acl (Array)

    API key to retrieve

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query used for the key

Returns:



302
303
304
305
306
# File 'lib/algolia/search_client.rb', line 302

def add_api_key!(acl, opts = {})
  response = add_api_key(acl, opts)

  response.wait(opts)
end

#assign_user_id(user_id, cluster_name, opts = {}) ⇒ Hash

Assign or Move a userID to a cluster.

Parameters:

  • user_id (String)
  • cluster_name (String)

Returns:

  • (Hash)


499
500
501
502
503
504
# File 'lib/algolia/search_client.rb', line 499

def assign_user_id(user_id, cluster_name, opts = {})
  request_options           = symbolize_hash(opts)
  request_options[:headers] = { 'X-Algolia-User-ID': user_id }

  @transporter.write(:POST, '/1/clusters/mapping', { cluster: cluster_name }, request_options)
end

#assign_user_ids(user_ids, cluster_name, opts = {}) ⇒ Hash

Assign multiple userIDs to a cluster.

Parameters:

  • user_ids (Array)
  • cluster_name (String)

Returns:

  • (Hash)


513
514
515
# File 'lib/algolia/search_client.rb', line 513

def assign_user_ids(user_ids, cluster_name, opts = {})
  @transporter.write(:POST, '/1/clusters/mapping/batch', { cluster: cluster_name, users: user_ids }, opts)
end

#clear_dictionary_entries(dictionary, opts = {}) ⇒ Object

Clear all entries for a given dictionary

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



712
713
714
# File 'lib/algolia/search_client.rb', line 712

def clear_dictionary_entries(dictionary, opts = {})
  replace_dictionary_entries(dictionary, [], opts)
end

#clear_dictionary_entries!(dictionary, opts = {}) ⇒ Object

Clear all entries for a given dictionary and wait for the task to finish

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



721
722
723
724
725
# File 'lib/algolia/search_client.rb', line 721

def clear_dictionary_entries!(dictionary, opts = {})
  response = replace_dictionary_entries(dictionary, [], opts)

  response.wait(opts)
end

#copy_index(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the source index to the destination index

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



216
217
218
219
220
# File 'lib/algolia/search_client.rb', line 216

def copy_index(src_index_name, dest_index_name, opts = {})
  response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', src_index_name), { operation: 'copy', destination: dest_index_name }, opts)

  IndexingResponse.new(init_index(src_index_name), response)
end

#copy_index!(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the source index to the destination index and wait for the task to complete

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



230
231
232
233
234
# File 'lib/algolia/search_client.rb', line 230

def copy_index!(src_index_name, dest_index_name, opts = {})
  response     = copy_index(src_index_name, dest_index_name, opts)

  response.wait(opts)
end

#copy_rules(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the rules from source index to destination index

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



132
133
134
135
136
# File 'lib/algolia/search_client.rb', line 132

def copy_rules(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['rules']
  copy_index(src_index_name, dest_index_name, request_options)
end

#copy_rules!(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the rules from source index to destination index and wait for the task to complete

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



146
147
148
149
150
# File 'lib/algolia/search_client.rb', line 146

def copy_rules!(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['rules']
  copy_index!(src_index_name, dest_index_name, request_options)
end

#copy_settings(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the settings from source index to destination index

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



160
161
162
163
164
# File 'lib/algolia/search_client.rb', line 160

def copy_settings(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['settings']
  copy_index(src_index_name, dest_index_name, request_options)
end

#copy_settings!(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the settings from source index to destination index and wait for the task to complete

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



174
175
176
177
178
# File 'lib/algolia/search_client.rb', line 174

def copy_settings!(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['settings']
  copy_index!(src_index_name, dest_index_name, request_options)
end

#copy_synonyms(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the synonyms from source index to destination index

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



188
189
190
191
192
# File 'lib/algolia/search_client.rb', line 188

def copy_synonyms(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['synonyms']
  copy_index(src_index_name, dest_index_name, request_options)
end

#copy_synonyms!(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Copy the synonyms from source index to destination index and wait for the task to complete

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



202
203
204
205
206
# File 'lib/algolia/search_client.rb', line 202

def copy_synonyms!(src_index_name, dest_index_name, opts = {})
  request_options         = symbolize_hash(opts)
  request_options[:scope] = ['synonyms']
  copy_index!(src_index_name, dest_index_name, request_options)
end

#custom_request(data, uri, method, call_type, opts = {}) ⇒ Object

Method available to make custom requests to the API



782
783
784
785
786
787
788
# File 'lib/algolia/search_client.rb', line 782

def custom_request(data, uri, method, call_type, opts = {})
  if call_type == WRITE
    @transporter.write(method.to_sym, uri, data, opts)
  elsif call_type == READ
    @transporter.read(method.to_sym, uri, data, opts)
  end
end

#delete_api_key(key, opts = {}) ⇒ DeleteApiKeyResponse

Delete the given API key

Parameters:

  • key (String)

    API key to delete

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



343
344
345
346
347
# File 'lib/algolia/search_client.rb', line 343

def delete_api_key(key, opts = {})
  response = @transporter.write(:DELETE, path_encode('/1/keys/%s', key), {}, opts)

  DeleteApiKeyResponse.new(self, response, key)
end

#delete_api_key!(key, opts = {}) ⇒ DeleteApiKeyResponse

Delete the given API key and wait for the task to complete

Parameters:

  • key (String)

    API key to delete

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



356
357
358
359
360
# File 'lib/algolia/search_client.rb', line 356

def delete_api_key!(key, opts = {})
  response = delete_api_key(key, opts)

  response.wait(opts)
end

#delete_dictionary_entries(dictionary, object_ids, opts = {}) ⇒ Object

Delete entries for a given dictionary

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • object_ids (Array<Hash>)

    array of object ids

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/algolia/search_client.rb', line 679

def delete_dictionary_entries(dictionary, object_ids, opts = {})
  request  = object_ids.map do |object_id|
    { objectID: object_id }
  end
  response = @transporter.write(
    :POST,
    path_encode('/1/dictionaries/%s/batch', dictionary),
    { clearExistingDictionaryEntries: false, requests: chunk('deleteEntry', request) },
    opts
  )

  DictionaryResponse.new(self, response)
end

#delete_dictionary_entries!(dictionary, object_ids, opts = {}) ⇒ Object

Delete entries for a given dictionary and wait for the task to finish

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • object_ids (Array<Hash>)

    array of object ids

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



699
700
701
702
703
# File 'lib/algolia/search_client.rb', line 699

def delete_dictionary_entries!(dictionary, object_ids, opts = {})
  response = delete_dictionary_entries(dictionary, object_ids, opts)

  response.wait(opts)
end

#get_api_key(key_id, opts = {}) ⇒ Hash

Get the designated API key

Parameters:

  • key_id (String)

    API key to retrieve

Returns:

  • (Hash)


278
279
280
# File 'lib/algolia/search_client.rb', line 278

def get_api_key(key_id, opts = {})
  @transporter.read(:GET, path_encode('/1/keys/%s', key_id), {}, opts)
end

#get_dictionary_settings(opts = {}) ⇒ Object

Retrieve settings for all the dictionaries

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



772
773
774
# File 'lib/algolia/search_client.rb', line 772

def get_dictionary_settings(opts = {})
  @transporter.read(:GET, '/1/dictionaries/*/settings', {}, opts)
end

#get_logs(opts = {}) ⇒ Hash

Retrieve the client logs

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


116
117
118
# File 'lib/algolia/search_client.rb', line 116

def get_logs(opts = {})
  @transporter.read(:GET, '/1/logs', {}, opts)
end

#get_task_status(index_name, task_id, opts = {}) ⇒ String

Check the status of a task on the server. All server task are asynchronous and you can check the status of a task with this method.

Parameters:

  • index_name (String)

    index used for the calls

  • task_id (Integer)

    the id of the task returned by server

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (String)


77
78
79
80
# File 'lib/algolia/search_client.rb', line 77

def get_task_status(index_name, task_id, opts = {})
  res = @transporter.read(:GET, path_encode('/1/indexes/%s/task/%s', index_name, task_id), {}, opts)
  get_option(res, 'status')
end

#get_top_user_ids(opts = {}) ⇒ Hash

Get the top 10 userIDs with the highest number of records per cluster.

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


523
524
525
# File 'lib/algolia/search_client.rb', line 523

def get_top_user_ids(opts = {})
  @transporter.read(:GET, '/1/clusters/mapping/top', {}, opts)
end

#get_user_id(user_id, opts = {}) ⇒ Hash

Returns the userID data stored in the mapping.

Parameters:

  • user_id (String)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


534
535
536
# File 'lib/algolia/search_client.rb', line 534

def get_user_id(user_id, opts = {})
  @transporter.read(:GET, path_encode('/1/clusters/mapping/%s', user_id), {}, opts)
end

#init_index(index_name) ⇒ Index

Initialize an index with a given name

Parameters:

  • index_name (String)

    name of the index to init

Returns:

  • (Index)

    new Index instance



92
93
94
95
96
97
98
# File 'lib/algolia/search_client.rb', line 92

def init_index(index_name)
  stripped_index_name = index_name.strip
  if stripped_index_name.empty?
    raise AlgoliaError, 'Please provide a valid index name'
  end
  Index.new(stripped_index_name, @transporter, @config, @logger)
end

#list_api_keys(opts = {}) ⇒ Hash

List all keys associated with the current client

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


394
395
396
# File 'lib/algolia/search_client.rb', line 394

def list_api_keys(opts = {})
  @transporter.read(:GET, '/1/keys', {}, opts)
end

#list_clusters(opts = {}) ⇒ Hash

List the clusters available in a multi-clusters setup for a single appID

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


544
545
546
# File 'lib/algolia/search_client.rb', line 544

def list_clusters(opts = {})
  @transporter.read(:GET, '/1/clusters', {}, opts)
end

#list_indexes(opts = {}) ⇒ Hash

List all indexes of the client

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


106
107
108
# File 'lib/algolia/search_client.rb', line 106

def list_indexes(opts = {})
  @transporter.read(:GET, '/1/indexes', {}, opts)
end

#list_user_ids(opts = {}) ⇒ Hash

List the userIDs assigned to a multi-clusters appID

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


554
555
556
# File 'lib/algolia/search_client.rb', line 554

def list_user_ids(opts = {})
  @transporter.read(:GET, '/1/clusters/mapping', {}, opts)
end

#move_index(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Move the source index to the destination index

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



248
249
250
251
252
# File 'lib/algolia/search_client.rb', line 248

def move_index(src_index_name, dest_index_name, opts = {})
  response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', src_index_name), { operation: 'move', destination: dest_index_name }, opts)

  IndexingResponse.new(init_index(src_index_name), response)
end

#move_index!(src_index_name, dest_index_name, opts = {}) ⇒ IndexingResponse

Move the source index to the destination index and wait for the task to complete

Parameters:

  • src_index_name (String)

    Name of the source index

  • dest_index_name (String)

    Name of the destination index

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



262
263
264
265
266
# File 'lib/algolia/search_client.rb', line 262

def move_index!(src_index_name, dest_index_name, opts = {})
  response = move_index(src_index_name, dest_index_name, opts)

  response.wait(opts)
end

#multiple_batch(operations, opts = {}) ⇒ MultipleIndexBatchIndexingResponse

Batch multiple operations

Parameters:

  • operations (Array)

    array of operations (addObject, updateObject, …)

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



443
444
445
446
447
# File 'lib/algolia/search_client.rb', line 443

def multiple_batch(operations, opts = {})
  response = @transporter.write(:POST, '/1/indexes/*/batch', { requests: operations }, opts)

  MultipleIndexBatchIndexingResponse.new(self, response)
end

#multiple_batch!(operations, opts = {}) ⇒ MultipleIndexBatchIndexingResponse

Batch multiple operations and wait for the task to complete

Parameters:

  • operations (Array)

    array of operations (addObject, updateObject, …)

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



456
457
458
459
460
# File 'lib/algolia/search_client.rb', line 456

def multiple_batch!(operations, opts = {})
  response = multiple_batch(operations, opts)

  response.wait(opts)
end

#multiple_get_objects(requests, opts = {}) ⇒ Hash

Retrieve multiple objects in one batch request

Parameters:

  • requests (Array)

    array of requests

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


469
470
471
# File 'lib/algolia/search_client.rb', line 469

def multiple_get_objects(requests, opts = {})
  @transporter.read(:POST, '/1/indexes/*/objects', { requests: requests }, opts)
end

#multiple_queries(queries, opts = {}) ⇒ Hash Also known as: search

Search multiple indices

Parameters:

  • queries (Array)

    array of queries

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


480
481
482
483
484
485
# File 'lib/algolia/search_client.rb', line 480

def multiple_queries(queries, opts = {})
  queries.each do |q|
    q[:params] = to_query_string(q[:params]) unless q[:params].nil? || q[:params].is_a?(String)
  end
  @transporter.read(:POST, '/1/indexes/*/queries', { requests: queries }, opts)
end

#pending_mappings?(opts = {}) ⇒ Hash Also known as: has_pending_mappings

Get the status of your clusters’ migrations or user creations

Parameters:

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


589
590
591
592
593
594
595
596
597
598
599
# File 'lib/algolia/search_client.rb', line 589

def pending_mappings?(opts = {})
  retrieve_mappings = false

  request_options = symbolize_hash(opts)
  if request_options.has_key?(:retrieveMappings)
    retrieve_mappings = request_options[:retrieveMappings]
    request_options.delete(:retrieveMappings)
  end

  @transporter.read(:GET, '/1/clusters/mapping/pending' + handle_params({ getClusters: retrieve_mappings }), {}, request_options)
end

#remove_user_id(user_id, opts = {}) ⇒ Hash

Remove a userID and its associated data from the multi-clusters

Parameters:

  • user_id (String)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


565
566
567
568
569
570
# File 'lib/algolia/search_client.rb', line 565

def remove_user_id(user_id, opts = {})
  request_options           = symbolize_hash(opts)
  request_options[:headers] = { 'X-Algolia-User-ID': user_id }

  @transporter.write(:DELETE, '/1/clusters/mapping', {}, request_options)
end

#replace_dictionary_entries(dictionary, dictionary_entries, opts = {}) ⇒ Object

Replace entries for a given dictionary

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • dictionary_entries (Array<Hash>)

    array of dictionary entries

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



648
649
650
651
652
653
654
655
656
657
# File 'lib/algolia/search_client.rb', line 648

def replace_dictionary_entries(dictionary, dictionary_entries, opts = {})
  response = @transporter.write(
    :POST,
    path_encode('/1/dictionaries/%s/batch', dictionary),
    { clearExistingDictionaryEntries: true, requests: chunk('addEntry', dictionary_entries) },
    opts
  )

  DictionaryResponse.new(self, response)
end

#replace_dictionary_entries!(dictionary, dictionary_entries, opts = {}) ⇒ Object

Replace entries for a given dictionary and wait for the task to finish

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • dictionary_entries (Array<Hash>)

    array of dictionary entries

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



665
666
667
668
669
# File 'lib/algolia/search_client.rb', line 665

def replace_dictionary_entries!(dictionary, dictionary_entries, opts = {})
  response = replace_dictionary_entries(dictionary, dictionary_entries, opts)

  response.wait(opts)
end

#restore_api_key(key, opts = {}) ⇒ RestoreApiKeyResponse

Restore the given API key

Parameters:

  • key (String)

    API key to restore

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



369
370
371
372
373
# File 'lib/algolia/search_client.rb', line 369

def restore_api_key(key, opts = {})
  @transporter.write(:POST, path_encode('/1/keys/%s/restore', key), {}, opts)

  RestoreApiKeyResponse.new(self, key)
end

#restore_api_key!(key, opts = {}) ⇒ RestoreApiKeyResponse

Restore the given API key and wait for the task to complete

Parameters:

  • key (String)

    API key to restore

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:



382
383
384
385
386
# File 'lib/algolia/search_client.rb', line 382

def restore_api_key!(key, opts = {})
  response = restore_api_key(key, opts)

  response.wait(opts)
end

#save_dictionary_entries(dictionary, dictionary_entries, opts = {}) ⇒ Object

Save entries for a given dictionary

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • dictionary_entries (Array<Hash>)

    array of dictionary entries

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



617
618
619
620
621
622
623
624
625
626
# File 'lib/algolia/search_client.rb', line 617

def save_dictionary_entries(dictionary, dictionary_entries, opts = {})
  response = @transporter.write(
    :POST,
    path_encode('/1/dictionaries/%s/batch', dictionary),
    { clearExistingDictionaryEntries: false, requests: chunk('addEntry', dictionary_entries) },
    opts
  )

  DictionaryResponse.new(self, response)
end

#save_dictionary_entries!(dictionary, dictionary_entries, opts = {}) ⇒ Object

Save entries for a given dictionary and wait for the task to finish

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • dictionary_entries (Array<Hash>)

    array of dictionary entries

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



634
635
636
637
638
# File 'lib/algolia/search_client.rb', line 634

def save_dictionary_entries!(dictionary, dictionary_entries, opts = {})
  response = save_dictionary_entries(dictionary, dictionary_entries, opts)

  response.wait(opts)
end

#search_dictionary_entries(dictionary, query, opts = {}) ⇒ Object

Search entries for a given dictionary

Parameters:

  • dictionary (String)

    dictionary name. Can be either ‘stopwords’, ‘plurals’ or ‘compounds’

  • query (String)

    query to send

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



733
734
735
736
737
738
739
740
# File 'lib/algolia/search_client.rb', line 733

def search_dictionary_entries(dictionary, query, opts = {})
  @transporter.read(
    :POST,
    path_encode('/1/dictionaries/%s/search', dictionary),
    { query: query },
    opts
  )
end

#search_user_ids(query, opts = {}) ⇒ Hash

Search for userIDs

Parameters:

  • query (String)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


579
580
581
# File 'lib/algolia/search_client.rb', line 579

def search_user_ids(query, opts = {})
  @transporter.read(:POST, '/1/clusters/mapping/search', { query: query }, opts)
end

#set_dictionary_settings(dictionary_settings, opts = {}) ⇒ Object

Set settings for all the dictionaries

Parameters:

  • dictionary_settings (Hash)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



749
750
751
752
753
# File 'lib/algolia/search_client.rb', line 749

def set_dictionary_settings(dictionary_settings, opts = {})
  response = @transporter.write(:PUT, '/1/dictionaries/*/settings', dictionary_settings, opts)

  DictionaryResponse.new(self, response)
end

#set_dictionary_settings!(dictionary_settings, opts = {}) ⇒ Object

Set settings for all the dictionaries and wait for the task to finish

Parameters:

  • dictionary_settings (Hash)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • DictionaryResponse



762
763
764
765
766
# File 'lib/algolia/search_client.rb', line 762

def set_dictionary_settings!(dictionary_settings, opts = {})
  response = set_dictionary_settings(dictionary_settings, opts)

  response.wait(opts)
end

#update_api_key(key, opts = {}) ⇒ UpdateApiKeyResponse

Update an API key with the optional parameters

Parameters:

  • key (String)

    API key to update

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query used to update the key

Returns:



315
316
317
318
319
320
321
# File 'lib/algolia/search_client.rb', line 315

def update_api_key(key, opts = {})
  request_options = symbolize_hash(opts)

  response = @transporter.write(:PUT, path_encode('/1/keys/%s', key), {}, request_options)

  UpdateApiKeyResponse.new(self, response, request_options)
end

#update_api_key!(key, opts = {}) ⇒ UpdateApiKeyResponse

Update an API key with the optional parameters and wait for the task to complete

Parameters:

  • key (String)

    API key to update

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query used to update the key

Returns:



330
331
332
333
334
# File 'lib/algolia/search_client.rb', line 330

def update_api_key!(key, opts = {})
  response = update_api_key(key, opts)

  response.wait(opts)
end

#wait_task(index_name, task_id, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {}) ⇒ Object

Fetch the task status until it returns as “published”, meaning the operation is done

Parameters:

  • index_name (String)
  • task_id (Integer)
  • time_before_retry (Integer) (defaults to: WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY)

    time before retrying the call, in ms

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • nil



58
59
60
61
62
63
64
65
66
# File 'lib/algolia/search_client.rb', line 58

def wait_task(index_name, task_id, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {})
  loop do
    status = get_task_status(index_name, task_id, opts)
    if status == 'published'
      return
    end
    sleep(time_before_retry.to_f / 1000)
  end
end