Class: Algolia::AccountClient

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/account_client.rb

Overview

A class which encapsulates the HTTPS communication with the Algolia API server for cross-app operations.

Class Method Summary collapse

Class Method Details

.copy_index(source_index, destination_index, request_options = {}) ⇒ Object

Copies settings, synonyms, rules and objects from the source index to the destination index. The replicas of the source index should not be copied.

Throw an exception if the destination index already exists Throw an exception if the indices are on the same application

Parameters:

  • source_index

    the source index object

  • destination_index

    the destination index object

  • request_options (defaults to: {})

    contains extra parameters to send with your query

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/algolia/account_client.rb', line 21

def copy_index(source_index, destination_index, request_options = {})
  raise AlgoliaError.new('The indexes are in the same application. Use Algolia::Client.copy_index instead.') if source_index.client.application_id == destination_index.client.application_id

  begin
    settings = destination_index.get_settings()
  rescue AlgoliaError
    # Destination index does not exists. We can proceed.
  else
    raise AlgoliaError.new("Destination index already exists. Please delete it before copying index across applications.")
  end

  responses = []

  settings = source_index.get_settings()
  responses << destination_index.set_settings(settings, {}, request_options)

  synonyms = []
  source_index.export_synonyms(100, request_options) do |synonym|
    synonym.delete('_highlightResult')
    synonyms << synonym
  end

  responses << destination_index.batch_synonyms(synonyms, false, false, request_options)

  rules = []
  source_index.export_rules(100, request_options) do |rule|
    rule.delete('_highlightResult')
    rules << rule
  end
  responses << destination_index.batch_rules(rules, false, false, request_options)

  # Copy objects
  responses = []
  batch = []
  batch_size = 1000
  count = 0

  source_index.browse do |obj|
    batch << obj
    count += 1

    if count == batch_size
      responses << destination_index.save_objects(batch, request_options)
      batch = []
      count = 0
    end
  end

  if batch.any?
    responses << destination_index.save_objects(batch, request_options)
  end

  responses
end

.copy_index!(source_index, destination_index, request_options = {}) ⇒ Object

The method copy settings, synonyms, rules and objects from the source index to the destination index and wait end of indexing. The replicas of the source index should not be copied

Throw an exception if the destination index already exists Throw an exception if the indices are on the same application

Parameters:

  • source_index

    the source index object

  • destination_index

    the destination index object

  • request_options (defaults to: {})

    contains extra parameters to send with your query



88
89
90
91
92
93
94
95
96
# File 'lib/algolia/account_client.rb', line 88

def copy_index!(source_index, destination_index, request_options = {})
  responses = self.copy_index(source_index, destination_index, request_options)

  responses.each do |res|
    destination_index.wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  end

  responses
end