zelastic 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd3ad1bd811164b7227b243782cea048602ece2c
4
- data.tar.gz: 6796d5134935589ca86d27601a2eb6d771ae53a7
3
+ metadata.gz: f642b0f5988b4f4a0fb9198da0f0276208d3039a
4
+ data.tar.gz: bb4a08940cf9791cac531be04d7eed6d63ca6fe4
5
5
  SHA512:
6
- metadata.gz: 4569aedd2380a6974ecc467cff030cc75527fc7d5adeec4c347f73595b298f9f21115f012c76a7f2e3ee881613f884139785e9ead50bed86acf2f32db127fbed
7
- data.tar.gz: fa8a9be28763777a93bf052fa3e53b9ce855c441a9dd4492eebf49e025aa3ed59a631c84e53fc1a9715e6ce1170ed89eaf1acd35c96b83ec74a20ed86606ed14
6
+ metadata.gz: bb13a0754667b7de1bb7a78e478beb4740641775bd609a2294063bcbc27186dd835d77ca7789f67dbef0d462b3e204402697ea92714fe9c19be8dab415535de7
7
+ data.tar.gz: 29ecd1278cca977883c5f2e90ddc4163f674e5064308955ba1363a58fbc7cc56e7f357ce557329422cd6e777d34ae023bac80403f2608dc697b0d3d0549a40de
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zelastic (0.2.0)
4
+ zelastic (0.3.0)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Zelastic
4
4
  class Config
5
- attr_reader :client, :data_source
5
+ attr_reader :clients, :data_source
6
6
 
7
7
  def initialize(
8
8
  client:,
@@ -11,7 +11,7 @@ module Zelastic
11
11
  **overrides,
12
12
  &index_data
13
13
  )
14
- @client = client
14
+ @clients = Array(client)
15
15
  @data_source = data_source
16
16
  @mapping = mapping
17
17
  @index_data = index_data
@@ -4,8 +4,9 @@ module Zelastic
4
4
  class IndexManager
5
5
  extend Forwardable
6
6
 
7
- def initialize(config)
7
+ def initialize(config, client: nil)
8
8
  @config = config
9
+ @client = client || config.clients.first
9
10
  end
10
11
 
11
12
  def create_index(unique_name)
@@ -97,8 +98,8 @@ module Zelastic
97
98
 
98
99
  private
99
100
 
100
- attr_reader :config
101
- def_delegators :config, :client, :logger
101
+ attr_reader :config, :client
102
+ def_delegators :config, :logger
102
103
 
103
104
  def indexer
104
105
  @indexer ||= Indexer.new(config)
@@ -5,8 +5,8 @@ module Zelastic
5
5
  class IndexingError < StandardError
6
6
  attr_reader :errors
7
7
 
8
- def initialize(errors)
9
- @errors = errors
8
+ def initialize(result)
9
+ @errors = result['items'].map { |item| item['error'] }.compact
10
10
  super("Errors indexing: #{errors.join(', ')}")
11
11
  end
12
12
  end
@@ -18,65 +18,50 @@ module Zelastic
18
18
  end
19
19
 
20
20
  def index_batch(batch, index_name: nil)
21
- indices = Array(index_name || write_indices)
22
21
  logger.info("ES: Indexing #{config.type} record")
23
22
 
24
23
  version = current_version
25
- execute_bulk(
26
- indices.flat_map do |index|
27
- batch.map do |record|
28
- index_command(index: index, version: version, record: record)
29
- end
24
+ execute_bulk do |index_name|
25
+ batch.map do |record|
26
+ index_command(index: index_name, version: version, record: record)
30
27
  end
31
- )
28
+ end
32
29
  end
33
30
 
34
31
  def index_record(record)
35
32
  version = current_version
36
33
 
37
- execute_bulk(
38
- write_indices.map do |index|
39
- index_command(index: index, version: version, record: record)
40
- end
41
- )
34
+ execute_bulk do |index_name|
35
+ index_command(index: index_name, version: version, record: record)
36
+ end
42
37
  end
43
38
 
44
39
  def delete_by_id(id)
45
- indices = client.indices.get_alias(name: config.write_alias).keys
46
-
47
- indices.each do |index|
48
- client.delete(
49
- index: index,
50
- type: config.type,
51
- id: id
52
- )
53
- end
40
+ delete_by_ids([id])
54
41
  end
55
42
 
56
43
  def delete_by_ids(ids)
57
44
  logger.info('ES: Deleting batch records')
58
45
 
59
- indices = config.client.indices.get_alias(name: config.write_alias).keys
60
-
61
- execute_bulk(
62
- indices.flat_map do |index|
63
- ids.map do |id|
64
- {
65
- delete: {
66
- _index: index,
67
- _type: config.type,
68
- _id: id
69
- }
46
+ execute_bulk do |index_name|
47
+ ids.map do |id|
48
+ {
49
+ delete: {
50
+ _index: index_name,
51
+ _type: config.type,
52
+ _id: id
70
53
  }
71
- end
54
+ }
72
55
  end
73
- )
56
+ end
74
57
  end
75
58
 
76
59
  def delete_by_query(query)
77
60
  logger.info('ES: Deleting batch records')
78
61
 
79
- config.client.delete_by_query(index: config.write_alias, body: { query: query })
62
+ config.clients.each do |client|
63
+ client.delete_by_query(index: config.write_alias, body: { query: query })
64
+ end
80
65
  end
81
66
 
82
67
  private
@@ -88,8 +73,8 @@ module Zelastic
88
73
  config.data_source.connection.select_one('SELECT txid_current()').fetch('txid_current')
89
74
  end
90
75
 
91
- def write_indices
92
- config.client.indices.get_alias(name: config.write_alias).keys
76
+ def write_indices(client)
77
+ client.indices.get_alias(name: config.write_alias).keys
93
78
  end
94
79
 
95
80
  def index_command(index:, version:, record:)
@@ -105,11 +90,13 @@ module Zelastic
105
90
  }
106
91
  end
107
92
 
108
- def execute_bulk(commands)
109
- result = config.client.bulk(body: commands)
110
- return result unless result['errors']
111
- errors = result['items'].map { |item| item['error'] }.compact
112
- raise IndexingError, errors
93
+ def execute_bulk
94
+ config.clients.map do |client|
95
+ commands = write_indices(client).map { |index_name| yield(index) }
96
+ client.bulk(body: commands).tap do |result|
97
+ raise IndexingError, result if result['errors']
98
+ end
99
+ end
113
100
  end
114
101
  end
115
102
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zelastic
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zelastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - carwow Developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-19 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport