1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Persister; |
13
|
|
|
|
14
|
|
|
use Elastica\Document; |
15
|
|
|
use Elastica\Exception\BulkException; |
16
|
|
|
use Elastica\Index; |
17
|
|
|
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Inserts, replaces and deletes single documents in an elastica type |
22
|
|
|
* Accepts domain model objects and converts them to elastica documents. |
23
|
|
|
* |
24
|
|
|
* @author Thibault Duplessis <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ObjectPersister implements ObjectPersisterInterface |
27
|
|
|
{ |
28
|
|
|
protected $index; |
29
|
|
|
protected $transformer; |
30
|
|
|
protected $objectClass; |
31
|
|
|
protected $fields; |
32
|
|
|
protected $logger; |
33
|
|
|
private $options; |
34
|
|
|
|
35
|
16 |
|
public function __construct(Index $index, ModelToElasticaTransformerInterface $transformer, string $objectClass, array $fields, array $options = []) |
36
|
|
|
{ |
37
|
16 |
|
$this->index = $index; |
38
|
16 |
|
$this->transformer = $transformer; |
39
|
16 |
|
$this->objectClass = $objectClass; |
40
|
16 |
|
$this->fields = $fields; |
41
|
16 |
|
$this->options = $options; |
42
|
16 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function handlesObject($object): bool |
48
|
|
|
{ |
49
|
|
|
return $object instanceof $this->objectClass; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setLogger(LoggerInterface $logger) |
53
|
|
|
{ |
54
|
|
|
$this->logger = $logger; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
7 |
|
public function insertOne($object) |
61
|
|
|
{ |
62
|
7 |
|
$this->insertMany([$object]); |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
5 |
|
public function replaceOne($object) |
69
|
|
|
{ |
70
|
5 |
|
$this->replaceMany([$object]); |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
3 |
|
public function deleteOne($object) |
77
|
|
|
{ |
78
|
3 |
|
$this->deleteMany([$object]); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function deleteById($id, $routing = false) |
85
|
|
|
{ |
86
|
|
|
$this->deleteManyByIdentifiers([$id], $routing); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
10 |
|
public function insertMany(array $objects) |
93
|
|
|
{ |
94
|
10 |
|
$documents = []; |
95
|
10 |
|
foreach ($objects as $object) { |
96
|
10 |
|
$documents[] = $this->transformToElasticaDocument($object); |
97
|
|
|
} |
98
|
|
|
try { |
99
|
8 |
|
$this->index->addDocuments($documents, $this->options); |
100
|
|
|
} catch (BulkException $e) { |
101
|
|
|
$this->log($e); |
102
|
|
|
} |
103
|
8 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
5 |
|
public function replaceMany(array $objects) |
109
|
|
|
{ |
110
|
5 |
|
$documents = []; |
111
|
5 |
|
foreach ($objects as $object) { |
112
|
5 |
|
$document = $this->transformToElasticaDocument($object); |
113
|
4 |
|
$document->setDocAsUpsert(true); |
114
|
4 |
|
$documents[] = $document; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
4 |
|
$this->index->updateDocuments($documents, $this->options); |
119
|
|
|
} catch (BulkException $e) { |
120
|
|
|
$this->log($e); |
121
|
|
|
} |
122
|
4 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
3 |
|
public function deleteMany(array $objects) |
128
|
|
|
{ |
129
|
3 |
|
$documents = []; |
130
|
3 |
|
foreach ($objects as $object) { |
131
|
3 |
|
$documents[] = $this->transformToElasticaDocument($object); |
132
|
|
|
} |
133
|
|
|
try { |
134
|
2 |
|
$this->index->deleteDocuments($documents); |
135
|
|
|
} catch (BulkException $e) { |
136
|
|
|
$this->log($e); |
137
|
|
|
} |
138
|
2 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function deleteManyByIdentifiers(array $identifiers, $routing = false) |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
$this->index->getClient()->deleteIds($identifiers, $this->index->getName(), $routing); |
147
|
|
|
} catch (BulkException $e) { |
148
|
|
|
$this->log($e); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Transforms an object to an elastica document. |
154
|
|
|
*/ |
155
|
6 |
|
public function transformToElasticaDocument(object $object): Document |
156
|
|
|
{ |
157
|
6 |
|
return $this->transformer->transform($object, $this->fields); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
162
|
|
|
* |
163
|
|
|
* @throws BulkException |
164
|
|
|
*/ |
165
|
|
|
private function log(BulkException $e) |
166
|
|
|
{ |
167
|
|
|
if (!$this->logger) { |
168
|
|
|
throw $e; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->logger->error($e); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|