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, logger = nil) ⇒ Index

Initialize an index



17
18
19
20
21
22
# File 'lib/algolia/search_index.rb', line 17

def initialize(name, transporter, config, logger = nil)
  @name        = name
  @transporter = transporter
  @config      = config
  @logger      = logger || LoggerHelper.create
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

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
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.



146
147
148
149
# File 'lib/algolia/search_index.rb', line 146

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



411
412
413
414
415
# File 'lib/algolia/search_index.rb', line 411

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



424
425
426
427
# File 'lib/algolia/search_index.rb', line 424

def batch!(requests, opts = {})
  response     = batch(requests, opts)
  response.wait(opts)
end

#browse_objects(opts = {}, &block) ⇒ Enumerator, ObjectIterator

Browse all index content



772
773
774
775
776
777
778
# File 'lib/algolia/search_index.rb', line 772

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



786
787
788
789
790
791
792
# File 'lib/algolia/search_index.rb', line 786

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



800
801
802
803
804
805
806
# File 'lib/algolia/search_index.rb', line 800

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



60
61
62
63
64
# File 'lib/algolia/search_index.rb', line 60

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



70
71
72
73
# File 'lib/algolia/search_index.rb', line 70

def clear_objects!(opts = {})
  response = clear_objects(opts)
  response.wait(opts)
end

#clear_rules(opts = {}) ⇒ IndexingResponse

Delete all Rules in the index



528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/algolia/search_index.rb', line 528

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



548
549
550
551
# File 'lib/algolia/search_index.rb', line 548

def clear_rules!(opts = {})
  response     = clear_rules(opts)
  response.wait(opts)
end

#clear_synonyms(opts = {}) ⇒ IndexingResponse

Delete all synonyms from the index



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/algolia/search_index.rb', line 698

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



722
723
724
725
# File 'lib/algolia/search_index.rb', line 722

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



158
159
160
161
162
# File 'lib/algolia/search_index.rb', line 158

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



79
80
81
82
83
# File 'lib/algolia/search_index.rb', line 79

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



89
90
91
92
# File 'lib/algolia/search_index.rb', line 89

def delete!(opts = {})
  response = delete(opts)
  response.wait(opts)
end

#delete_by(filters, opts = {}) ⇒ IndexingResponse

Delete all records matching the query



385
386
387
388
389
# File 'lib/algolia/search_index.rb', line 385

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



398
399
400
401
402
# File 'lib/algolia/search_index.rb', line 398

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



335
336
337
# File 'lib/algolia/search_index.rb', line 335

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



346
347
348
349
# File 'lib/algolia/search_index.rb', line 346

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



358
359
360
361
362
363
364
# File 'lib/algolia/search_index.rb', line 358

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



373
374
375
376
# File 'lib/algolia/search_index.rb', line 373

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



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/algolia/search_index.rb', line 560

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



586
587
588
589
# File 'lib/algolia/search_index.rb', line 586

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



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/algolia/search_index.rb', line 733

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



757
758
759
760
# File 'lib/algolia/search_index.rb', line 757

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



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/algolia/search_index.rb', line 1030

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


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
136
137
# File 'lib/algolia/search_index.rb', line 109

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



188
189
190
# File 'lib/algolia/search_index.rb', line 188

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/algolia/search_index.rb', line 199

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



440
441
442
# File 'lib/algolia/search_index.rb', line 440

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



978
979
980
981
982
983
984
985
986
# File 'lib/algolia/search_index.rb', line 978

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



602
603
604
# File 'lib/algolia/search_index.rb', line 602

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.



51
52
53
54
# File 'lib/algolia/search_index.rb', line 51

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



171
172
173
174
175
# File 'lib/algolia/search_index.rb', line 171

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



278
279
280
# File 'lib/algolia/search_index.rb', line 278

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



289
290
291
292
# File 'lib/algolia/search_index.rb', line 289

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



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/algolia/search_index.rb', line 301

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
  end
  request_options.delete(:createIfNotExists)

  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



323
324
325
326
# File 'lib/algolia/search_index.rb', line 323

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



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
847
848
# File 'lib/algolia/search_index.rb', line 819

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, { logger: logger })
  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



857
858
859
# File 'lib/algolia/search_index.rb', line 857

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



868
869
870
871
872
873
# File 'lib/algolia/search_index.rb', line 868

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



882
883
884
885
886
887
# File 'lib/algolia/search_index.rb', line 882

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



896
897
898
899
900
901
# File 'lib/algolia/search_index.rb', line 896

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



910
911
912
913
914
915
# File 'lib/algolia/search_index.rb', line 910

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



225
226
227
# File 'lib/algolia/search_index.rb', line 225

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



236
237
238
239
# File 'lib/algolia/search_index.rb', line 236

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



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

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



266
267
268
269
# File 'lib/algolia/search_index.rb', line 266

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



451
452
453
# File 'lib/algolia/search_index.rb', line 451

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



462
463
464
465
# File 'lib/algolia/search_index.rb', line 462

def save_rule!(rule, opts = {})
  response = save_rules([rule], opts)
  response.wait(opts)
end

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

Create or update rules



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
507
508
# File 'lib/algolia/search_index.rb', line 474

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



517
518
519
520
# File 'lib/algolia/search_index.rb', line 517

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



613
614
615
# File 'lib/algolia/search_index.rb', line 613

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



625
626
627
628
# File 'lib/algolia/search_index.rb', line 625

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



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
676
677
# File 'lib/algolia/search_index.rb', line 638

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



687
688
689
690
# File 'lib/algolia/search_index.rb', line 687

def save_synonyms!(synonyms, opts = {})
  response = save_synonyms(synonyms, opts)
  response.wait(opts)
end

#search(query, opts = {}) ⇒ Hash

Perform a search on the index



928
929
930
# File 'lib/algolia/search_index.rb', line 928

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



941
942
943
944
# File 'lib/algolia/search_index.rb', line 941

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



964
965
966
# File 'lib/algolia/search_index.rb', line 964

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



953
954
955
# File 'lib/algolia/search_index.rb', line 953

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



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/algolia/search_index.rb', line 995

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



1017
1018
1019
1020
# File 'lib/algolia/search_index.rb', line 1017

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.



35
36
37
38
39
40
41
42
43
# File 'lib/algolia/search_index.rb', line 35

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