Class: Algolia::Search::Index
- Inherits:
-
Object
- Object
- Algolia::Search::Index
- Defined in:
- lib/algolia/search_index.rb
Overview
Class Index
Constant Summary
Constants included from CallType
CallType::READ, CallType::WRITE
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#transporter ⇒ Object
readonly
Returns the value of attribute transporter.
Class Method Summary collapse
-
.get_object_position(objects, object_id) ⇒ Integer
Retrieve the given object position in a set of results.
Instance Method Summary collapse
-
#batch(requests, opts = {}) ⇒ IndexingResponse
Send a batch request.
-
#batch!(requests, opts = {}) ⇒ IndexingResponse
Send a batch request and wait for operation to complete.
-
#browse_objects(opts = {}, &block) ⇒ Enumerator, ObjectIterator
Browse all index content.
-
#browse_rules(opts = {}, &block) ⇒ Enumerator, RuleIterator
Browse all rules.
-
#browse_synonyms(opts = {}, &block) ⇒ Enumerator, SynonymIterator
Browse all synonyms.
-
#clear_objects(opts = {}) ⇒ Object
Delete the index content.
-
#clear_objects!(opts = {}) ⇒ Object
Delete the index content and wait for operation to complete.
-
#clear_rules(opts = {}) ⇒ IndexingResponse
Delete all Rules in the index.
-
#clear_rules!(opts = {}) ⇒ IndexingResponse
Delete all Rules in the index and wait for operation to complete.
-
#clear_synonyms(opts = {}) ⇒ IndexingResponse
Delete all synonyms from the index.
-
#clear_synonyms!(opts = {}) ⇒ IndexingResponse
Delete all synonyms from the index and wait for operation to complete.
-
#copy_to(name, opts = {}) ⇒ IndexingResponse
Copy the current index to the given destination name.
-
#delete(opts = {}) ⇒ Object
Delete an existing index.
-
#delete!(opts = {}) ⇒ Object
Delete an existing index and wait for operation to complete.
-
#delete_by(filters, opts = {}) ⇒ IndexingResponse
Delete all records matching the query.
-
#delete_by!(filters, opts = {}) ⇒ IndexingResponse
Delete all records matching the query and wait for operation to complete.
-
#delete_object(object_id, opts = {}) ⇒ IndexingResponse
Delete an existing object from an index.
-
#delete_object!(object_id, opts = {}) ⇒ IndexingResponse
Delete an existing object from an index and wait for operation to complete.
-
#delete_objects(object_ids, opts = {}) ⇒ IndexingResponse
Delete several existing objects from an index.
-
#delete_objects!(object_ids, opts = {}) ⇒ IndexingResponse
Delete several existing objects from an index and wait for operation to complete.
-
#delete_rule(object_id, opts = {}) ⇒ IndexingResponse
Delete the Rule with the specified objectID.
-
#delete_rule!(object_id, opts = {}) ⇒ IndexingResponse
Delete the Rule with the specified objectID and wait for operation to complete.
-
#delete_synonym(object_id, opts = {}) ⇒ IndexingResponse
Delete a single synonyms set, identified by the given objectID.
-
#delete_synonym!(object_id, opts = {}) ⇒ IndexingResponse
Delete a single synonyms set, identified by the given objectID and wait for operation to complete.
-
#exists ⇒ Boolean
(also: #exists?)
Checks if the current index exists.
-
#find_object(callback, opts = {}) ⇒ Hash|AlgoliaHttpError
Find object by the given condition.
-
#get_object(object_id, opts = {}) ⇒ Hash
Retrieve one object from the index.
-
#get_objects(object_ids, opts = {}) ⇒ Hash
Retrieve one or more objects in a single API call.
-
#get_rule(object_id, opts = {}) ⇒ Hash
Retrieve the Rule with the specified objectID.
-
#get_settings(opts = {}) ⇒ Hash
Retrieve index settings.
-
#get_synonym(object_id, opts = {}) ⇒ Hash
Fetch a synonym object identified by its objectID.
-
#get_task_status(task_id, opts = {}) ⇒ Object
Check the status of a task on the server.
-
#initialize(name, transporter, config) ⇒ Index
constructor
Initialize an index.
-
#move_to(name, opts = {}) ⇒ IndexingResponse
Move the current index to the given destination name.
-
#partial_update_object(object, opts = {}) ⇒ IndexingResponse
Partially update an object.
-
#partial_update_object!(object, opts = {}) ⇒ IndexingResponse
Partially update an object and wait for operation to complete.
-
#partial_update_objects(objects, opts = {}) ⇒ IndexingResponse
Partially update several objects.
-
#partial_update_objects!(objects, opts = {}) ⇒ IndexingResponse
Partially update several objects and wait for operation to complete.
-
#replace_all_objects(objects, opts = {}) ⇒ Enumerator, SynonymIterator
Replace all objects in the index.
-
#replace_all_objects!(objects, opts = {}) ⇒ Enumerator, SynonymIterator
Replace all objects in the index and wait for the operation to complete.
-
#replace_all_rules(rules, opts = {}) ⇒ Array, IndexingResponse
Replace all rules in the index.
-
#replace_all_rules!(rules, opts = {}) ⇒ Array, IndexingResponse
Replace all rules in the index and wait for the operation to complete.
-
#replace_all_synonyms(synonyms, opts = {}) ⇒ Array, IndexingResponse
Replace all synonyms in the index.
-
#replace_all_synonyms!(synonyms, opts = {}) ⇒ Array, IndexingResponse
Replace all synonyms in the index and wait for the operation to complete.
-
#save_object(object, opts = {}) ⇒ IndexingResponse
Add an object to the index.
-
#save_object!(object, opts = {}) ⇒ IndexingResponse
Add an object to the index and wait for operation to complete.
-
#save_objects(objects, opts = {}) ⇒ IndexingResponse
Add several objects to the index.
-
#save_objects!(objects, opts = {}) ⇒ IndexingResponse
Add several objects to the index and wait for operation to complete.
-
#save_rule(rule, opts = {}) ⇒ Array, IndexingResponse
Create or update a rule.
-
#save_rule!(rule, opts = {}) ⇒ Array, IndexingResponse
Create or update a rule and wait for operation to complete.
-
#save_rules(rules, opts = {}) ⇒ Array, IndexingResponse
Create or update rules.
-
#save_rules!(rules, opts = {}) ⇒ Array, IndexingResponse
Create or update rules and wait for operation to complete.
-
#save_synonym(synonym, opts = {}) ⇒ Array, IndexingResponse
Create a new synonym object or update the existing synonym object with the given object ID.
-
#save_synonym!(synonym, opts = {}) ⇒ Array, IndexingResponse
Create a new synonym object or update the existing synonym object with the given object ID and wait for operation to finish.
-
#save_synonyms(synonyms, opts = {}) ⇒ Array, IndexingResponse
Create/update multiple synonym objects at once, potentially replacing the entire list of synonyms if replaceExistingSynonyms is true.
-
#save_synonyms!(synonyms, opts = {}) ⇒ Array, IndexingResponse
Create/update multiple synonym objects at once, potentially replacing the entire list of synonyms if replaceExistingSynonyms is true and wait for operation to complete.
-
#search(query, opts = {}) ⇒ Hash
Perform a search on the index.
-
#search_for_facet_values(facet_name, facet_query, opts = {}) ⇒ Hash
Search for values of a given facet, optionally restricting the returned values to those contained in objects matching other search criteria.
-
#search_rules(query, opts = {}) ⇒ Hash
Search or browse all rules, optionally filtering them by type.
-
#search_synonyms(query, opts = {}) ⇒ Hash
Search or browse all synonyms, optionally filtering them by type.
-
#set_settings(settings, opts = {}) ⇒ IndexingResponse
Update some index settings.
-
#set_settings!(settings, opts = {}) ⇒ IndexingResponse
Update some index settings and wait for operation to complete.
-
#wait_task(task_id, time_before_retry = Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {}) ⇒ Object
Wait the publication of a task on the server.
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(name, transporter, config) ⇒ Index
Initialize an index
16 17 18 19 20 |
# File 'lib/algolia/search_index.rb', line 16 def initialize(name, transporter, config) @name = name @transporter = transporter @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/algolia/search_index.rb', line 8 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/algolia/search_index.rb', line 8 def name @name end |
#transporter ⇒ Object (readonly)
Returns the value of attribute transporter.
8 9 10 |
# File 'lib/algolia/search_index.rb', line 8 def transporter @transporter end |
Class Method Details
.get_object_position(objects, object_id) ⇒ Integer
Retrieve the given object position in a set of results.
144 145 146 147 |
# File 'lib/algolia/search_index.rb', line 144 def self.get_object_position(objects, object_id) hits = get_option(objects, 'hits') hits.find_index { |hit| get_option(hit, 'objectID') == object_id } || -1 end |
Instance Method Details
#batch(requests, opts = {}) ⇒ IndexingResponse
Send a batch request
409 410 411 412 413 |
# File 'lib/algolia/search_index.rb', line 409 def batch(requests, opts = {}) response = raw_batch(requests, opts) IndexingResponse.new(self, response) end |
#batch!(requests, opts = {}) ⇒ IndexingResponse
Send a batch request and wait for operation to complete
422 423 424 425 |
# File 'lib/algolia/search_index.rb', line 422 def batch!(requests, opts = {}) response = batch(requests, opts) response.wait(opts) end |
#browse_objects(opts = {}, &block) ⇒ Enumerator, ObjectIterator
Browse all index content
770 771 772 773 774 775 776 |
# File 'lib/algolia/search_index.rb', line 770 def browse_objects(opts = {}, &block) if block_given? ObjectIterator.new(@transporter, @name, opts).each(&block) else ObjectIterator.new(@transporter, @name, opts) end end |
#browse_rules(opts = {}, &block) ⇒ Enumerator, RuleIterator
Browse all rules
784 785 786 787 788 789 790 |
# File 'lib/algolia/search_index.rb', line 784 def browse_rules(opts = {}, &block) if block_given? RuleIterator.new(@transporter, @name, opts).each(&block) else RuleIterator.new(@transporter, @name, opts) end end |
#browse_synonyms(opts = {}, &block) ⇒ Enumerator, SynonymIterator
Browse all synonyms
798 799 800 801 802 803 804 |
# File 'lib/algolia/search_index.rb', line 798 def browse_synonyms(opts = {}, &block) if block_given? SynonymIterator.new(@transporter, @name, opts).each(&block) else SynonymIterator.new(@transporter, @name, opts) end end |
#clear_objects(opts = {}) ⇒ Object
Delete the index content
58 59 60 61 62 |
# File 'lib/algolia/search_index.rb', line 58 def clear_objects(opts = {}) response = @transporter.write(:POST, path_encode('/1/indexes/%s/clear', @name), {}, opts) IndexingResponse.new(self, response) end |
#clear_objects!(opts = {}) ⇒ Object
Delete the index content and wait for operation to complete
68 69 70 71 |
# File 'lib/algolia/search_index.rb', line 68 def clear_objects!(opts = {}) response = clear_objects(opts) response.wait(opts) end |
#clear_rules(opts = {}) ⇒ IndexingResponse
Delete all Rules in the index
526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/algolia/search_index.rb', line 526 def clear_rules(opts = {}) forward_to_replicas = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end response = @transporter.write(:POST, path_encode('1/indexes/%s/rules/clear', @name) + handle_params({ forwardToReplicas: forward_to_replicas }), '', ) IndexingResponse.new(self, response) end |
#clear_rules!(opts = {}) ⇒ IndexingResponse
Delete all Rules in the index and wait for operation to complete
546 547 548 549 |
# File 'lib/algolia/search_index.rb', line 546 def clear_rules!(opts = {}) response = clear_rules(opts) response.wait(opts) end |
#clear_synonyms(opts = {}) ⇒ IndexingResponse
Delete all synonyms from the index
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/algolia/search_index.rb', line 696 def clear_synonyms(opts = {}) forward_to_replicas = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end response = @transporter.write( :POST, path_encode('1/indexes/%s/synonyms/clear', @name) + handle_params({ forwardToReplicas: forward_to_replicas }), '', ) IndexingResponse.new(self, response) end |
#clear_synonyms!(opts = {}) ⇒ IndexingResponse
Delete all synonyms from the index and wait for operation to complete
720 721 722 723 |
# File 'lib/algolia/search_index.rb', line 720 def clear_synonyms!(opts = {}) response = clear_synonyms(opts) response.wait(opts) end |
#copy_to(name, opts = {}) ⇒ IndexingResponse
Copy the current index to the given destination name
156 157 158 159 160 |
# File 'lib/algolia/search_index.rb', line 156 def copy_to(name, opts = {}) response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', @name), { operation: 'copy', destination: name }, opts) IndexingResponse.new(self, response) end |
#delete(opts = {}) ⇒ Object
Delete an existing index
77 78 79 80 81 |
# File 'lib/algolia/search_index.rb', line 77 def delete(opts = {}) response = @transporter.write(:DELETE, path_encode('/1/indexes/%s', @name), opts) IndexingResponse.new(self, response) end |
#delete!(opts = {}) ⇒ Object
Delete an existing index and wait for operation to complete
87 88 89 90 |
# File 'lib/algolia/search_index.rb', line 87 def delete!(opts = {}) response = delete(opts) response.wait(opts) end |
#delete_by(filters, opts = {}) ⇒ IndexingResponse
Delete all records matching the query
383 384 385 386 387 |
# File 'lib/algolia/search_index.rb', line 383 def delete_by(filters, opts = {}) response = @transporter.write(:POST, path_encode('/1/indexes/%s/deleteByQuery', @name), filters, opts) IndexingResponse.new(self, response) end |
#delete_by!(filters, opts = {}) ⇒ IndexingResponse
Delete all records matching the query and wait for operation to complete
396 397 398 399 400 |
# File 'lib/algolia/search_index.rb', line 396 def delete_by!(filters, opts = {}) response = delete_by(filters, opts) response.wait(opts) end |
#delete_object(object_id, opts = {}) ⇒ IndexingResponse
Delete an existing object from an index
333 334 335 |
# File 'lib/algolia/search_index.rb', line 333 def delete_object(object_id, opts = {}) delete_objects([object_id], opts) end |
#delete_object!(object_id, opts = {}) ⇒ IndexingResponse
Delete an existing object from an index and wait for operation to complete
344 345 346 347 |
# File 'lib/algolia/search_index.rb', line 344 def delete_object!(object_id, opts = {}) response = delete_objects([object_id], opts) response.wait(opts) end |
#delete_objects(object_ids, opts = {}) ⇒ IndexingResponse
Delete several existing objects from an index
356 357 358 359 360 361 362 |
# File 'lib/algolia/search_index.rb', line 356 def delete_objects(object_ids, opts = {}) objects = object_ids.map do |object_id| { objectID: object_id } end IndexingResponse.new(self, raw_batch(chunk('deleteObject', objects), opts)) end |
#delete_objects!(object_ids, opts = {}) ⇒ IndexingResponse
Delete several existing objects from an index and wait for operation to complete
371 372 373 374 |
# File 'lib/algolia/search_index.rb', line 371 def delete_objects!(object_ids, opts = {}) response = delete_objects(object_ids, opts) response.wait(opts) end |
#delete_rule(object_id, opts = {}) ⇒ IndexingResponse
Delete the Rule with the specified objectID
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/algolia/search_index.rb', line 558 def delete_rule(object_id, opts = {}) forward_to_replicas = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end response = @transporter.write( :DELETE, path_encode('1/indexes/%s/rules/%s', @name, object_id) + handle_params({ forwardToReplicas: forward_to_replicas }), '', ) IndexingResponse.new(self, response) end |
#delete_rule!(object_id, opts = {}) ⇒ IndexingResponse
Delete the Rule with the specified objectID and wait for operation to complete
584 585 586 587 |
# File 'lib/algolia/search_index.rb', line 584 def delete_rule!(object_id, opts = {}) response = delete_rule(object_id, opts) response.wait(opts) end |
#delete_synonym(object_id, opts = {}) ⇒ IndexingResponse
Delete a single synonyms set, identified by the given objectID
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 |
# File 'lib/algolia/search_index.rb', line 731 def delete_synonym(object_id, opts = {}) forward_to_replicas = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end response = @transporter.write( :DELETE, path_encode('1/indexes/%s/synonyms/%s', @name, object_id) + handle_params({ forwardToReplicas: forward_to_replicas }), '', ) IndexingResponse.new(self, response) end |
#delete_synonym!(object_id, opts = {}) ⇒ IndexingResponse
Delete a single synonyms set, identified by the given objectID and wait for operation to complete
755 756 757 758 |
# File 'lib/algolia/search_index.rb', line 755 def delete_synonym!(object_id, opts = {}) response = delete_synonym(object_id, opts) response.wait(opts) end |
#exists ⇒ Boolean Also known as: exists?
Checks if the current index exists
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
# File 'lib/algolia/search_index.rb', line 1028 def exists begin get_settings rescue AlgoliaHttpError => e if e.code == 404 return false end raise e end true end |
#find_object(callback, opts = {}) ⇒ Hash|AlgoliaHttpError
Find object by the given condition.
Options can be passed in request_options body:
- query (string): pass a query
- paginate (bool): choose if you want to iterate through all the
documents (true) or only the first page (false). Default is true. The function takes a block to filter the results from search query Usage example:
index.find_object({'query' => '', 'paginate' => true}) {|obj| obj.key?('company') and obj['company'] == 'Apple'}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/algolia/search_index.rb', line 107 def find_object(callback, opts = {}) = symbolize_hash(opts) paginate = true page = 0 query = .delete(:query) || '' paginate = .delete(:paginate) if .has_key?(:paginate) has_next_page = true while has_next_page [:page] = page res = symbolize_hash(search(query, )) res[:hits].each_with_index do |hit, i| if callback.call(hit) return { object: hit, position: i, page: page } end end has_next_page = page + 1 < res[:nbPages] raise AlgoliaHttpError.new(404, 'Object not found') unless paginate && has_next_page page += 1 end end |
#get_object(object_id, opts = {}) ⇒ Hash
Retrieve one object from the index
186 187 188 |
# File 'lib/algolia/search_index.rb', line 186 def get_object(object_id, opts = {}) @transporter.read(:GET, path_encode('/1/indexes/%s/%s', @name, object_id), {}, opts) end |
#get_objects(object_ids, opts = {}) ⇒ Hash
Retrieve one or more objects in a single API call
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/algolia/search_index.rb', line 197 def get_objects(object_ids, opts = {}) = symbolize_hash(opts) attributes_to_retrieve = get_option(, 'attributesToRetrieve') .delete(:attributesToRetrieve) requests = [] object_ids.each do |object_id| request = { indexName: @name, objectID: object_id.to_s } if attributes_to_retrieve request[:attributesToRetrieve] = attributes_to_retrieve end requests.push(request) end @transporter.read(:POST, '/1/indexes/*/objects', { 'requests': requests }, opts) end |
#get_rule(object_id, opts = {}) ⇒ Hash
Retrieve the Rule with the specified objectID
438 439 440 |
# File 'lib/algolia/search_index.rb', line 438 def get_rule(object_id, opts = {}) @transporter.read(:GET, path_encode('/1/indexes/%s/rules/%s', @name, object_id), {}, opts) end |
#get_settings(opts = {}) ⇒ Hash
Retrieve index settings
976 977 978 979 980 981 982 983 984 |
# File 'lib/algolia/search_index.rb', line 976 def get_settings(opts = {}) opts_default = { getVersion: 2 } opts = opts_default.merge(opts) response = @transporter.read(:GET, path_encode('/1/indexes/%s/settings', @name), {}, opts) deserialize_settings(response, @config.symbolize_keys) end |
#get_synonym(object_id, opts = {}) ⇒ Hash
Fetch a synonym object identified by its objectID
600 601 602 |
# File 'lib/algolia/search_index.rb', line 600 def get_synonym(object_id, opts = {}) @transporter.read(:GET, path_encode('/1/indexes/%s/synonyms/%s', @name, object_id), {}, opts) end |
#get_task_status(task_id, opts = {}) ⇒ Object
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.
49 50 51 52 |
# File 'lib/algolia/search_index.rb', line 49 def get_task_status(task_id, opts = {}) res = @transporter.read(:GET, path_encode('/1/indexes/%s/task/%s', @name, task_id), {}, opts) get_option(res, 'status') end |
#move_to(name, opts = {}) ⇒ IndexingResponse
Move the current index to the given destination name
169 170 171 172 173 |
# File 'lib/algolia/search_index.rb', line 169 def move_to(name, opts = {}) response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', @name), { operation: 'move', destination: name }, opts) IndexingResponse.new(self, response) end |
#partial_update_object(object, opts = {}) ⇒ IndexingResponse
Partially update an object
276 277 278 |
# File 'lib/algolia/search_index.rb', line 276 def partial_update_object(object, opts = {}) partial_update_objects([object], opts) end |
#partial_update_object!(object, opts = {}) ⇒ IndexingResponse
Partially update an object and wait for operation to complete
287 288 289 290 |
# File 'lib/algolia/search_index.rb', line 287 def partial_update_object!(object, opts = {}) response = partial_update_objects([object], opts) response.wait(opts) end |
#partial_update_objects(objects, opts = {}) ⇒ IndexingResponse
Partially update several objects
299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/algolia/search_index.rb', line 299 def partial_update_objects(objects, opts = {}) generate_object_id = false = symbolize_hash(opts) if get_option(, 'createIfNotExists') generate_object_id = true .delete(:createIfNotExists) end if generate_object_id IndexingResponse.new(self, raw_batch(chunk('partialUpdateObject', objects), )) else IndexingResponse.new(self, raw_batch(chunk('partialUpdateObjectNoCreate', objects), )) end end |
#partial_update_objects!(objects, opts = {}) ⇒ IndexingResponse
Partially update several objects and wait for operation to complete
321 322 323 324 |
# File 'lib/algolia/search_index.rb', line 321 def partial_update_objects!(objects, opts = {}) response = partial_update_objects(objects, opts) response.wait(opts) end |
#replace_all_objects(objects, opts = {}) ⇒ Enumerator, SynonymIterator
Replace all objects in the index
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
# File 'lib/algolia/search_index.rb', line 817 def replace_all_objects(objects, opts = {}) safe = false = symbolize_hash(opts) if [:safe] safe = true .delete(:safe) end tmp_index_name = @name + '_tmp_' + rand(10000000).to_s copy_to_response = copy_to(tmp_index_name, .merge({ scope: %w(settings synonyms rules) })) if safe copy_to_response.wait end # TODO: consider create a new client with state of retry is shared tmp_client = Algolia::Search::Client.new(@config) tmp_index = tmp_client.init_index(tmp_index_name) save_objects_response = tmp_index.save_objects(objects, ) if safe save_objects_response.wait end move_to_response = tmp_index.move_to(@name) if safe move_to_response.wait end end |
#replace_all_objects!(objects, opts = {}) ⇒ Enumerator, SynonymIterator
Replace all objects in the index and wait for the operation to complete
855 856 857 |
# File 'lib/algolia/search_index.rb', line 855 def replace_all_objects!(objects, opts = {}) replace_all_objects(objects, opts.merge(safe: true)) end |
#replace_all_rules(rules, opts = {}) ⇒ Array, IndexingResponse
Replace all rules in the index
866 867 868 869 870 871 |
# File 'lib/algolia/search_index.rb', line 866 def replace_all_rules(rules, opts = {}) = symbolize_hash(opts) [:clearExistingRules] = true save_rules(rules, ) end |
#replace_all_rules!(rules, opts = {}) ⇒ Array, IndexingResponse
Replace all rules in the index and wait for the operation to complete
880 881 882 883 884 885 |
# File 'lib/algolia/search_index.rb', line 880 def replace_all_rules!(rules, opts = {}) = symbolize_hash(opts) [:clearExistingRules] = true save_rules!(rules, ) end |
#replace_all_synonyms(synonyms, opts = {}) ⇒ Array, IndexingResponse
Replace all synonyms in the index
894 895 896 897 898 899 |
# File 'lib/algolia/search_index.rb', line 894 def replace_all_synonyms(synonyms, opts = {}) = symbolize_hash(opts) [:replaceExistingSynonyms] = true save_synonyms(synonyms, ) end |
#replace_all_synonyms!(synonyms, opts = {}) ⇒ Array, IndexingResponse
Replace all synonyms in the index and wait for the operation to complete
908 909 910 911 912 913 |
# File 'lib/algolia/search_index.rb', line 908 def replace_all_synonyms!(synonyms, opts = {}) = symbolize_hash(opts) [:replaceExistingSynonyms] = true save_synonyms!(synonyms, ) end |
#save_object(object, opts = {}) ⇒ IndexingResponse
Add an object to the index
223 224 225 |
# File 'lib/algolia/search_index.rb', line 223 def save_object(object, opts = {}) save_objects([object], opts) end |
#save_object!(object, opts = {}) ⇒ IndexingResponse
Add an object to the index and wait for operation to complete
234 235 236 237 |
# File 'lib/algolia/search_index.rb', line 234 def save_object!(object, opts = {}) response = save_objects([object], opts) response.wait(opts) end |
#save_objects(objects, opts = {}) ⇒ IndexingResponse
Add several objects to the index
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/algolia/search_index.rb', line 246 def save_objects(objects, opts = {}) = symbolize_hash(opts) generate_object_id = [:auto_generate_object_id_if_not_exist] || false .delete(:auto_generate_object_id_if_not_exist) if generate_object_id IndexingResponse.new(self, raw_batch(chunk('addObject', objects), )) else IndexingResponse.new(self, raw_batch(chunk('updateObject', objects, true), )) end end |
#save_objects!(objects, opts = {}) ⇒ IndexingResponse
Add several objects to the index and wait for operation to complete
264 265 266 267 |
# File 'lib/algolia/search_index.rb', line 264 def save_objects!(objects, opts = {}) response = save_objects(objects, opts) response.wait(opts) end |
#save_rule(rule, opts = {}) ⇒ Array, IndexingResponse
Create or update a rule
449 450 451 |
# File 'lib/algolia/search_index.rb', line 449 def save_rule(rule, opts = {}) save_rules([rule], opts) end |
#save_rule!(rule, opts = {}) ⇒ Array, IndexingResponse
Create or update a rule and wait for operation to complete
460 461 462 463 |
# File 'lib/algolia/search_index.rb', line 460 def save_rule!(rule, opts = {}) response = save_rules([rule], opts) response.wait(opts) end |
#save_rules(rules, opts = {}) ⇒ Array, IndexingResponse
Create or update rules
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/algolia/search_index.rb', line 472 def save_rules(rules, opts = {}) if rules.is_a?(RuleIterator) iterated = [] rules.each do |rule| iterated.push(rule) end rules = iterated end if rules.empty? return [] end forward_to_replicas = false clear_existing_rules = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end if [:clearExistingRules] clear_existing_rules = true .delete(:clearExistingRules) end rules.each do |rule| get_object_id(rule) end response = @transporter.write(:POST, path_encode('/1/indexes/%s/rules/batch', @name) + handle_params({ forwardToReplicas: forward_to_replicas, clearExistingRules: clear_existing_rules }), rules, ) IndexingResponse.new(self, response) end |
#save_rules!(rules, opts = {}) ⇒ Array, IndexingResponse
Create or update rules and wait for operation to complete
515 516 517 518 |
# File 'lib/algolia/search_index.rb', line 515 def save_rules!(rules, opts = {}) response = save_rules(rules, opts) response.wait(opts) end |
#save_synonym(synonym, opts = {}) ⇒ Array, IndexingResponse
Create a new synonym object or update the existing synonym object with the given object ID
611 612 613 |
# File 'lib/algolia/search_index.rb', line 611 def save_synonym(synonym, opts = {}) save_synonyms([synonym], opts) end |
#save_synonym!(synonym, opts = {}) ⇒ Array, IndexingResponse
Create a new synonym object or update the existing synonym object with the given object ID and wait for operation to finish
623 624 625 626 |
# File 'lib/algolia/search_index.rb', line 623 def save_synonym!(synonym, opts = {}) response = save_synonyms([synonym], opts) response.wait(opts) end |
#save_synonyms(synonyms, opts = {}) ⇒ Array, IndexingResponse
Create/update multiple synonym objects at once, potentially replacing the entire list of synonyms if replaceExistingSynonyms is true
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/algolia/search_index.rb', line 636 def save_synonyms(synonyms, opts = {}) if synonyms.is_a?(SynonymIterator) iterated = [] synonyms.each do |synonym| iterated.push(synonym) end synonyms = iterated end if synonyms.empty? return [] end synonyms.each do |synonym| get_object_id(synonym) end forward_to_replicas = false replace_existing_synonyms = false = symbolize_hash(opts) if [:forwardToReplicas] forward_to_replicas = true .delete(:forwardToReplicas) end if [:replaceExistingSynonyms] replace_existing_synonyms = true .delete(:replaceExistingSynonyms) end response = @transporter.write( :POST, path_encode('/1/indexes/%s/synonyms/batch', @name) + handle_params({ forwardToReplicas: forward_to_replicas, replaceExistingSynonyms: replace_existing_synonyms }), synonyms, ) IndexingResponse.new(self, response) end |
#save_synonyms!(synonyms, opts = {}) ⇒ Array, IndexingResponse
Create/update multiple synonym objects at once, potentially replacing the entire list of synonyms if replaceExistingSynonyms is true and wait for operation to complete
685 686 687 688 |
# File 'lib/algolia/search_index.rb', line 685 def save_synonyms!(synonyms, opts = {}) response = save_synonyms(synonyms, opts) response.wait(opts) end |
#search(query, opts = {}) ⇒ Hash
Perform a search on the index
926 927 928 |
# File 'lib/algolia/search_index.rb', line 926 def search(query, opts = {}) @transporter.read(:POST, path_encode('/1/indexes/%s/query', @name), { 'query': query.to_s }, opts) end |
#search_for_facet_values(facet_name, facet_query, opts = {}) ⇒ Hash
Search for values of a given facet, optionally restricting the returned values to those contained in objects matching other search criteria
939 940 941 942 |
# File 'lib/algolia/search_index.rb', line 939 def search_for_facet_values(facet_name, facet_query, opts = {}) @transporter.read(:POST, path_encode('/1/indexes/%s/facets/%s/query', @name, facet_name), { 'facetQuery': facet_query }, opts) end |
#search_rules(query, opts = {}) ⇒ Hash
Search or browse all rules, optionally filtering them by type
962 963 964 |
# File 'lib/algolia/search_index.rb', line 962 def search_rules(query, opts = {}) @transporter.read(:POST, path_encode('/1/indexes/%s/rules/search', @name), { query: query.to_s }, opts) end |
#search_synonyms(query, opts = {}) ⇒ Hash
Search or browse all synonyms, optionally filtering them by type
951 952 953 |
# File 'lib/algolia/search_index.rb', line 951 def search_synonyms(query, opts = {}) @transporter.read(:POST, path_encode('/1/indexes/%s/synonyms/search', @name), { query: query.to_s }, opts) end |
#set_settings(settings, opts = {}) ⇒ IndexingResponse
Update some index settings. Only specified settings are overridden
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
# File 'lib/algolia/search_index.rb', line 993 def set_settings(settings, opts = {}) = symbolize_hash(opts) forward_to_replicas = .delete(:forwardToReplicas) || false response = @transporter.write( :PUT, path_encode('/1/indexes/%s/settings', @name) + handle_params({ forwardToReplicas: forward_to_replicas }), settings, ) IndexingResponse.new(self, response) end |
#set_settings!(settings, opts = {}) ⇒ IndexingResponse
Update some index settings and wait for operation to complete. Only specified settings are overridden
1015 1016 1017 1018 |
# File 'lib/algolia/search_index.rb', line 1015 def set_settings!(settings, opts = {}) response = set_settings(settings, opts) response.wait(opts) end |
#wait_task(task_id, time_before_retry = Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {}) ⇒ 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.
33 34 35 36 37 38 39 40 41 |
# File 'lib/algolia/search_index.rb', line 33 def wait_task(task_id, time_before_retry = Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {}) loop do status = get_task_status(task_id, opts) if status == 'published' return end sleep(time_before_retry.to_f / 1000) end end |