-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.php
More file actions
279 lines (244 loc) · 8.33 KB
/
Service.php
File metadata and controls
279 lines (244 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace Snowcap\ElasticaBundle;
use Elastica\Index;
use Elastica\ResultSet;
use Elastica\Search;
use Elastica\Type\Mapping;
use Snowcap\ElasticaBundle\Indexer\IndexerInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
/**
* This service class is the main entry point for Elastica operations
*
* @package Snowcap\ElasticaBundle
*/
class Service extends ContainerAware implements ServiceInterface
{
/**
* @var Client
*/
protected $client;
/**
* @var string
*/
protected $namespace;
/**
* @var array
*/
protected $indexes = array();
/**
* @var array
*/
protected $types = array();
/**
* @var array
*/
protected $indexers = array();
/**
* @param Client $client
* @param string $namespace
*/
public function __construct(Client $client, $namespace)
{
$this->client = $client;
$this->namespace = $namespace;
}
/**
* Create indexes as defined in the config
*
*/
public function createIndexes()
{
foreach ($this->indexes as $indexName => $indexParams) {
$index = $this->client->getIndex($indexName);
$response = $index->create($indexParams, true);
$this->createTypes($index);
}
}
/**
* Create types associated with the given index
*
* @param Index $index
*/
protected function createTypes(Index $index)
{
foreach ($this->indexers as $indexerAlias => $indexer) {
$type = $index->getType($indexerAlias);
$mapping = new Mapping();
$mapping->setType($type);
$mapping->setProperties($indexer->getMappings());
$mapping->send();
}
}
/**
* Reindex all indexable content
*
*/
public function reindex()
{
foreach ($this->indexes as $indexName => $indexParams) {
$index = $this->client->getIndex($indexName);
foreach ($this->indexers as $indexerAlias => $indexer) {
$type = $index->getType($indexerAlias);
$entities = $indexer->getEntitiesToIndex($this->container->get('doctrine.orm.entity_manager'), $type);
foreach($entities as $entity) {
$indexer->addIndex($entity, $type);
}
$this->container->get('doctrine.orm.entity_manager')->clear();
}
}
}
/**
* Rebuild one type associated to all indexes
*
* @param string $typeName
*/
public function rebuildType($typeName)
{
// Check indexer with the given type name
if (!isset($typeName, $this->indexers)) {
throw new \UnexpectedValueException(sprintf('The indexer for type "%s" does not exist.', $typeName));
}
/** @var IndexerInterface $indexer */
$indexer = $this->indexers[$typeName];
foreach ($this->indexes as $indexName => $indexParams) {
$index = $this->client->getIndex($indexName);
$type = $index->getType($typeName);
// Create the type with the correct mapping
$type->delete();
$mapping = new Mapping();
$mapping->setType($type);
$mapping->setProperties($indexer->getMappings());
$mapping->send();
// Reindex data
$entities = $indexer->getEntitiesToIndex($this->container->get('doctrine.orm.entity_manager'), $type);
foreach($entities as $entity) {
$indexer->addIndex($entity, $type);
}
$this->container->get('doctrine.orm.entity_manager')->clear();
}
}
/**
* Take the appropriate index action for the given entity
*
* @param object $entity
*/
public function index($entity)
{
foreach ($this->indexes as $indexName => $indexParams) {
$index = $this->client->getIndex($indexName);
foreach ($this->indexers as $indexerAlias => $indexer) {
if($indexer->supports($entity)) {
$type = $index->getType($indexerAlias);
$indexableEntities = $indexer->getIndexableEntities($entity);
foreach ($indexableEntities as $indexableEntity) {
if($this->container->get('doctrine.orm.entity_manager')->getUnitOfWork()->isScheduledForDelete($indexableEntity)) {
$action = IndexerInterface::ACTION_REMOVE;
}
else {
$action = $indexer->getIndexAction($indexableEntity, $type);
}
switch($action) {
case IndexerInterface::ACTION_ADD:
$indexer->addIndex($indexableEntity, $type);
break;
case IndexerInterface::ACTION_REMOVE:
$indexer->removeIndex($indexableEntity, $type);
break;
}
}
}
}
}
}
public function indexRemove($entity)
{
foreach ($this->indexes as $indexName => $indexParams) {
$index = $this->client->getIndex($indexName);
foreach ($this->indexers as $indexerAlias => $indexer) {
if($indexer->supports($entity)) {
$type = $index->getType($indexerAlias);
$indexableEntities = $indexer->getIndexableEntities($entity);
foreach ($indexableEntities as $indexableEntity) {
if($indexableEntity->getId() !== null) {
if (get_class($entity) === get_class($indexableEntity)) {
$indexer->removeIndexById($indexableEntity->getId(), $type);
} else {
// Special case: a managed entity has been removed, but
// it isn't the main indexable entity, so instead of
// removing anything, we need to update the indexable entity
// to let him know some of his related is gone
$indexer->addIndex($indexableEntity, $type);
}
}
}
}
}
}
}
/**
* Perform a simple search on the given index and types
*
* @param string $query
* @param string|array $index
* @param array $types
* @param array|int|null $options
* @return ResultSet
*/
public function search($query, $index, $types = null, $options = null)
{
$search = new Search($this->client);
if (!is_array($index)) {
$index = array($index);
}
if($types === null) {
$types = array_keys($this->indexers);
}
foreach ($index as $idx) {
$search->addIndex($this->addNamespace($idx));
}
$search->addTypes($types);
return $search->search($query, $options);
}
/**
* Register an index
*
* @param string $alias
* @param Indexer\IndexerInterface $indexer
* @throws \UnexpectedValueException
*/
public function registerIndexer($alias, IndexerInterface $indexer)
{
foreach($indexer->getManagedClasses() as $managedClass) {
if(!class_exists($managedClass)) {
$message = 'Invalid managed class "%s" provided in indexer "%s"';
throw new \UnexpectedValueException(sprintf($message, $managedClass, get_class($indexer)));
}
}
$this->indexers[$alias] = $indexer;
}
/**
* Return all indexers
*
* @return array
*/
public function getIndexers()
{
return $this->indexers;
}
/**
* Set indexes
*
* @param array $indexes
*/
public function setIndexes(array $indexes)
{
$namespacedIndexes = array();
foreach($indexes as $indexName => $indexParams) {
$namespacedIndexes[$this->addNamespace($indexName)] = $indexParams;
}
$this->indexes = $namespacedIndexes;
}
private function addNamespace($indexName) {
return $this->namespace . '.' . $indexName;
}
}