Class: Algolia::Index
- Inherits:
-
Object
- Object
- Algolia::Index
- Defined in:
- lib/algolia/index.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#add_object(obj, objectID = nil) ⇒ Object
Add an object in this index.
-
#add_object!(obj, objectID = nil) ⇒ Object
Add an object in this index and wait end of indexing.
-
#add_objects(objs) ⇒ Object
Add several objects in this index.
-
#add_objects!(obj) ⇒ Object
Add several objects in this index and wait end of indexing.
-
#add_user_key(acls, validity = 0) ⇒ Object
Create a new user key.
-
#clear ⇒ Object
Delete the index content.
-
#clear! ⇒ Object
Delete the index content and wait end of indexing.
-
#delete ⇒ Object
Delete an index.
-
#delete_object(objectID) ⇒ Object
Delete an object from the index.
-
#delete_object!(objectID) ⇒ Object
Delete an object from the index and wait end of indexing.
-
#delete_user_key(key) ⇒ Object
Delete an existing user key.
-
#get_object(objectID, attributesToRetrieve = nil) ⇒ Object
Get an object from this index.
-
#get_settings ⇒ Object
Get settings of this index.
-
#get_user_key(key) ⇒ Object
Get ACL of a user key.
-
#initialize(name) ⇒ Index
constructor
A new instance of Index.
-
#list_user_keys ⇒ Object
List all existing user keys with their associated ACLs.
-
#partial_update_object(obj) ⇒ Object
Update partially an object (only update attributes passed in argument).
-
#partial_update_object!(obj) ⇒ Object
Update partially an object (only update attributes passed in argument) and wait indexing.
-
#save_object(obj) ⇒ Object
Override the content of object.
-
#save_object!(obj) ⇒ Object
Override the content of object and wait indexing.
-
#save_objects(objs) ⇒ Object
Override the content of several objects.
-
#save_objects!(objs) ⇒ Object
Override the content of several objects and wait indexing.
-
#search(query, params = {}) ⇒ Object
Search inside the index.
-
#set_settings(new_settings) ⇒ Object
Set settings for this index.
-
#wait_task(taskID, timeBeforeRetry = 100) ⇒ Object
Wait the publication of a task on the server.
Constructor Details
#initialize(name) ⇒ Index
Returns a new instance of Index.
9 10 11 |
# File 'lib/algolia/index.rb', line 9 def initialize(name) self.name = name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/algolia/index.rb', line 7 def name @name end |
Instance Method Details
#add_object(obj, objectID = nil) ⇒ Object
Add an object in this index
26 27 28 29 30 31 32 33 |
# File 'lib/algolia/index.rb', line 26 def add_object(obj, objectID = nil) raise ArgumentError.new("argument must not be an array") if obj.is_a?(Array) if objectID == nil Algolia.client.post(Protocol.index_uri(name), obj.to_json) else Algolia.client.put(Protocol.object_uri(name, objectID), obj.to_json) end end |
#add_object!(obj, objectID = nil) ⇒ Object
Add an object in this index and wait end of indexing
41 42 43 44 45 |
# File 'lib/algolia/index.rb', line 41 def add_object!(obj, objectID = nil) res = add_object(obj, objectID) wait_task(res["taskID"]) return res end |
#add_objects(objs) ⇒ Object
Add several objects in this index
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/algolia/index.rb', line 53 def add_objects(objs) raise ArgumentError.new("argument must be an array of object") if !objs.is_a?(Array) requests = [] objs.each do |obj| raise ArgumentError.new("argument must be an array of object") if obj.is_a?(Array) requests.push({"action" => "addObject", "body" => obj}) end request = {"requests" => requests}; Algolia.client.post(Protocol.batch_uri(name), request.to_json) end |
#add_objects!(obj) ⇒ Object
Add several objects in this index and wait end of indexing
70 71 72 73 74 |
# File 'lib/algolia/index.rb', line 70 def add_objects!(obj) res = add_objects(obj) wait_task(res["taskID"]) return res end |
#add_user_key(acls, validity = 0) ⇒ Object
Create a new user key
@param acls the list of ACL for this key. Defined by an array of strings that
can contains the following values:
- search: allow to search (https and http)
- addObject: allows to add a new object in the index (https only)
- updateObject : allows to change content of an existing object (https only)
- deleteObject : allows to delete an existing object (https only)
- deleteIndex : allows to delete index content (https only)
- settings : allows to get index settings (https only)
- editSettings : allows to change index settings (https only)
@param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
326 327 328 |
# File 'lib/algolia/index.rb', line 326 def add_user_key(acls, validity = 0) Algolia.client.post(Protocol.index_keys_uri(name), {"acl" => acls, "validity" => validity}.to_json) end |
#clear ⇒ Object
Delete the index content
239 240 241 |
# File 'lib/algolia/index.rb', line 239 def clear Algolia.client.post(Protocol.clear_uri(name), nil) end |
#clear! ⇒ Object
Delete the index content and wait end of indexing
246 247 248 249 250 |
# File 'lib/algolia/index.rb', line 246 def clear! res = clear wait_task(res["taskID"]) return res end |
#delete ⇒ Object
Delete an index
return an object of the form array(:deletedAt => “2013-01-18T15:33:13.556Z”)
16 17 18 |
# File 'lib/algolia/index.rb', line 16 def delete Algolia.client.delete(Protocol.index_uri(name)) end |
#delete_object(objectID) ⇒ Object
Delete an object from the index
217 218 219 220 221 222 |
# File 'lib/algolia/index.rb', line 217 def delete_object(objectID) if (objectID == nil || objectID.length == 0) then raise AlgoliaProtocolError.new(0, "objectID is required") end Algolia.client.delete(Protocol.object_uri(name, objectID)) end |
#delete_object!(objectID) ⇒ Object
Delete an object from the index and wait end of indexing
229 230 231 232 233 |
# File 'lib/algolia/index.rb', line 229 def delete_object!(objectID) res = delete_object(objectID) wait_task(res["taskID"]) return res end |
#delete_user_key(key) ⇒ Object
Delete an existing user key
331 332 333 |
# File 'lib/algolia/index.rb', line 331 def delete_user_key(key) Algolia.client.delete(Protocol.index_key_uri(name, key)) end |
#get_object(objectID, attributesToRetrieve = nil) ⇒ Object
Get an object from this index
125 126 127 128 129 130 131 |
# File 'lib/algolia/index.rb', line 125 def get_object(objectID, attributesToRetrieve = nil) if attributesToRetrieve == nil Algolia.client.get(Protocol.object_uri(name, objectID, nil)) else Algolia.client.get(Protocol.object_uri(name, objectID, {"attributes" => attributesToRetrieve})) end end |
#get_settings ⇒ Object
Get settings of this index
298 299 300 |
# File 'lib/algolia/index.rb', line 298 def get_settings Algolia.client.get(Protocol.settings_uri(name)) end |
#get_user_key(key) ⇒ Object
Get ACL of a user key
308 309 310 |
# File 'lib/algolia/index.rb', line 308 def get_user_key(key) Algolia.client.get(Protocol.index_key_uri(name, key)) end |
#list_user_keys ⇒ Object
List all existing user keys with their associated ACLs
303 304 305 |
# File 'lib/algolia/index.rb', line 303 def list_user_keys Algolia.client.get(Protocol.index_keys_uri(name)) end |
#partial_update_object(obj) ⇒ Object
Update partially an object (only update attributes passed in argument)
196 197 198 |
# File 'lib/algolia/index.rb', line 196 def partial_update_object(obj) Algolia.client.post(Protocol.partial_object_uri(name, obj["objectID"]), obj.to_json) end |
#partial_update_object!(obj) ⇒ Object
Update partially an object (only update attributes passed in argument) and wait indexing
206 207 208 209 210 |
# File 'lib/algolia/index.rb', line 206 def partial_update_object!(obj) res = partial_update_object(obj) wait_task(res["taskID"]) return res end |
#save_object(obj) ⇒ Object
Override the content of object
153 154 155 |
# File 'lib/algolia/index.rb', line 153 def save_object(obj) Algolia.client.put(Protocol.object_uri(name, obj["objectID"]), obj.to_json) end |
#save_object!(obj) ⇒ Object
Override the content of object and wait indexing
161 162 163 164 165 |
# File 'lib/algolia/index.rb', line 161 def save_object!(obj) res = save_object(obj) wait_task(res["taskID"]) return res end |
#save_objects(objs) ⇒ Object
Override the content of several objects
171 172 173 174 175 176 177 178 |
# File 'lib/algolia/index.rb', line 171 def save_objects(objs) requests = [] objs.each do |obj| requests.push({"action" => "updateObject", "objectID" => obj["objectID"], "body" => obj}) end request = {"requests" => requests}; Algolia.client.post(Protocol.batch_uri(name), request.to_json) end |
#save_objects!(objs) ⇒ Object
Override the content of several objects and wait indexing
184 185 186 187 188 |
# File 'lib/algolia/index.rb', line 184 def save_objects!(objs) res = save_objects(objs) wait_task(res["taskID"]) return res end |
#search(query, params = {}) ⇒ Object
Search inside the index
115 116 117 |
# File 'lib/algolia/index.rb', line 115 def search(query, params = {}) Algolia.client.get(Protocol.search_uri(name, query, params)) end |
#set_settings(new_settings) ⇒ Object
Set settings for this index
293 294 295 |
# File 'lib/algolia/index.rb', line 293 def set_settings(new_settings) Algolia.client.put(Protocol.settings_uri(name), new_settings.to_json) end |
#wait_task(taskID, timeBeforeRetry = 100) ⇒ Object
Wait the publication of a task on the server. All server task are asynchronous and you can check with this method that the task is published.
139 140 141 142 143 144 145 146 147 |
# File 'lib/algolia/index.rb', line 139 def wait_task(taskID, timeBeforeRetry = 100) loop do status = Algolia.client.get(Protocol.task_uri(name, taskID))["status"] if status == "published" return end sleep(timeBeforeRetry.to_f / 1000) end end |