1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaProviderBundle\Provider; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use GBProd\ElasticaProviderBundle\Event\HasProvidedDocument; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstract class for data providing |
11
|
|
|
* |
12
|
|
|
* @author gbprod <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class BulkProvider implements Provider |
15
|
|
|
{ |
16
|
|
|
const DEFAULT_BULK_SIZE = 1000; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $index; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $type; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var EventDispatcherInterface |
35
|
|
|
*/ |
36
|
|
|
private $dispatcher; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $bulkSize = self::DEFAULT_BULK_SIZE; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $currentBulk; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $currentBulkSize; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
6 |
|
public function run(Client $client, $index, $type, EventDispatcherInterface $dispatcher) |
57
|
|
|
{ |
58
|
6 |
|
$this->initialize($client, $index, $type, $dispatcher); |
59
|
6 |
|
$this->populate(); |
60
|
6 |
|
$this->commit(); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
6 |
|
private function initialize(Client $client, $index, $type, EventDispatcherInterface $dispatcher) |
64
|
|
|
{ |
65
|
6 |
|
$this->client = $client; |
66
|
6 |
|
$this->index = $index; |
67
|
6 |
|
$this->type = $type; |
68
|
6 |
|
$this->dispatcher = $dispatcher; |
69
|
|
|
|
70
|
6 |
|
$this->currentBulkSize = 0; |
71
|
6 |
|
$this->currentBulk = ['body' => []]; |
72
|
6 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Populate |
76
|
|
|
* |
77
|
|
|
* @return null |
78
|
|
|
*/ |
79
|
|
|
abstract protected function populate(); |
80
|
|
|
|
81
|
6 |
|
private function commit() |
82
|
|
|
{ |
83
|
6 |
|
$this->flushBulk(); |
84
|
|
|
|
85
|
6 |
|
$this->client->refreshAll(); |
86
|
6 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Index document |
90
|
|
|
* |
91
|
|
|
* @param string $id |
92
|
|
|
* @param array $body |
93
|
|
|
*/ |
94
|
2 |
|
public function index($id, array $body) |
95
|
|
|
{ |
96
|
2 |
|
$this->addBulkAction('index', $id, $body); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
5 |
|
private function addBulkAction($action, $id, array $body = null) |
100
|
|
|
{ |
101
|
5 |
|
$this->addBulkData($action, $id, $body); |
102
|
|
|
|
103
|
5 |
|
$this->incrementBulk(); |
104
|
|
|
|
105
|
5 |
|
$this->dispatcher->dispatch( |
106
|
5 |
|
'elasticsearch.has_provided_document', |
107
|
5 |
|
new HasProvidedDocument($id) |
108
|
5 |
|
); |
109
|
5 |
|
} |
110
|
|
|
|
111
|
5 |
|
private function addBulkData($action, $id, array $body = null) |
112
|
|
|
{ |
113
|
5 |
|
$this->currentBulk['body'][] = [ |
114
|
|
|
$action => [ |
115
|
5 |
|
'_index' => $this->index, |
116
|
5 |
|
'_type' => $this->type, |
117
|
5 |
|
'_id' => $id, |
118
|
|
|
] |
119
|
5 |
|
]; |
120
|
|
|
|
121
|
5 |
|
if (null !== $body) { |
122
|
4 |
|
$this->currentBulk['body'][] = $body; |
123
|
4 |
|
} |
124
|
5 |
|
} |
125
|
|
|
|
126
|
5 |
|
private function incrementBulk() |
127
|
|
|
{ |
128
|
5 |
|
if ($this->shouldFlushBulk()) { |
129
|
1 |
|
$this->flushBulk(); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
5 |
|
$this->currentBulkSize++; |
133
|
5 |
|
} |
134
|
|
|
|
135
|
5 |
|
private function shouldFlushBulk() |
136
|
|
|
{ |
137
|
5 |
|
return $this->currentBulkSize >= $this->bulkSize |
138
|
5 |
|
&& $this->currentBulkSize > 0 |
139
|
5 |
|
; |
140
|
|
|
} |
141
|
|
|
|
142
|
6 |
|
private function flushBulk() |
143
|
|
|
{ |
144
|
6 |
|
$this->client->bulk($this->currentBulk); |
145
|
|
|
|
146
|
6 |
|
$this->currentBulkSize = 0; |
147
|
6 |
|
$this->currentBulk = ['body' => []]; |
148
|
6 |
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Delete document |
153
|
|
|
* |
154
|
|
|
* @param string $id |
155
|
|
|
*/ |
156
|
1 |
|
public function delete($id) |
157
|
|
|
{ |
158
|
1 |
|
$this->addBulkAction('delete', $id); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Create a document |
163
|
|
|
* |
164
|
|
|
* @param string $id |
165
|
|
|
* @param array $body |
166
|
|
|
*/ |
167
|
1 |
|
public function create($id, array $body) |
168
|
|
|
{ |
169
|
1 |
|
$this->addBulkAction('create', $id, $body); |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Update a document |
174
|
|
|
* |
175
|
|
|
* @param string $id |
176
|
|
|
* @param array $body |
177
|
|
|
*/ |
178
|
1 |
|
public function update($id, array $body) |
179
|
|
|
{ |
180
|
1 |
|
$this->addBulkAction('update', $id, ['doc' => $body]); |
181
|
1 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
1 |
|
public function count() |
187
|
|
|
{ |
188
|
1 |
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Change bulk size |
193
|
|
|
* |
194
|
|
|
* @param int $bulkSize |
195
|
|
|
* |
196
|
|
|
* @return self |
197
|
|
|
*/ |
198
|
1 |
|
public function changeBulkSize($bulkSize = self::DEFAULT_BULK_SIZE) |
199
|
|
|
{ |
200
|
1 |
|
$this->bulkSize = $bulkSize; |
201
|
|
|
|
202
|
1 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|