Skip to content

Commit 7e3354d

Browse files
committed
[POC] Implemtend working config with multiple indexes per document class
1 parent 428d21e commit 7e3354d

File tree

3 files changed

+170
-90
lines changed

3 files changed

+170
-90
lines changed

DependencyInjection/Compiler/MappingPass.php

Lines changed: 131 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
use ONGR\ElasticsearchBundle\Annotation\Index;
1515
use ONGR\ElasticsearchBundle\DependencyInjection\Configuration;
16+
use ONGR\ElasticsearchBundle\Exception\DocumentIndexParserException;
1617
use ONGR\ElasticsearchBundle\Mapping\Converter;
1718
use ONGR\ElasticsearchBundle\Mapping\DocumentParser;
1819
use ONGR\ElasticsearchBundle\Mapping\IndexSettings;
1920
use ONGR\ElasticsearchBundle\Service\IndexService;
2021
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
22+
use Symfony\Component\DependencyInjection\Container;
2123
use Symfony\Component\DependencyInjection\ContainerBuilder;
2224
use Symfony\Component\DependencyInjection\Definition;
2325

@@ -36,9 +38,65 @@ class MappingPass implements CompilerPassInterface
3638
public function process(ContainerBuilder $container)
3739
{
3840
$kernelDir = $container->getParameter('kernel.project_dir');
41+
$parser = $container->get(DocumentParser::class);
3942

43+
$indexClasses = [];
44+
$indexSettingsArray = [];
4045
foreach ($container->getParameter(Configuration::ONGR_SOURCE_DIR) as $dir) {
41-
$this->handleDirectoryMapping($container, $kernelDir . $dir);
46+
foreach ($this->getNamespaces($kernelDir . $dir) as $namespace) {
47+
$indexClasses[$namespace] = $namespace;
48+
}
49+
}
50+
51+
$overwrittenClasses = [];
52+
$indexOverrides = $container->getParameter(Configuration::ONGR_INDEXES_OVERRIDE);
53+
54+
foreach ($indexOverrides as $name => $indexOverride) {
55+
$class = isset($indexOverride['class']) ? $indexOverride['class'] : $name;
56+
57+
if (!isset($indexClasses[$class])) {
58+
throw new \RuntimeException(
59+
sprintf(
60+
'Document `%s` defined in ongr_elasticsearch.indexes config could not been found',
61+
$class,
62+
)
63+
);
64+
}
65+
66+
$indexSettings = $this->parseIndexSettingsFromClass($parser, $class);
67+
68+
if ($class !== $name) {
69+
$indexSettings->setIndexName('ongr.es.index.'.$name);
70+
}
71+
72+
if (isset($indexOverride['alias'])) {
73+
$indexSettings->setAlias($indexOverride['alias']);
74+
}
75+
76+
if (isset($indexOverride['settings'])) {
77+
$indexSettings->setIndexMetadata($indexOverride['settings']);
78+
}
79+
80+
if (isset($indexOverride['hosts'])) {
81+
$indexSettings->setHosts($indexOverride['hosts']);
82+
}
83+
84+
if (isset($indexOverride['default'])) {
85+
$indexSettings->setDefaultIndex($indexOverride['default']);
86+
}
87+
88+
$indexSettingsArray[$name] = $indexSettings;
89+
$overwrittenClasses[$class] = $class;
90+
}
91+
92+
foreach (array_diff($indexClasses, $overwrittenClasses) as $indexClass) {
93+
try {
94+
$indexSettingsArray[$indexClass] = $this->parseIndexSettingsFromClass($parser, $indexClass);
95+
} catch (DocumentIndexParserException $e) {}
96+
}
97+
98+
foreach($indexSettingsArray as $indexSettings) {
99+
$this->createIndex($container, $indexSettings);
42100
}
43101

44102
$container->setParameter(Configuration::ONGR_INDEXES, $this->indexes);
@@ -48,88 +106,84 @@ public function process(ContainerBuilder $container)
48106
);
49107
}
50108

