Skip to content

Commit dce0a4a

Browse files
authored
Merge pull request #1962 from mwoynarski/bugfix/index_patterns
Updated index template feature to be compatible with ES 6.x+
2 parents 42adbc0 + 0f771ed commit dce0a4a

File tree

11 files changed

+34
-27
lines changed

11 files changed

+34
-27
lines changed

doc/templates.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fos_elastica:
1818
<name>:
1919
client: default
2020
template_name: <template name>
21-
template: some_index_*
21+
index_patterns: ["some_index_*", "some_other_index_*"]
2222
settings:
2323
number_of_shards: 1
2424
number_of_replicas: 0
@@ -32,7 +32,7 @@ fos_elastica:
3232
Index template is similar to index configuration and has the same fields like `settings`, `client`, etc. with additional fields:
3333

3434
1. `template_name` - template name. If omitted then used key (`<name>`) of `index_templates` section. Example: `template_1`
35-
2. `template` - template pattern. Example: `te*` or `bar*`
35+
2. `index_patterns` - index pattern(s) on which to apply the template. Example: `te*` or `bar*`
3636

3737
To apply templates changes, you should run `fos:elastica:reset-templates` command:
3838

src/Configuration/IndexConfigInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* config: TElasticConfig,
2929
* mapping: TMapping,
3030
* model: mixed,
31-
* template?: string,
31+
* index_patterns?: array,
3232
* }
3333
* @phpstan-type TElasticConfig = array{
3434
* date_detection?: bool,

src/Configuration/IndexTemplateConfig.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class IndexTemplateConfig implements IndexConfigInterface
2525
/**
2626
* Index name pattern.
2727
*
28-
* @var string
28+
* @var array<string>
2929
*/
30-
private $template;
30+
private array $indexPatterns;
3131

3232
/**
3333
* Constructor expects an array as generated by the Container Configuration builder.
@@ -45,18 +45,18 @@ public function __construct(array $config)
4545
// @phpstan-ignore-next-line Ignored because of a bug in PHPStan (https://github.com/phpstan/phpstan/issues/5091)
4646
$this->mapping = $config['mapping'];
4747

48-
if (!isset($config['template'])) {
49-
throw new \InvalidArgumentException('Index template value must be set');
48+
if (empty($config['index_patterns'])) {
49+
throw new \InvalidArgumentException("Property 'index_patterns' must be set for index template {$this->name}");
5050
}
5151

52-
$this->template = $config['template'];
52+
$this->indexPatterns = $config['index_patterns'];
5353
}
5454

5555
/**
5656
* Gets index name pattern.
5757
*/
58-
public function getTemplate(): string
58+
public function getIndexPatterns(): array
5959
{
60-
return $this->template;
60+
return $this->indexPatterns;
6161
}
6262
}

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ private function addIndexTemplatesSection(ArrayNodeDefinition $rootNode)
519519
->scalarNode('template_name')
520520
->info('Defaults to the name of the index template, but can be modified if the index name is different in ElasticSearch')
521521
->end()
522-
->scalarNode('template')->isRequired()->end()
522+
->scalarNode('template')->end()
523+
->arrayNode('index_patterns')->prototype('scalar')->end()->end()
523524
->scalarNode('client')->end()
524525
->variableNode('settings')->defaultValue([])->end()
525526
->booleanNode('date_detection')->end()

src/DependencyInjection/FOSElasticaExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $con
269269
$indexId = \sprintf('fos_elastica.index_template.%s', $name);
270270
$indexTemplateName = $indexTemplate['template_name'] ?? $name;
271271

272+
if (empty($indexTemplate['index_patterns']) && empty($indexTemplate['template'])) {
273+
throw new \InvalidArgumentException("One of 'template', 'index_patterns' must be provided for index template {$indexTemplateName}");
274+
}
275+
272276
$indexDef = new ChildDefinition('fos_elastica.index_template_prototype');
273277
$indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndexTemplate']);
274278
$indexDef->replaceArgument(0, $indexTemplateName);
@@ -289,7 +293,7 @@ private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $con
289293
'reference' => $reference,
290294
'name' => $name,
291295
'settings' => $indexTemplate['settings'],
292-
'template' => $indexTemplate['template'],
296+
'index_patterns' => !empty($indexTemplate['index_patterns']) ? $indexTemplate['index_patterns'] : [$indexTemplate['template']],
293297
];
294298

