Class: Algolia::Search::Index

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

Overview

Class Index

Constant Summary

Constants included from CallType

CallType::READ, CallType::WRITE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

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

Constructor Details

#initialize(name, transporter, config) ⇒ Index

Initialize an index

Parameters:

  • name (String)

    name of the index

  • transporter (Object)

    transport object used for the connection

  • config (Config)

    a Config object which contains your APP_ID and API_KEY



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

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/algolia/search_index.rb', line 8

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/algolia/search_index.rb', line 8

def name
  @name
end

#transporterObject (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.

Parameters:

  • objects (Array)

    the result set to browse

  • object_id (String)

    the object to look for

Returns:

  • (Integer)

    position of the object, or -1 if it's not in the array



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

Parameters:

  • requests (Hash)

    hash containing the requests to batch

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • requests (Hash)

    hash containing the requests to batch

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query



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

Parameters:

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

    contains extra parameters to send with your query



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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
  request_options     = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end

  response = @transporter.write(:POST, path_encode('1/indexes/%s/rules/clear', @name) + handle_params({ forwardToReplicas: forward_to_replicas }), '', request_options)

  IndexingResponse.new(self, response)
end

#clear_rules!(opts = {}) ⇒ IndexingResponse

Delete all Rules in the index and wait for operation to complete

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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
  request_options     = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end
  response = @transporter.write(
    :POST,
    path_encode('1/indexes/%s/synonyms/clear', @name) + handle_params({ forwardToReplicas: forward_to_replicas }),
    '',
    request_options
  )

  IndexingResponse.new(self, response)
end

#clear_synonyms!(opts = {}) ⇒ IndexingResponse

Delete all synonyms from the index and wait for operation to complete

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • name (String)

    destination index name

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query



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

Parameters:

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

    contains extra parameters to send with your query



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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
  request_options     = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end

  response = @transporter.write(
    :DELETE,
    path_encode('1/indexes/%s/rules/%s', @name, object_id) + handle_params({ forwardToReplicas: forward_to_replicas }),
    '',
    request_options
  )

  IndexingResponse.new(self, response)
end

#delete_rule!(object_id, opts = {}) ⇒ IndexingResponse

Delete the Rule with the specified objectID and wait for operation to complete

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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
  request_options     = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end
  response = @transporter.write(
    :DELETE,
    path_encode('1/indexes/%s/synonyms/%s', @name, object_id) + handle_params({ forwardToReplicas: forward_to_replicas }),
    '',
    request_options
  )

  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

Parameters:

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

    contains extra parameters to send with your query

Returns:



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

#existsBoolean Also known as: exists?

Checks if the current index exists

Returns:

  • (Boolean)


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'}

Parameters:

  • callback (Lambda)

    contains extra parameters to send with your query

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

    contains extra parameters to send with your query

Returns:

  • (Hash|AlgoliaHttpError)

    the matching object and its position in the result set



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 = {})
  request_options = symbolize_hash(opts)
  paginate        = true
  page            = 0

  query    = request_options.delete(:query) || ''
  paginate = request_options.delete(:paginate) if request_options.has_key?(:paginate)

  has_next_page = true
  while has_next_page
    request_options[:page] = page
    res                    = symbolize_hash(search(query, request_options))

    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

Parameters:

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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 = {})
  request_options        = symbolize_hash(opts)
  attributes_to_retrieve = get_option(request_options, 'attributesToRetrieve')
  request_options.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

Parameters:

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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.

Parameters:

  • task_id (Integer)

    the id of the task returned by server

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

    contains extra parameters to send with your query



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

Parameters:

  • name (String)

    destination index name

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • object (String)

    object ID to partially update

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • object (String)

    object ID to partially update

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • objects (Array)

    array of objectIDs to partially update

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

    contains extra parameters to send with your query

Returns:



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
  request_options    = symbolize_hash(opts)
  if get_option(request_options, 'createIfNotExists')
    generate_object_id = true
    request_options.delete(:createIfNotExists)
  end

  if generate_object_id
    IndexingResponse.new(self, raw_batch(chunk('partialUpdateObject', objects), request_options))
  else
    IndexingResponse.new(self, raw_batch(chunk('partialUpdateObjectNoCreate', objects), request_options))
  end
end

#partial_update_objects!(objects, opts = {}) ⇒ IndexingResponse

Partially update several objects and wait for operation to complete

Parameters:

  • objects (Array)

    array of objectIDs to partially update

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • objects (Array)

    Array of objects

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

    contains extra parameters to send with your query

Returns:



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
  request_options = symbolize_hash(opts)
  if request_options[:safe]
    safe = true
    request_options.delete(:safe)
  end

  tmp_index_name   = @name + '_tmp_' + rand(10000000).to_s
  copy_to_response = copy_to(tmp_index_name, request_options.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, request_options)

  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

Parameters:

  • objects (Array)

    Array of objects

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • rules (Array)

    Array of rules

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

    contains extra parameters to send with your query

Returns:



866
867
868
869
870
871
# File 'lib/algolia/search_index.rb', line 866