51-
/**
52-
* @param ContainerBuilder $container
53-
* @param string $dir
54-
*
55-
* @throws \ReflectionException
56-
*/
57-
private function handleDirectoryMapping(ContainerBuilder $container, string $dir): void
109+
private function parseIndexSettingsFromClass(DocumentParser $parser, string $className) : IndexSettings
58110
{
59-
/** @var DocumentParser $parser */
60-
$parser = $container->get(DocumentParser::class);
61-
$indexesOverride = $container->getParameter(Configuration::ONGR_INDEXES_OVERRIDE);
62-
$converterDefinition = $container->getDefinition(Converter::class);
111+
$class = new \ReflectionClass($className);
63112

64-
foreach ($this->getNamespaces($dir) as $namespace) {
65-
$class = new \ReflectionClass($namespace);
113+
/** @var Index $document */
114+
$document = $parser->getIndexAnnotation($class);
66115

67-
if (isset($indexesOverride[$namespace]['alias']) && $indexesOverride[$namespace]['alias']) {
68-
$indexAlias = $indexesOverride[$namespace]['alias'];
69-
} else {
70-
$indexAlias = $parser->getIndexAliasName($class);
71-
}
116+
if ($document === null) {
117+
throw new DocumentIndexParserException();
118+
}
72119

73-
/** @var Index $document */
74-
$document = $parser->getIndexAnnotation($class);
75-
$indexMetadata = $parser->getIndexMetadata($class);
76-
77-
if (!empty($indexMetadata)) {
78-
$indexMetadata['settings'] = array_filter(array_merge_recursive(
79-
$indexMetadata['settings'] ?? [],
80-
[
81-
'number_of_replicas' => $document->numberOfReplicas,
82-
'number_of_shards' => $document->numberOfShards,
83-
],
84-
$indexesOverride[$namespace]['settings'] ?? []
85-
));
86-
87-
$indexSettings = new Definition(
88-
IndexSettings::class,
89-
[
90-
$namespace,
91-
$indexAlias,
92-
$indexAlias,
93-
$indexMetadata,
94-
$indexesOverride[$namespace]['hosts'] ?? $document->hosts,
95-
$indexesOverride[$namespace]['default'] ?? $document->default,
96-
]
97-
);
120+
$indexSettings = new IndexSettings(
121+
$className,
122+
$className,
123+
$parser->getIndexAliasName($class),
124+
$parser->getIndexMetadata($class),
125+
$parser->getPropertyMetadata($class),
126+
$document->hosts,
127+
$parser->isDefaultIndex($class)
128+
);
98129

99-
$indexServiceDefinition = new Definition(IndexService::class, [
100-
$namespace,
101-
$converterDefinition,
102-
$container->getDefinition('event_dispatcher'),
103-
$indexSettings,
104-
$container->getParameter(Configuration::ONGR_PROFILER_CONFIG)
105-
? $container->getDefinition('ongr.esb.tracer') : null
106-
]);
107-
$indexServiceDefinition->setPublic(true);
108-
$converterDefinition->addMethodCall(
109-
'addClassMetadata',
110-
[
111-
$namespace,
112-
$parser->getPropertyMetadata($class)
113-
]
114-
);
130+
$indexSettings->setIndexMetadata(['settings' => [
131+
'number_of_replicas' => $document->numberOfReplicas,
132+
'number_of_shards' => $document->numberOfShards,
133+
]]);
115134

116-
$container->setDefinition($namespace, $indexServiceDefinition);
117-
$this->indexes[$indexAlias] = $namespace;
118-
$isCurrentIndexDefault = $parser->isDefaultIndex($class);
119-
if ($this->defaultIndex && $isCurrentIndexDefault) {
120-
throw new \RuntimeException(
121-
sprintf(
122-
'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s`',
123-
$this->defaultIndex,
124-
$indexAlias
125-
)
126-
);
127-
}
128-
129-
if ($isCurrentIndexDefault) {
130-
$this->defaultIndex = $indexAlias;
131-
}
132-
}
135+
return $indexSettings;
136+
}
137+
138+
private function createIndex(Container $container, IndexSettings $indexSettings) {
139+
$converterDefinition = $container->getDefinition(Converter::class);
140+
141+
$indexSettingsDefinition = new Definition(
142+
IndexSettings::class,
143+
[
144+
$indexSettings->getNamespace(),
145+
$indexSettings->getAlias(),
146+
$indexSettings->getAlias(),
147+
$indexSettings->getIndexMetadata(),
148+
$indexSettings->getPropertyMetadata(),
149+
$indexSettings->getHosts(),
150+
$indexSettings->isDefaultIndex(),
151+
]
152+
);
153+
154+
$indexServiceDefinition = new Definition(IndexService::class, [
155+
$indexSettings->getNamespace(),
156+
$converterDefinition,
157+
$container->getDefinition('event_dispatcher'),
158+
$indexSettingsDefinition,
159+
$container->getParameter(Configuration::ONGR_PROFILER_CONFIG)
160+
? $container->getDefinition('ongr.esb.tracer') : null
161+
]);
162+
163+
$indexServiceDefinition->setPublic(true);
164+
$converterDefinition->addMethodCall(
165+
'addClassMetadata',
166+
[
167+
$indexSettings->getNamespace(),
168+
$indexSettings->getPropertyMetadata()
169+
]
170+
);
171+
172+
$container->setDefinition($indexSettings->getIndexName(), $indexServiceDefinition);
173+
$this->indexes[$indexSettings->getAlias()] = $indexSettings->getIndexName();
174+
$isCurrentIndexDefault = $indexSettings->isDefaultIndex();
175+
if ($this->defaultIndex && $isCurrentIndexDefault) {
176+
throw new \RuntimeException(
177+
sprintf(
178+
'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s`',
179+
$this->defaultIndex,
180+
$indexSettings->getAlias()
181+
)
182+
);
183+
}
184+
185+
if ($isCurrentIndexDefault) {
186+
$this->defaultIndex = $indexSettings->getAlias();
133187
}
134188
}
135189

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\Exception;
13+
14+
class DocumentIndexParserException extends DocumentParserException
15+
{
16+
}

Mapping/IndexSettings.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class IndexSettings
1616
private $indexName;
1717
private $alias;
1818
private $indexMetadata;
19+
private $propertyMetadata;
1920
private $hosts;
2021
private $defaultIndex = false;
2122

@@ -24,13 +25,15 @@ public function __construct(
2425
string $indexName,
2526
string $alias,
2627
array $indexMetadata = [],
28+
array $propertyMetadata = [],
2729
array $hosts = [],
2830
bool $defaultIndex = false
2931
) {
3032
$this->namespace = $namespace;
3133
$this->indexName = $indexName;
3234
$this->alias = $alias;
3335
$this->indexMetadata = $indexMetadata;
36+
$this->propertyMetadata = $propertyMetadata;
3437
$this->hosts = $hosts;
3538
$this->defaultIndex = $defaultIndex;
3639
}
@@ -40,64 +43,71 @@ public function getNamespace()
4043
return $this->namespace;
4144
}
4245

