|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GBProd\ElasticsearchDataProviderBundle\DataProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
|
6
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasIndexedDocument; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract class for data providing |
|
11
|
|
|
* |
|
12
|
|
|
* @author gbprod <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class DataProvider implements DataProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const BATCH_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 array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $currentBulk; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var int |
|
45
|
|
|
*/ |
|
46
|
|
|
private $currentBulkSize; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function run( |
|
52
|
|
|
Client $client, |
|
53
|
|
|
$index, |
|
54
|
|
|
$type, |
|
55
|
|
|
EventDispatcherInterface $dispatcher |
|
56
|
|
|
) { |
|
57
|
3 |
|
$this->client = $client; |
|
58
|
3 |
|
$this->index = $index; |
|
59
|
3 |
|
$this->type = $type; |
|
60
|
3 |
|
$this->dispatcher = $dispatcher; |
|
61
|
|
|
|
|
62
|
3 |
|
$this->currentBulkSize = 0; |
|
63
|
3 |
|
$this->currentBulk = ['body' => []]; |
|
64
|
|
|
|
|
65
|
3 |
|
$this->populate(); |
|
66
|
|
|
|
|
67
|
3 |
|
$this->flushBulk(); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Populate |
|
72
|
|
|
* |
|
73
|
|
|
* @return null |
|
74
|
|
|
*/ |
|
75
|
|
|
abstract protected function populate(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Index document |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $id |
|
81
|
|
|
* @param array $body |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function index($id, array $body) |
|
84
|
|
|
{ |
|
85
|
2 |
|
$this->currentBulk['body'][] = [ |
|
86
|
|
|
'index' => [ |
|
87
|
2 |
|
'_index' => $this->index, |
|
88
|
2 |
|
'_type' => $this->type, |
|
89
|
2 |
|
'_id' => $id, |
|
90
|
|
|
] |
|
91
|
2 |
|
]; |
|
92
|
|
|
|
|
93
|
2 |
|
$this->currentBulk['body'][] = $body; |
|
94
|
|
|
|
|
95
|
2 |
|
if ($this->shouldFlushBulk()) { |
|
96
|
1 |
|
$this->flushBulk(); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$this->currentBulkSize++; |
|
100
|
|
|
|
|
101
|
2 |
|
$this->dispatcher->dispatch( |
|
102
|
2 |
|
'elasticsearch.has_indexed_document', |
|
103
|
2 |
|
new HasIndexedDocument($id) |
|
104
|
2 |
|
); |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
protected function flushBulk() |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->client->bulk($this->currentBulk); |
|
110
|
|
|
|
|
111
|
3 |
|
$this->currentBulkSize = 0; |
|
112
|
3 |
|
$this->currentBulk = ['body' => []]; |
|
113
|
3 |
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
private function shouldFlushBulk() |
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->currentBulkSize >= self::BATCH_SIZE; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function count() |
|
124
|
|
|
{ |
|
125
|
1 |
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|