First, you'll have to include the new version in your Gemfile. To do so, change the following:
- gem 'algoliasearch'
+ gem 'algolia', git: 'https://github.com/algolia/algoliasearch-client-ruby.git', tag: 'v2.0.0-beta.1'
Then, you'll need to change your current require
statements:
- require 'algoliasearch'
+ require 'algolia'
All classes have been namespaced. The mostly used classes are now as follows:
Algolia::Client
->Algolia::Search::Client
Algolia::Index
->Algolia::Search::Index
Algolia::AccountClient
->Algolia::Account::Client
Algolia::Analytics
->Algolia::Analytics::Client
Algolia::Insights
->Algolia::Insights::Client
There's a slight change in how you initialize the client. The index initialization didn't change.
# Before
client = Algolia::Client.new(
application_id: 'APP_ID',
api_key: 'API_KEY'
)
index = client.init_index('index_name')
# After
client = Algolia::Search::Client.create('APP_ID', 'API_KEY')
index = client.init_index('index_name')
# or
search_config = Algolia::Search::Config.new(application_id: app_id, api_key: api_key)
client = Algolia::Search::Client.create_with_config(search_config)
index = client.init_index('index_name')
By default the keys of the response hashes are symbols. If you wish to change that for strings, use the following configuration
search_config = Algolia::Search::Config.new(application_id: app_id, api_key: api_key, symbolize_keys: false)
client = Algolia::Search::Client.create_with_config(search_config)
The search parameters and request options are still optional, but they are combined into a single hash instead of two. For example:
# Before
request_opts = { 'X-Algolia-UserToken': 'user123' }
search_params = { hitsPerPage: 50 }
index.search('query', search_params, request_opts)
# After
opts = {
headers: { 'X-Algolia-UserToken': 'user123' },
hitsPerPage: 50
}
index.search('query', opts)
The set_extra_header
method has been moved from the Client to the Algolia::BaseConfig
class. You have to define your extra headers on Client instantiation.
# Before
client.set_extra_header('admin', 'admin-key')
# After
# `Algolia::Search::Config` inherits from `Algolia::BaseConfig`
config = Algolia::Search::Config.new(app_id: 'APP_ID', api_key: 'API_KEY')
config.set_extra_header('admin', 'admin-key')
client = Algolia::Search::Client.create_with_config(config)
The strategy
parameter is no longer a string, but a key in the requestOptions
.
queries = [
{ indexName: 'index_name1', params: { query: 'query', hitsPerPage: 2 } },
{ indexName: 'index_name2', params: { query: 'another_query', hitsPerPage: 5 } }
]
# Before
client.multiple_queries(queries, 'stopIfEnoughMatches')
# After
client.multiple_queries(queries, { strategy: 'stopIfEnoughMatches' })
No change.
No change.
No change.
No change.
This method is moved to the Algolia::Search::Client
class.
# Before
secured_api_key = Algolia.generate_secured_api_key('api_key', {
validUntil: now - (10 * 60)
})
# After
secured_api_key = Algolia::Search::Client.generate_secured_api_key('api_key', {
validUntil: now - (10 * 60)
})
acl
is still the first parameter. The other parameters have been moved to the requestOptions
.
# Before
client.add_api_key({ acl: ['search'], description: 'A description', indexes: ['index']})
# After
client.add_api_key(['search'], {
description: 'A description',
indexes: ['index']
})
This method is moved to the Algolia::Search::Client
class.
# Before
Algolia.update_api_key('api_key', { maxHitsPerQuery: 42 })
# After
client.update_api_key('api_key', { maxHitsPerQuery: 42 })
No change.
No change.
No change.
No change.
This method is moved to the Algolia::Search::Client
class.
# Before
Algolia.get_secured_api_key_remaining_validity('api_key')
# After
Algolia::Search::Client.get_secured_api_key_remaining_validity('api_key')
No change.
No change.
Newly added method to add multiple userIDs to a cluster.
user_ids = ['1','2','3']
# Before
user_ids.each { |id| client.assign_user_id(id, 'my-cluster')}
# After
client.assign_user_ids(user_ids, 'my-cluster')
No change.
No change.
No change.
The page
and hitsPerPage
parameters are now part of the requestOptions
.
# Before
page = 0
hits_per_page = 20
client.list_user_ids(page, hits_per_page)
# After
client.list_user_ids({ hitPerPage: 20, page: 0 })
No change.
The clusterName
, page
and hitsPerPage
parameters are now part of the requestOptions
.
# Before
page = 0
hits_per_page = 12
client.search_user_ids('query', 'my-cluster', page, hits_per_page)
# After
client.search_user_ids('query', {clusterName: 'my-cluster', hitPerPage: 12, page: 0 })
New method to check the status of your clusters' migration or user creation.
client.pending_mappings?({ retrieveMappings: true })
The offset
, length
, and type
parameters are now part of the requestOptions
.
# Before
offset = 5
length = 100
puts client.get_logs(offset, length, 'all')
# After
client.get_logs({ offset: 5, length: 10, type: 'all' })
No change.
searchParameters
and requestOptions
are a single parameter now.
# Before
request_opts = { 'X-Algolia-UserToken': 'user123' }
search_params = { hitsPerPage: 50 }
index.search('query', search_params, request_opts)
# After
opts = {
headers: { 'X-Algolia-UserToken': 'user123' },
hitsPerPage: 50
}
index.search('query', opts)
searchParameters
and requestOptions
are a single parameter now.
# Before
request_opts = { 'X-Algolia-UserToken': 'user123' }
search_params = { hitsPerPage: 50 }
index.search_for_facet_values('category', 'phone', search_params, request_opts)
# After
opts = {
headers: { 'X-Algolia-UserToken': 'user123' },
hitsPerPage: 50
}
index.search_for_facet_values('category', 'phone', opts)
The method takes a lambda, proc or block as the first argument (anything that responds to call
), and the requestOptions
as the second
# Before
index.find_object({query: 'query', paginate: true}) { |hit| hit[:title].include?('algolia') }
# After
index.find_object(-> (hit) { hit[:title].include?('algolia') }, { query: 'query', paginate: true })
The classname has changed, not the method itself.
# Before
position = Algolia::Index.get_object_position(results, 'object')
# After
position = Algolia::Search::Index.get_object_position(results, 'object')
These methods have been removed in favor of save_object
and save_objects
.
No change.
The objectID
parameter is removed. create_if_not_exists
is now part of the requestOptions
parameter.
obj = { objectID: '1234', prop: 'value' }
# Before
create_if_not_exists = true
index.partial_update_object(obj, obj[:objectID], create_if_not_exists)
# After
index.partial_update_object(obj, { createIfNotExists: true })
The create_if_not_exists
parameter is now part of the requestOptions
parameter.
# Before
create_if_not_exists = true
index.partial_update_objects(objects, create_if_not_exists)
# After
index.partial_update_objects(objects, { createIfNotExists: true })
No change.
No change.
No change.
Renamed to clear_objects
.
# Before
index.clear_index
# After
index.clear_objects
The attributesToRetrieve
parameter is now part of the requestOptions
.
# Before
index.get_object('1234', ['title'])
index.get_objects([1,2,3], ['title'])
# After
index.get_object('1234', { attributesToRetrieve: ['title'] })
index.get_objects([1,2,3], { attributesToRetrieve: ['title'] })
No change.
No change.
No change.
No change.
Instead of calling the delete_index
method on the client, you should call the delete
method directly on the index object.
# Before
client.delete_index('foo')
# After
index.delete
Renamed to browse_objects
.
# Before
request_opts = { 'X-Algolia-UserToken': 'user123' }
index.browse({ query: 'query'}, nil, request_opts) do |hit|
puts hit
end
# After
opts = {
query: 'query',
headers: { 'X-Algolia-UserToken': 'user123' }
}
index.browse_objects(opts) do |hit|
puts hit
end
No change.
The objectID
parameter has been removed, and should be part of the synonym hash.
# Before
forward_to_replicas = true
index.save_synonym('one', { objectID: 'one', type: 'synonym', synonyms: %w(one two) }, forward_to_replicas)
# After
index.save_synonym({ objectID: 'one', type: 'synonym', synonyms: %w(one two) }, { forwardToReplicas: true})
Renamed to save_synonyms
. forwardToReplicas
and replaceExistingSynonyms
parmameters are now part of requestOptions
.
# Before
forward_to_replicas = true
replace_existing_synonyms = true
index.batch_synonyms(synonyms, forward_to_replicas, replace_existing_synonyms)
# After
index.save_synonyms(synonyms, { forwardToReplicas: true, replaceExistingSynonyms: true })
No change.
No change.
No change.
No change.
No change.
Renamed to browse_synonyms
.
# Before
synonyms = index.export_synonyms
# After
synonyms = index.browse_synonyms
The objectID
parameter has been removed, and should be part of the Rule object.
# Before
index.save_rule('unique-id', {
objectID: 'unique-id',
condition: { anchoring: 'is', pattern: 'pattern' },
consequence: {
params: {
query: {
edits: [
{ type: 'remove', delete: 'pattern' }
]
}
}
}
})
# After
index.save_rule({
objectID: 'unique-id',
condition: { anchoring: 'is', pattern: 'pattern' },
consequence: {
params: {
query: {
edits: [
{ type: 'remove', delete: 'pattern' }
]
}
}
}
})
Renamed to save_rules
. The forwardToReplicas
and clearExistingRules
parameters should now be part of the requestOptions
.
# Before
forward_to_replicas = true
clear_existing_rules = true
index.batch_rules(rules, forward_to_replicas, clear_existing_rules)
# After
index.save_rules(rules, { forwardToReplicas: true, clearExistingRules: true })
No change.
The forwardToReplicas
parameter is now part of the requestOptions
.
# Before
forward_to_replicas = true
index.delete_rule('rule-id', forward_to_replicas)
# After
index.delete_rule('rule-id', { forwardToReplicas: true })
The forwardToReplicas
parameter is now part of the requestOptions
.
# Before
forward_to_replicas = true
index.clear_rules(forward_to_replicas)
# After
index.clear_rules({ forwardToReplicas: true })
No change.
No change.
Renamed to browse_rules
.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
No change.
This new method is available on the Algolia::Recommendation::Client
class.
This new method is available on the Algolia::Recommendation::Client
class.
You can configure timeouts by passing a custom Algolia::Search::Config
object to the constructor of your client.
# Before
client = Algolia::Client.new({
application_id: 'app_id',
api_key: 'api_key',
connect_timeout: 2,
receive_timeout: 10,
})
# After
search_config = Algolia::Search::Config.new(application_id: 'app_id', api_key: 'api_key', read_timeout: 10, connect_timeout: 2)
client = Algolia::Search::Client.create_with_config(search_config)