def replace_all_rules(rules, opts = {})
  request_options                      = symbolize_hash(opts)
  request_options[:clearExistingRules] = true

  save_rules(rules, request_options)
end

#replace_all_rules!(rules, opts = {}) ⇒ Array, IndexingResponse

Replace all rules in the index and wait for the operation to complete

Parameters:

  • rules (Array)

    Array of rules

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

    contains extra parameters to send with your query

Returns:



880
881
882
883
884
885
# File 'lib/algolia/search_index.rb', line 880

def replace_all_rules!(rules, opts = {})
  request_options                      = symbolize_hash(opts)
  request_options[:clearExistingRules] = true

  save_rules!(rules, request_options)
end

#replace_all_synonyms(synonyms, opts = {}) ⇒ Array, IndexingResponse

Replace all synonyms in the index

Parameters:

  • synonyms (Array)

    Array of synonyms

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

    contains extra parameters to send with your query

Returns:



894
895
896
897
898
899
# File 'lib/algolia/search_index.rb', line 894

def replace_all_synonyms(synonyms, opts = {})
  request_options                           = symbolize_hash(opts)
  request_options[:replaceExistingSynonyms] = true

  save_synonyms(synonyms, request_options)
end

#replace_all_synonyms!(synonyms, opts = {}) ⇒ Array, IndexingResponse

Replace all synonyms in the index and wait for the operation to complete

Parameters:

  • synonyms (Array)

    Array of synonyms

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

    contains extra parameters to send with your query

Returns:



908
909
910
911
912
913
# File 'lib/algolia/search_index.rb', line 908

def replace_all_synonyms!(synonyms, opts = {})
  request_options                           = symbolize_hash(opts)
  request_options[:replaceExistingSynonyms] = true

  save_synonyms!(synonyms, request_options)
end

#save_object(object, opts = {}) ⇒ IndexingResponse

Add an object to the index

Parameters:

  • object (Hash)

    the object to save

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • object (Hash)

    the object to save

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • objects (Array)

    the objects to save

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

    contains extra parameters to send with your query

Returns:



246
247
248
249
250
251
252
253
254
255
# File 'lib/algolia/search_index.rb', line 246

def save_objects(objects, opts = {})
  request_options    = symbolize_hash(opts)
  generate_object_id = request_options[:auto_generate_object_id_if_not_exist] || false
  request_options.delete(:auto_generate_object_id_if_not_exist)
  if generate_object_id
    IndexingResponse.new(self, raw_batch(chunk('addObject', objects), request_options))
  else
    IndexingResponse.new(self, raw_batch(chunk('updateObject', objects, true), request_options))
  end
end

#save_objects!(objects, opts = {}) ⇒ IndexingResponse

Add several objects to the index and wait for operation to complete

Parameters:

  • objects (Array)

    the objects to save

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • rule (Hash)

    a hash containing a rule objectID and different conditions/consequences

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • rule (Hash)

    a hash containing a rule objectID and different conditions/consequences

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • rules (Array)

    an array of hashes containing a rule objectID and different conditions/consequences

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

    contains extra parameters to send with your query

Returns:



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
  request_options      = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end

  if request_options[:clearExistingRules]
    clear_existing_rules = true
    request_options.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, request_options)

  IndexingResponse.new(self, response)
end

#save_rules!(rules, opts = {}) ⇒ Array, IndexingResponse

Create or update rules and wait for operation to complete

Parameters:

  • rules (Array)

    an array of hashes containing a rule objectID and different conditions/consequences

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • synonym (Hash)

    Synonym object

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • synonym (Hash)

    Synonym object

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • synonyms (Array)

    Array of Synonym objects

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

    contains extra parameters to send with your query

Returns:



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

  request_options = symbolize_hash(opts)

  if request_options[:forwardToReplicas]
    forward_to_replicas = true
    request_options.delete(:forwardToReplicas)
  end

  if request_options[:replaceExistingSynonyms]
    replace_existing_synonyms = true
    request_options.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,
    request_options
  )

  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

Parameters:

  • synonyms (Array)

    Array of Synonym objects

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

    contains extra parameters to send with your query

Returns:



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

Parameters:

  • query

    the full text query

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

  • facet_name (String)
  • facet_query (String)
  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

  • query (String)

    Search for specific rules matching this string

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

  • query (String)

    Search for specific synonyms matching this string

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

    contains extra parameters to send with your query

Returns:

  • (Hash)


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

Parameters:

  • settings (Hash)

    the settings to update

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

    contains extra parameters to send with your query

Returns:



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 = {})
  request_options      = symbolize_hash(opts)
  forward_to_replicas  = request_options.delete(:forwardToReplicas) || false

  response = @transporter.write(
    :PUT,
    path_encode('/1/indexes/%s/settings', @name) + handle_params({ forwardToReplicas: forward_to_replicas }),
    settings,
    request_options
  )

  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

Parameters:

  • settings (Hash)

    the settings to update

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

    contains extra parameters to send with your query

Returns:



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.

Parameters:

  • task_id

    the id of the task returned by server

  • time_before_retry (defaults to: Defaults::WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY)

    the time in milliseconds before retry (default = 100ms)

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

    contains extra parameters to send with your query



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