1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://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\Type; |
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 $type; |
29
|
|
|
protected $transformer; |
30
|
|
|
protected $objectClass; |
31
|
|
|
protected $fields; |
32
|
|
|
protected $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Type $type |
36
|
|
|
* @param ModelToElasticaTransformerInterface $transformer |
37
|
|
|
* @param string $objectClass |
38
|
|
|
* @param array $fields |
39
|
|
|
*/ |
40
|
17 |
|
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) |
41
|
|
|
{ |
42
|
17 |
|
$this->type = $type; |
43
|
17 |
|
$this->transformer = $transformer; |
44
|
17 |
|
$this->objectClass = $objectClass; |
45
|
17 |
|
$this->fields = $fields; |
46
|
17 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function handlesObject($object) |
52
|
|
|
{ |
53
|
|
|
return $object instanceof $this->objectClass; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param LoggerInterface $logger |
58
|
|
|
*/ |
59
|
|
|
public function setLogger(LoggerInterface $logger) |
60
|
|
|
{ |
61
|
|
|
$this->logger = $logger; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
7 |
|
public function insertOne($object) |
68
|
|
|
{ |
69
|
7 |
|
$this->insertMany([$object]); |
70
|
6 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
5 |
|
public function replaceOne($object) |
76
|
|
|
{ |
77
|
5 |
|
$this->replaceMany([$object]); |
78
|
4 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
3 |
|
public function deleteOne($object) |
84
|
|
|
{ |
85
|
3 |
|
$this->deleteMany([$object]); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function deleteById($id) |
92
|
|
|
{ |
93
|
|
|
$this->deleteManyByIdentifiers([$id]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
10 |
View Code Duplication |
public function insertMany(array $objects) |
100
|
|
|
{ |
101
|
10 |
|
$documents = []; |
102
|
10 |
|
foreach ($objects as $object) { |
103
|
10 |
|
$documents[] = $this->transformToElasticaDocument($object); |
104
|
|
|
} |
105
|
|
|
try { |
106
|
8 |
|
$this->type->addDocuments($documents); |
107
|
|
|
} catch (BulkException $e) { |
108
|
|
|
$this->log($e); |
109
|
|
|
} |
110
|
8 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
5 |
|
public function replaceMany(array $objects) |
116
|
|
|
{ |
117
|
5 |
|
$documents = []; |
118
|
5 |
|
foreach ($objects as $object) { |
119
|
5 |
|
$document = $this->transformToElasticaDocument($object); |
120
|
4 |
|
$document->setDocAsUpsert(true); |
121
|
4 |
|
$documents[] = $document; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
try { |
125
|
4 |
|
$this->type->updateDocuments($documents); |
126
|
|
|
} catch (BulkException $e) { |
127
|
|
|
$this->log($e); |
128
|
|
|
} |
129
|
4 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
3 |
View Code Duplication |
public function deleteMany(array $objects) |
135
|
|
|
{ |
136
|
3 |
|
$documents = []; |
137
|
3 |
|
foreach ($objects as $object) { |
138
|
3 |
|
$documents[] = $this->transformToElasticaDocument($object); |
139
|
|
|
} |
140
|
|
|
try { |
141
|
2 |
|
$this->type->deleteDocuments($documents); |
142
|
|
|
} catch (BulkException $e) { |
143
|
|
|
$this->log($e); |
144
|
|
|
} |
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function deleteManyByIdentifiers(array $identifiers) |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
|
|
$this->type->getIndex()->getClient()->deleteIds($identifiers, $this->type->getIndex(), $this->type); |
154
|
|
|
} catch (BulkException $e) { |
155
|
|
|
$this->log($e); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Transforms an object to an elastica document. |
161
|
|
|
* |
162
|
|
|
* @param object $object |
163
|
|
|
* |
164
|
|
|
* @return Document the elastica document |
165
|
|
|
*/ |
166
|
6 |
|
public function transformToElasticaDocument($object) |
167
|
|
|
{ |
168
|
6 |
|
return $this->transformer->transform($object, $this->fields); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
173
|
|
|
* |
174
|
|
|
* @param BulkException $e |
175
|
|
|
* |
176
|
|
|
* @throws BulkException |
177
|
|
|
*/ |
178
|
|
|
private function log(BulkException $e) |
179
|
|
|
{ |
180
|
|
|
if (!$this->logger) { |
181
|
|
|
throw $e; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$this->logger->error($e); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|