class Algolia::Index
def self.get_object_position(objects, object_id)
-
(Integer)- position of the object, or -1 if it's not in the array
Parameters:
-
object_id(String) -- the object to look for -
objects(Array) -- the result set to browse
def self.get_object_position(objects, object_id) objects['hits'].find_index { |hit| hit['objectID'] == object_id } || -1 end
def add_api_key(object, validity = 0, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
max_hits_per_query() -- the maximum number of hits this API key can retrieve in one call (0 means unlimited) -
max_queries_per_IP_per_hour() -- the maximum number of API calls allowed from an IP address per hour (0 means unlimited) -
validity() -- the number of seconds after which the key will be automatically removed (0 means no time limit for this key) -
object() -- can be two different parameters:
def add_api_key(object, validity = 0, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, request_options = {}) if object.instance_of?(Array) params = { :acl => object } else params = object end params['validity'] = validity.to_i if validity != 0 params['maxHitsPerQuery'] = max_hits_per_query.to_i if max_hits_per_query != 0 params['maxQueriesPerIPPerHour'] = max_queries_per_IP_per_hour.to_i if max_queries_per_IP_per_hour != 0 client.post(Protocol.index_keys_uri(name), params.to_json, :write, request_options) end
def add_object(object, objectID = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID(optional) -- an objectID you want to attribute to this object -
object() -- the object to add to the index.
def add_object(object, objectID = nil, request_options = {}) check_object(object) if objectID.nil? || objectID.to_s.empty? client.post(Protocol.index_uri(name), object.to_json, :write, request_options) else client.put(Protocol.object_uri(name, objectID), object.to_json, :write, request_options) end end
def add_object!(object, objectID = nil, request_options = {})
-
Request() -- options object. Contains extra URL parameters or headers -
objectID(optional) -- an objectID you want to attribute to this object -
object() -- the object to add to the index.
def add_object!(object, objectID = nil, request_options = {}) res = add_object(object, objectID, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def add_objects(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to add inside the index.
def add_objects(objects, request_options = {}) batch(build_batch('addObject', objects, false), request_options) end
def add_objects!(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to add inside the index.
def add_objects!(objects, request_options = {}) res = add_objects(objects, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def batch(request, request_options = {})
Send a batch request
def batch(request, request_options = {}) client.post(Protocol.batch_uri(name), request.to_json, :batch, request_options) end
def batch!(request, request_options = {})
Send a batch request and wait the end of the indexing
def batch!(request, request_options = {}) res = batch(request, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def batch_rules(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
clear_existing_rules() -- should we clear the existing rules before adding the new ones -
forward_to_replicas() -- should we forward the delete to replica indices -
rules() -- the array of rules to add/update
def batch_rules(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {}) client.post("#{Protocol.batch_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}&clearExistingRules=#{clear_existing_rules}", rules.to_json, :batch, request_options) end
def batch_rules!(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
clear_existing_rules() -- should we clear the existing rules before adding the new ones -
forward_to_replicas() -- should we forward the delete to replica indices -
rules() -- the array of rules to add/update
def batch_rules!(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {}) res = batch_rules(rules, forward_to_replicas, clear_existing_rules, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) return res end
def batch_synonyms(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
replace_existing_synonyms() -- should we replace the existing synonyms before adding the new ones -
forward_to_replicas() -- should we forward the delete to replica indices -
synonyms() -- the array of synonyms to add/update
def batch_synonyms(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {}) client.post("#{Protocol.batch_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}&replaceExistingSynonyms=#{replace_existing_synonyms}", synonyms.to_json, :batch, request_options) end
def batch_synonyms!(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
replace_existing_synonyms() -- should we replace the existing synonyms before adding the new ones -
forward_to_replicas() -- should we forward the delete to replica indices -
synonyms() -- the array of synonyms to add/update
def batch_synonyms!(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {}) res = batch_synonyms(synonyms, forward_to_replicas, replace_existing_synonyms, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def browse(page_or_query_parameters = nil, hits_per_page = nil, request_options = {}, &block)
-
hits_per_page() -- Pagination parameter used to select the number of hits per page. Defaults to 1000. -
page() -- Pagination parameter used to select the page to retrieve. -
request_options() -- contains extra parameters to send with your query -
queryParameters() -- An optional second parameters hash here for backward-compatibility (which will be merged with the first) -
queryParameters() -- The hash of query parameters to use to browse
def browse(page_or_query_parameters = nil, hits_per_page = nil, request_options = {}, &block) params = {} if page_or_query_parameters.is_a?(Hash) params.merge!(page_or_query_parameters) else params[:page] = page_or_query_parameters unless page_or_query_parameters.nil? end if hits_per_page.is_a?(Hash) params.merge!(hits_per_page) else params[:hitsPerPage] = hits_per_page unless hits_per_page.nil? end if block_given? IndexBrowser.new(client, name, params).browse(request_options, &block) else params[:page] ||= 0 params[:hitsPerPage] ||= 1000 client.get(Protocol.browse_uri(name, params), :read, request_options) end end
def browse_from(cursor, hits_per_page = 1000, request_options = {})
-
request_options() -- contains extra parameters to send with your query
def browse_from(cursor, hits_per_page = 1000, request_options = {}) client.post(Protocol.browse_uri(name), { :cursor => cursor, :hitsPerPage => hits_per_page }.to_json, :read, request_options) end
def build_batch(action, objects, with_object_id = false)
def build_batch(action, objects, with_object_id = false) check_array(objects) { :requests => objects.map { |object| check_object(object, true) h = { :action => action, :body => object } h[:objectID] = get_objectID(object).to_s if with_object_id h } } end
def check_array(object)
def check_array(object) raise ArgumentError.new('argument must be an array of objects') if !object.is_a?(Array) end
def check_object(object, in_array = false)
def check_object(object, in_array = false) case object when Array raise ArgumentError.new(in_array ? 'argument must be an array of objects' : 'argument must not be an array') when String, Integer, Float, TrueClass, FalseClass, NilClass raise ArgumentError.new("argument must be an #{'array of' if in_array} object, got: #{object.inspect}") else # ok end end
def clear(request_options = {})
-
request_options() -- contains extra parameters to send with your query
def clear(request_options = {}) client.post(Protocol.clear_uri(name), {}, :write, request_options) end
def clear!(request_options = {})
Delete the index content and wait end of indexing
def clear!(request_options = {}) res = clear(request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def clear_rules(forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices
def clear_rules(forward_to_replicas = false, request_options = {}) client.post("#{Protocol.clear_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}", {}, :write, request_options) end
def clear_rules!(forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices
def clear_rules!(forward_to_replicas = false, request_options = {}) res = clear_rules(forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) return res end
def clear_synonyms(forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices
def clear_synonyms(forward_to_replicas = false, request_options = {}) client.post("#{Protocol.clear_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}", {}, :write, request_options) end
def clear_synonyms!(forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices
def clear_synonyms!(forward_to_replicas = false, request_options = {}) res = clear_synonyms(forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def delete(request_options = {})
-
request_options() -- contains extra parameters to send with your query
def delete(request_options = {}) client.delete(Protocol.index_uri(name), :write, request_options) end
def delete!(request_options = {})
-
request_options() -- contains extra parameters to send with your query
def delete!(request_options = {}) res = delete(request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def delete_api_key(key, request_options = {})
Delete an existing user key
def delete_api_key(key, request_options = {}) client.delete(Protocol.index_key_uri(name, key), :write, request_options) end
def delete_by(params, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- query parameters
def delete_by(params, request_options = {}) raise ArgumentError.new('params cannot be nil, use the `clear` method to wipe the entire index') if params.nil? params = sanitized_delete_by_query_params(params) client.post(Protocol.delete_by_uri(name), params.to_json, :write, request_options) end
def delete_by!(params, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- query parameters
def delete_by!(params, request_options = {}) res = delete_by(params, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) if res res end
def delete_by_query(query, params = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- the optional query parameters -
query() -- the query string
def delete_by_query(query, params = nil, request_options = {}) raise ArgumentError.new('query cannot be nil, use the `clear` method to wipe the entire index') if query.nil? && params.nil? params = sanitized_delete_by_query_params(params) params[:query] = query params[:hitsPerPage] = 1000 params[:distinct] = false params[:attributesToRetrieve] = ['objectID'] params[:cursor] = '' ids = [] while params[:cursor] != nil result = browse(params, nil, request_options) params[:cursor] = result['cursor'] hits = result['hits'] break if hits.empty? ids += hits.map { |hit| hit['objectID'] } end delete_objects(ids, request_options) end
def delete_by_query!(query, params = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- the optional query parameters -
query() -- the query string
def delete_by_query!(query, params = nil, request_options = {}) res = delete_by_query(query, params, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) if res res end
def delete_object(objectID, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the unique identifier of object to delete
def delete_object(objectID, request_options = {}) raise ArgumentError.new('objectID must not be blank') if objectID.nil? || objectID == '' client.delete(Protocol.object_uri(name, objectID), :write, request_options) end
def delete_object!(objectID, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the unique identifier of object to delete
def delete_object!(objectID, request_options = {}) res = delete_object(objectID, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def delete_objects(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- an array of objectIDs
def delete_objects(objects, request_options = {}) check_array(objects) batch(build_batch('deleteObject', objects.map { |objectID| { :objectID => objectID } }, false), request_options) end
def delete_objects!(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- an array of objectIDs
def delete_objects!(objects, request_options = {}) res = delete_objects(objects, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def delete_rule(objectID, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
objectID() -- the rule objectID
def delete_rule(objectID, forward_to_replicas = false, request_options = {}) client.delete("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write, request_options) end
def delete_rule!(objectID, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
objectID() -- the rule objectID
def delete_rule!(objectID, forward_to_replicas = false, request_options = {}) res = delete_rule(objectID, forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) return res end
def delete_synonym(objectID, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
objectID() -- the synonym objectID
def delete_synonym(objectID, forward_to_replicas = false, request_options = {}) client.delete("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write, request_options) end
def delete_synonym!(objectID, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
objectID() -- the synonym objectID
def delete_synonym!(objectID, forward_to_replicas = false, request_options = {}) res = delete_synonym(objectID, forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def exists
-
(Boolean)-
def exists begin get_settings rescue AlgoliaProtocolError => e if e.code === 404 return false end raise e end return true end
def export_rules(hits_per_page = 100, request_options = {}, &_block)
-
request_options() -- contains extra parameters to send with your query - Optional -
hits_per_page() -- Amount of rules to retrieve on each internal request - Optional - Default: 100
def export_rules(hits_per_page = 100, request_options = {}, &_block) res = [] page = 0 loop do curr = search_rules('', { :hitsPerPage => hits_per_page, :page => page }, request_options)['hits'] curr.each do |rule| res << rule yield rule if block_given? end break if curr.size < hits_per_page page += 1 end res end
def export_synonyms(hits_per_page = 100, request_options = {}, &_block)
-
request_options() -- contains extra parameters to send with your query - Optional -
hits_per_page() -- Amount of synonyms to retrieve on each internal request - Optional - Default: 100
def export_synonyms(hits_per_page = 100, request_options = {}, &_block) res = [] page = 0 loop do curr = search_synonyms('', { :hitsPerPage => hits_per_page, :page => page }, request_options)['hits'] curr.each do |synonym| res << synonym yield synonym if block_given? end break if curr.size < hits_per_page page += 1 end res end
def find_object(request_options = {})
-
(Hash)- the matching object and its position in the result set
Parameters:
-
request_options() -- contains extra parameters to send with your query
def find_object(request_options = {}) paginate = true page = 0 query = request_options[:query] || request_options['query'] || '' request_options.delete(:query) request_options.delete('query') if request_options.has_key? :paginate paginate = request_options[:paginate] end if request_options.has_key? 'paginate' paginate = request_options['paginate'] end request_options.delete(:paginate) request_options.delete('paginate') while true request_options['page'] = page res = search(query, request_options) res['hits'].each_with_index do |hit, i| if yield(hit) return { 'object' => hit, 'position' => i, 'page' => page, } end end if block_given? has_next_page = page + 1 < res['nbPages'] if !paginate || !has_next_page raise AlgoliaObjectNotFoundError.new('Object not found') end page += 1 end end
def get_api_key(key, request_options = {})
Get ACL of a user key
def get_api_key(key, request_options = {}) client.get(Protocol.index_key_uri(name, key), :read, request_options) end
def get_object(objectID, attributes_to_retrieve = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
attributes_to_retrieve(optional) -- if set, contains the list of attributes to retrieve as an array of strings of a string separated by "," -
objectID() -- the unique identifier of the object to retrieve
def get_object(objectID, attributes_to_retrieve = nil, request_options = {}) attributes_to_retrieve = attributes_to_retrieve.join(',') if attributes_to_retrieve.is_a?(Array) if attributes_to_retrieve.nil? client.get(Protocol.object_uri(name, objectID, nil), :read, request_options) else client.get(Protocol.object_uri(name, objectID, { :attributes => attributes_to_retrieve }), :read, request_options) end end
def get_objectID(object, objectID = nil)
def get_objectID(object, objectID = nil) check_object(object) objectID ||= object[:objectID] || object['objectID'] raise ArgumentError.new("Missing 'objectID'") if objectID.nil? return objectID end
def get_objects(objectIDs, attributes_to_retrieve = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
attributes_to_retrieve(optional) -- if set, contains the list of attributes to retrieve as an array of strings of a string separated by "," -
objectIDs() -- the array of unique identifier of the objects to retrieve
def get_objects(objectIDs, attributes_to_retrieve = nil, request_options = {}) attributes_to_retrieve = attributes_to_retrieve.join(',') if attributes_to_retrieve.is_a?(Array) requests = objectIDs.map do |objectID| req = { :indexName => name, :objectID => objectID.to_s } req[:attributesToRetrieve] = attributes_to_retrieve unless attributes_to_retrieve.nil? req end client.post(Protocol.objects_uri, { :requests => requests }.to_json, :read, request_options)['results'] end
def get_rule(objectID, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the rule objectID
def get_rule(objectID, request_options = {}) client.get(Protocol.rule_uri(name, objectID), :read, request_options) end
def get_settings(options = {}, request_options = {})
Get settings of this index
def get_settings(options = {}, request_options = {}) options['getVersion'] = 2 if !options[:getVersion] && !options['getVersion'] client.get(Protocol.settings_uri(name, options).to_s, :read, request_options) end
def get_synonym(objectID, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the synonym objectID
def get_synonym(objectID, request_options = {}) client.get(Protocol.synonym_uri(name, objectID), :read, request_options) end
def get_task_status(taskID, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
taskID() -- the id of the task returned by server
def get_task_status(taskID, request_options = {}) client.get_task_status(name, taskID, request_options) end
def initialize(name, client = nil)
def initialize(name, client = nil) self.name = name self.client = client || Algolia.client end
def list_api_keys(request_options = {})
List all existing user keys with their associated ACLs
def list_api_keys(request_options = {}) client.get(Protocol.index_keys_uri(name), :read, request_options) end
def partial_update_object(object, objectID = nil, create_if_not_exits = true, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
create_if_not_exits() -- a boolean, if true creates the object if this one doesn't exist -
objectID() -- the associated objectID, if nil 'object' must contain an 'objectID' key -
object() -- the object attributes to override
def partial_update_object(object, objectID = nil, create_if_not_exits = true, request_options = {}) client.post(Protocol.partial_object_uri(name, get_objectID(object, objectID), create_if_not_exits), object.to_json, :write, request_options) end
def partial_update_object!(object, objectID = nil, create_if_not_exits = true, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
create_if_not_exits() -- a boolean, if true creates the object if this one doesn't exist -
objectID() -- the associated objectID, if nil 'object' must contain an 'objectID' key -
object() -- the attributes to override
def partial_update_object!(object, objectID = nil, create_if_not_exits = true, request_options = {}) res = partial_update_object(object, objectID, create_if_not_exits, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def partial_update_objects(objects, create_if_not_exits = true, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
create_if_not_exits() -- a boolean, if true create the objects if they don't exist -
objects() -- an array of objects to update (each object must contains a objectID attribute)
def partial_update_objects(objects, create_if_not_exits = true, request_options = {}) if create_if_not_exits batch(build_batch('partialUpdateObject', objects, true), request_options) else batch(build_batch('partialUpdateObjectNoCreate', objects, true), request_options) end end
def partial_update_objects!(objects, create_if_not_exits = true, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
create_if_not_exits() -- a boolean, if true create the objects if they don't exist -
objects() -- an array of objects to update (each object must contains a objectID attribute)
def partial_update_objects!(objects, create_if_not_exits = true, request_options = {}) res = partial_update_objects(objects, create_if_not_exits, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def replace_all_objects(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to save
def replace_all_objects(objects, request_options = {}) safe = request_options[:safe] || request_options['safe'] || false request_options.delete(:safe) request_options.delete('safe') tmp_index = @client.init_index(@name + '_tmp_' + rand(10000000).to_s) responses = [] scope = ['settings', 'synonyms', 'rules'] res = @client.copy_index(@name, tmp_index.name, scope, request_options) responses << res if safe wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) end batch = [] batch_size = 1000 count = 0 objects.each do |object| batch << object count += 1 if count == batch_size res = tmp_index.add_objects(batch, request_options) responses << res batch = [] count = 0 end end if batch.any? res = tmp_index.add_objects(batch, request_options) responses << res end if safe responses.each do |res| tmp_index.wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) end end res = @client.move_index(tmp_index.name, @name, request_options) responses << res if safe wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) end responses end
def replace_all_objects!(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to save
def replace_all_objects!(objects, request_options = {}) replace_all_objects(objects, request_options.merge(:safe => true)) end
def replace_all_rules(rules, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
rules() -- the array of rules to add
def replace_all_rules(rules, request_options = {}) forward_to_replicas = request_options[:forwardToReplicas] || request_options['forwardToReplicas'] || false batch_rules(rules, forward_to_replicas, true, request_options) end
def replace_all_rules!(rules, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
rules() -- the array of rules to add
def replace_all_rules!(rules, request_options = {}) res = replace_all_rules(rules, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def replace_all_synonyms(synonyms, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
synonyms() -- the array of synonyms to add
def replace_all_synonyms(synonyms, request_options = {}) forward_to_replicas = request_options[:forwardToReplicas] || request_options['forwardToReplicas'] || false batch_synonyms(synonyms, forward_to_replicas, true, request_options) end
def replace_all_synonyms!(synonyms, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
synonyms() -- the array of synonyms to add
def replace_all_synonyms!(synonyms, request_options = {}) res = replace_all_synonyms(synonyms, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def sanitized_delete_by_query_params(params)
def sanitized_delete_by_query_params(params) params ||= {} params.delete(:hitsPerPage) params.delete('hitsPerPage') params.delete(:attributesToRetrieve) params.delete('attributesToRetrieve') params end
def save_object(object, objectID = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the associated objectID, if nil 'object' must contain an 'objectID' key -
object() -- the object to save
def save_object(object, objectID = nil, request_options = {}) client.put(Protocol.object_uri(name, get_objectID(object, objectID)), object.to_json, :write, request_options) end
def save_object!(object, objectID = nil, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objectID() -- the associated objectID, if nil 'object' must contain an 'objectID' key -
object() -- the object to save
def save_object!(object, objectID = nil, request_options = {}) res = save_object(object, objectID, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def save_objects(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to save, each object must contain an 'objectID' key
def save_objects(objects, request_options = {}) batch(build_batch('updateObject', objects, true), request_options) end
def save_objects!(objects, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
objects() -- the array of objects to save, each object must contain an objectID attribute
def save_objects!(objects, request_options = {}) res = save_objects(objects, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def save_rule(objectID, rule, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
rule() -- the rule -
objectID() -- the rule objectID
def save_rule(objectID, rule, forward_to_replicas = false, request_options = {}) raise ArgumentError.new('objectID must not be blank') if objectID.nil? || objectID == '' client.put("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", rule.to_json, :write, request_options) end
def save_rule!(objectID, rule, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
rule() -- the rule -
objectID() -- the rule objectID
def save_rule!(objectID, rule, forward_to_replicas = false, request_options = {}) res = save_rule(objectID, rule, forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) return res end
def save_synonym(objectID, synonym, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
synonym() -- the synonym -
objectID() -- the synonym objectID
def save_synonym(objectID, synonym, forward_to_replicas = false, request_options = {}) client.put("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", synonym.to_json, :write, request_options) end
def save_synonym!(objectID, synonym, forward_to_replicas = false, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
forward_to_replicas() -- should we forward the delete to replica indices -
synonym() -- the synonym -
objectID() -- the synonym objectID
def save_synonym!(objectID, synonym, forward_to_replicas = false, request_options = {}) res = save_synonym(objectID, synonym, forward_to_replicas, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def search(query, params = {}, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
args(optional) -- if set, contains an associative array with query parameters: -
query() -- the full text query
def search(query, params = {}, request_options = {}) encoded_params = Hash[params.map { |k, v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }] encoded_params[:query] = query client.post(Protocol.search_post_uri(name), { :params => Protocol.to_query(encoded_params) }.to_json, :search, request_options) end
def search_disjunctive_faceting(query, disjunctive_facets, params = {}, refinements = {}, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
refinements() -- a hash ("string" -> ["array", "of", "refined", "values"]) representing the current refinements -
params() -- a hash representing the regular query parameters -
disjunctive_facets() -- the array of disjunctive facets -
query() -- the query
def search_disjunctive_faceting(query, disjunctive_facets, params = {}, refinements = {}, request_options = {}) raise ArgumentError.new('Argument "disjunctive_facets" must be a String or an Array') unless disjunctive_facets.is_a?(String) || disjunctive_facets.is_a?(Array) raise ArgumentError.new('Argument "refinements" must be a Hash of Arrays') if !refinements.is_a?(Hash) || !refinements.select { |k, v| !v.is_a?(Array) }.empty? # extract disjunctive facets & associated refinements disjunctive_facets = disjunctive_facets.split(',') if disjunctive_facets.is_a?(String) disjunctive_refinements = {} refinements.each do |k, v| disjunctive_refinements[k] = v if disjunctive_facets.include?(k) || disjunctive_facets.include?(k.to_s) end # build queries queries = [] ## hits + regular facets query filters = [] refinements.to_a.each do |k, values| r = values.map { |v| "#{k}:#{v}" } if disjunctive_refinements[k.to_s] || disjunctive_refinements[k.to_sym] # disjunctive refinements are ORed filters << r else # regular refinements are ANDed filters += r end end queries << params.merge({ :index_name => self.name, :query => query, :facetFilters => filters }) ## one query per disjunctive facet (use all refinements but the current one + hitsPerPage=1 + single facet) disjunctive_facets.each do |disjunctive_facet| filters = [] refinements.each do |k, values| if k.to_s != disjunctive_facet.to_s r = values.map { |v| "#{k}:#{v}" } if disjunctive_refinements[k.to_s] || disjunctive_refinements[k.to_sym] # disjunctive refinements are ORed filters << r else # regular refinements are ANDed filters += r end end end queries << params.merge({ :index_name => self.name, :query => query, :page => 0, :hitsPerPage => 1, :attributesToRetrieve => [], :attributesToHighlight => [], :attributesToSnippet => [], :facets => disjunctive_facet, :facetFilters => filters, :analytics => false }) end answers = client.multiple_queries(queries, { :request_options => request_options }) # aggregate answers ## first answer stores the hits + regular facets aggregated_answer = answers['results'][0] ## others store the disjunctive facets aggregated_answer['disjunctiveFacets'] = {} answers['results'].each_with_index do |a, i| next if i == 0 a['facets'].each do |facet, values| ## add the facet to the disjunctive facet hash aggregated_answer['disjunctiveFacets'][facet] = values ## concatenate missing refinements (disjunctive_refinements[facet.to_s] || disjunctive_refinements[facet.to_sym] || []).each do |r| if aggregated_answer['disjunctiveFacets'][facet][r].nil? aggregated_answer['disjunctiveFacets'][facet][r] = 0 end end end end aggregated_answer end
def search_for_facet_values(facet_name, facet_query, search_parameters = {}, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
search_parameters() -- An optional query to take extra search parameters into account. -
facet_query() -- Text to search for in the facet's values -
facet_name() -- Name of the facet to search. It must have been declared in the
def search_for_facet_values(facet_name, facet_query, search_parameters = {}, request_options = {}) params = search_parameters.clone params['facetQuery'] = facet_query client.post(Protocol.search_facet_uri(name, facet_name), params.to_json, :read, request_options) end
def search_rules(query, params = {}, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- an optional hash of :anchoring, :context, :page, :hitsPerPage -
query() -- the query
def search_rules(query, params = {}, request_options = {}) anchoring = params[:anchoring] context = params[:context] page = params[:page] || params['page'] || 0 hits_per_page = params[:hitsPerPage] || params['hitsPerPage'] || 20 params = { :query => query, :page => page, :hitsPerPage => hits_per_page } params[:anchoring] = anchoring unless anchoring.nil? params[:context] = context unless context.nil? client.post(Protocol.search_rules_uri(name), params.to_json, :read, request_options) end
def search_synonyms(query, params = {}, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
params() -- an optional hash of :type, :page, :hitsPerPage -
query() -- the query
def search_synonyms(query, params = {}, request_options = {}) type = params[:type] || params['type'] type = type.join(',') if type.is_a?(Array) page = params[:page] || params['page'] || 0 hits_per_page = params[:hitsPerPage] || params['hitsPerPage'] || 20 params = { :query => query, :type => type.to_s, :page => page, :hitsPerPage => hits_per_page } client.post(Protocol.search_synonyms_uri(name), params.to_json, :read, request_options) end
def set_settings(new_settings, options = {}, request_options = {})
Set settings for this index
def set_settings(new_settings, options = {}, request_options = {}) client.put(Protocol.settings_uri(name, options), new_settings.to_json, :write, request_options) end
def set_settings!(new_settings, options = {}, request_options = {})
Set settings for this index and wait end of indexing
def set_settings!(new_settings, options = {}, request_options = {}) res = set_settings(new_settings, options, request_options) wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) res end
def update_api_key(key, object, validity = 0, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
max_hits_per_query() -- the maximum number of hits this API key can retrieve in one call (0 means unlimited) -
max_queries_per_IP_per_hour() -- the maximum number of API calls allowed from an IP address per hour (0 means unlimited) -
validity() -- the number of seconds after which the key will be automatically removed (0 means no time limit for this key) -
object() -- can be two different parameters:
def update_api_key(key, object, validity = 0, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, request_options = {}) if object.instance_of?(Array) params = { :acl => object } else params = object end params['validity'] = validity.to_i if validity != 0 params['maxHitsPerQuery'] = max_hits_per_query.to_i if max_hits_per_query != 0 params['maxQueriesPerIPPerHour'] = max_queries_per_IP_per_hour.to_i if max_queries_per_IP_per_hour != 0 client.put(Protocol.index_key_uri(name, key), params.to_json, :write, request_options) end
def wait_task(taskID, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options = {})
-
request_options() -- contains extra parameters to send with your query -
time_before_retry() -- the time in milliseconds before retry (default = 100ms) -
taskID() -- the id of the task returned by server
def wait_task(taskID, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options = {}) client.wait_task(name, taskID, time_before_retry, request_options) end