43-
public function setNamespace($namespace): self
46+
public function setNamespace($namespace): void
4447
{
4548
$this->namespace = $namespace;
46-
return $this;
4749
}
4850

4951
public function getIndexName()
5052
{
5153
return $this->indexName;
5254
}
5355

54-
public function setIndexName($indexName): self
56+
public function setIndexName($indexName): void
5557
{
5658
$this->indexName = $indexName;
57-
return $this;
5859
}
5960

6061
public function getAlias()
6162
{
6263
return $this->alias;
6364
}
6465

65-
public function setAlias($alias): self
66+
public function setAlias($alias): void
6667
{
6768
$this->alias = $alias;
68-
return $this;
6969
}
7070

7171
public function getIndexMetadata()
7272
{
7373
return $this->indexMetadata;
7474
}
7575

76-
public function setIndexMetadata($indexMetadata): self
76+
public function setIndexMetadata($indexMetadata): void
7777
{
78-
$this->indexMetadata = $indexMetadata;
79-
return $this;
78+
$this->indexMetadata = array_filter(array_merge_recursive(
79+
$this->indexMetadata,
80+
$indexMetadata
81+
));
82+
}
83+
84+
public function getPropertyMetadata(): array
85+
{
86+
return $this->propertyMetadata;
87+
}
88+
89+
public function setPropertyMetadata(array $propertyMetadata): void
90+
{
91+
$this->propertyMetadata = $propertyMetadata;
8092
}
8193

8294
public function getHosts()
8395
{
8496
return $this->hosts;
8597
}
8698

87-
public function setHosts($hosts): self
99+
public function setHosts($hosts): void
88100
{
89101
$this->hosts = $hosts;
90-
return $this;
91102
}
92103

93104
public function isDefaultIndex(): bool
94105
{
95106
return $this->defaultIndex;
96107
}
97108

98-
public function setDefaultIndex(bool $defaultIndex): self
109+
public function setDefaultIndex(bool $defaultIndex): void
99110
{
100111
$this->defaultIndex = $defaultIndex;
101-
return $this;
102112
}
103113
}

0 commit comments

Comments
 (0)