295299
$this->loadIndexConfig((array) $indexTemplate, $this->indexTemplateConfigs[$name]);

src/Index/MappingBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public function buildIndexMapping(IndexConfigInterface $indexConfig): array
5757
/**
5858
* Builds mappings for an entire index template.
5959
*
60-
* @return array{mappings: TMapping, settings: TSettings, template: string}
60+
* @return array{mappings: TMapping, settings: TSettings, index_patterns: array<string>}
6161
*/
6262
public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig): array
6363
{
6464
$mapping = $this->buildIndexMapping($indexTemplateConfig);
65-
$mapping['template'] = $indexTemplateConfig->getTemplate();
65+
$mapping['index_patterns'] = $indexTemplateConfig->getIndexPatterns();
6666

6767
return $mapping;
6868
}

src/Index/TemplateResetter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function resetIndex(string $indexName, bool $deleteIndexes = false): void
6767
*/
6868
public function deleteTemplateIndexes(IndexTemplateConfig $template): void
6969
{
70-
$this->client->request($template->getTemplate().'/', Request::DELETE);
70+
foreach ($template->getIndexPatterns() as $pattern) {
71+
$this->client->request($pattern.'/', Request::DELETE);
72+
}
7173
}
7274
}

tests/Unit/Configuration/IndexTemplateConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testInstantiate()
2626
'elasticsearch_name' => 'index_template_elastic_name1',
2727
'name' => 'index_template1',
2828
'settings' => [1],
29-
'template' => 't*',
29+
'index_patterns' => ['t*'],
3030
'config' => [],
3131
'mapping' => [],
3232
];
@@ -38,7 +38,7 @@ public function testInstantiate()
3838
'elasticsearch_name' => $indexTemplate->getElasticSearchName(),
3939
'name' => $indexTemplate->getName(),
4040
'settings' => $indexTemplate->getSettings(),
41-
'template' => $indexTemplate->getTemplate(),
41+
'index_patterns' => $indexTemplate->getIndexPatterns(),
4242
'config' => [],
4343
'mapping' => [],
4444
]

tests/Unit/Configuration/Source/TemplateContainerSourceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public function testGetConfiguration()
4545
'settings' => [
4646
'some_setting' => 'setting_value',
4747
],
48-
'template' => 'some_index_config_*',
48+
'index_patterns' => ['some_index_config_*'],
4949
],
5050
]
5151
);
5252
$indexes = $containerSource->getConfiguration();
5353
$this->assertInstanceOf(IndexTemplateConfig::class, $indexes['some_index_template']);
5454
$templateConfig = $indexes['some_index_template'];
5555
$this->assertEquals('some_index_template', $templateConfig->getName());
56-
$this->assertEquals('some_index_config_*', $templateConfig->getTemplate());
56+
$this->assertEquals(['some_index_config_*'], $templateConfig->getIndexPatterns());
5757
$this->assertEquals(
5858
[
5959
'some_setting' => 'setting_value',

tests/Unit/Index/MappingBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public function testMappingBuilderStoreProperty()
9797
public function testBuildIndexTemplateMapping()
9898
{
9999
$config = new IndexTemplateConfig(
100-
['template' => 'index_template_*', 'name' => 'some_template', 'config' => [], 'mapping' => $this->indexConfig->getMapping()]
100+
['index_patterns' => ['index_template_*'], 'name' => 'some_template', 'config' => [], 'mapping' => $this->indexConfig->getMapping()]
101101
);
102102
$this->assertEquals(
103103
[
104-
'template' => 'index_template_*',
104+
'index_patterns' => ['index_template_*'],
105105
'mappings' => $this->indexConfig->getMapping(),
106106
],
107107
$this->builder->buildIndexTemplateMapping($config)

0 commit comments

Comments
 (0)