Skip to content

Commit c064338

Browse files
dbalabkaXWB
authored andcommitted
Index templates support (#1343)
* Initial implementation of index template support #916 � Conflicts: � tests/Unit/DependencyInjection/ConfigurationTest.php � tests/Unit/Elastica/ClientTest.php * Minor fixes * Added documentation about index templates
1 parent 8b30ba6 commit c064338

37 files changed

+1582
-110
lines changed

doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Available documentation for FOSElasticaBundle
99
* [Using a Serializer](serializer.md)
1010
* [Types](types.md)
1111
* [Provider](provider.md)
12+
* [Index templates](templates.md)
1213

1314
Cookbook Entries
1415
----------------

doc/templates.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Index templates
2+
================
3+
4+
Index templates allow you to define templates that will automatically be applied when new indices are created
5+
(see more in [official documentation](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/indices-templates.html)).
6+
Index Templates widely is used to create historically indexes:
7+
8+
* storing logs (Kibana) to prevent index growing because of incorrectly mapped fields
9+
* metrics (Marvel) to archive or delete old information
10+
11+
Here's example on how to configure index templates:
12+
13+
```yaml
14+
# app/config/config.yml
15+
16+
fos_elastica:
17+
index_templates:
18+
<name>:
19+
client: default
20+
template_name: <template name>
21+
template: some_index_*
22+
settings:
23+
number_of_shards: 1
24+
number_of_replicas: 0
25+
types:
26+
auto_suggest:
27+
mappings:
28+
<field name>: <params>
29+
...
30+
```
31+
32+
Index template is similar to index configuration and has the same fields like `types`, `settings`, `client`, etc. with additional fields:
33+
34+
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*`
36+
37+
To apply templates changes, you should run `fos:elastica:reset-templates` command:
38+
39+
* `--index` - index template name to reset. If no index template name specified than all templates will be reset
40+
* `--force-delete` - will delete all indexes that match index templates patterns. Aware that pattern may match various indexes.
41+
42+
You must run the following command to sync templates configuration on ES server with YAML configurations:
43+
```bash
44+
php bin/console fos:elastica:reset-templates
45+
```
46+
47+
You can build-in this command into the deployment process to automate template configuration sync.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Command;
13+
14+
use FOS\ElasticaBundle\Index\TemplateResetter;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Question\ConfirmationQuestion;
20+
21+
/**
22+
* Reset search indexes templates.
23+
*/
24+
final class ResetTemplatesCommand extends Command
25+
{
26+
protected static $defaultName = 'fos:elastica:reset-templates';
27+
28+
/** @var TemplateResetter */
29+
private $resetter;
30+
31+
public function __construct(
32+
TemplateResetter $resetter
33+
) {
34+
parent::__construct();
35+
36+
$this->resetter = $resetter;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setName('fos:elastica:reset-templates')
46+
->addOption(
47+
'index',
48+
null,
49+
InputOption::VALUE_REQUIRED,
50+
'The index template to reset. If no index template name specified than all templates will be reset'
51+
)
52+
->addOption(
53+
'force-delete',
54+
null,
55+
InputOption::VALUE_NONE,
56+
'Delete all indexes that matches index templates patterns. ' .
57+
'Aware that pattern may match various indexes.'
58+
)
59+
->setDescription('Reset search indexes templates')
60+
;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protected function execute(InputInterface $input, OutputInterface $output)
67+
{
68+
$indexTemplate = $input->getOption('index');
69+
$deleteByPattern = $input->getOption('force-delete');
70+
71+
if ($deleteByPattern) {
72+
$helper = $this->getHelper('question');
73+
$question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
74+
75+
if (!$helper->ask($input, $output, $question)) {
76+
return 1;
77+
}
78+
}
79+
80+
if (null !== $indexTemplate) {
81+
$output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
82+
$this->resetter->resetIndex($indexTemplate, $deleteByPattern);
83+
} else {
84+
$output->writeln('<info>Resetting all templates</info>');
85+
$this->resetter->resetAllIndexes($deleteByPattern);
86+
}
87+
88+
return 0;
89+
}
90+
}

src/Configuration/IndexConfig.php

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,9 @@
2020

2121
namespace FOS\ElasticaBundle\Configuration;
2222

23-
class IndexConfig
23+
class IndexConfig implements IndexConfigInterface
2424
{
25-
/**
26-
* The name of the index for ElasticSearch.
27-
*
28-
* @var string
29-
*/
30-
private $elasticSearchName;
31-
32-
/**
33-
* The internal name of the index. May not be the same as the name used in ElasticSearch,
34-
* especially if aliases are enabled.
35-
*
36-
* @var string
37-
*/
38-
private $name;
39-
40-
/**
41-
* An array of settings sent to ElasticSearch when creating the index.
42-
*
43-
* @var array
44-
*/
45-
private $settings;
46-
47-
/**
48-
* All types that belong to this index.
49-
*
50-
* @var TypeConfig[]
51-
*/
52-
private $types;
25+
use IndexConfigTrait;
5326

5427
/**
5528
* Indicates if the index should use an alias, allowing an index repopulation to occur
@@ -75,54 +48,6 @@ public function __construct($name, array $types, array $config)
7548
$this->useAlias = isset($config['useAlias']) ? $config['useAlias'] : false;
7649
}
7750

78-
/**
79-
* @return string
80-
*/
81-
public function getElasticSearchName()
82-
{
83-
return $this->elasticSearchName;
84-
}
85-
86-
/**
87-
* @return string
88-
*/
89-
public function getName()
90-
{
91-
return $this->name;
92-
}
93-
94-
/**
95-
* @return array
96-
*/
97-
public function getSettings()
98-
{
99-
return $this->settings;
100-
}
101-
102-
/**
103-
* @param string $typeName
104-
*
105-
* @return TypeConfig
106-
*
107-
* @throws \InvalidArgumentException
108-
*/
109-
public function getType($typeName)
110-
{
111-
if (!array_key_exists($typeName, $this->types)) {
112-
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
113-
}
114-
115-
return $this->types[$typeName];
116-
}
117-
118-
/**
119-
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
120-
*/
121-
public function getTypes()
122-
{
123-
return $this->types;
124-
}
125-
12651
/**
12752
* @return bool
12853
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace FOS\ElasticaBundle\Configuration;
4+
5+
/**
6+
* Interface Index config interface
7+
*
8+
* @author Dmitry Balabka <[email protected]>
9+
*/
10+
interface IndexConfigInterface
11+
{
12+
/**
13+
* @return string
14+
*/
15+
public function getElasticSearchName();
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getName();
21+
22+
/**
23+
* @return array
24+
*/
25+
public function getSettings();
26+
27+
/**
28+
* @param string $typeName
29+
*
30+
* @return TypeConfig
31+
*
32+
* @throws \InvalidArgumentException
33+
*/
34+
public function getType($typeName);
35+
36+
/**
37+
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
38+
*/
39+
public function getTypes();
40+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/*
3+
* This file is part of the OpCart software.
4+
*
5+
* (c) 2015, OpticsPlanet, Inc
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace FOS\ElasticaBundle\Configuration;
12+
13+
/**
14+
* Index configuration trait class
15+
*
16+
* @author Dmitry Balabka <[email protected]>
17+
*/
18+
trait IndexConfigTrait
19+
{
20+
/**
21+
* The name of the index for ElasticSearch.
22+
*
23+
* @var string
24+
*/
25+
private $elasticSearchName;
26+
27+
/**
28+
* The internal name of the index. May not be the same as the name used in ElasticSearch,
29+
* especially if aliases are enabled.
30+
*
31+
* @var string
32+
*/
33+
private $name;
34+
35+
/**
36+
* An array of settings sent to ElasticSearch when creating the index.
37+
*
38+
* @var array
39+
*/
40+
private $settings;
41+
42+
/**
43+
* All types that belong to this index.
44+
*
45+
* @var TypeConfig[]
46+
*/
47+
private $types;
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getElasticSearchName()
53+
{
54+
return $this->elasticSearchName;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getName()
61+
{
62+
return $this->name;
63+
}
64+
65+
/**
66+
* @return array
67+
*/
68+
public function getSettings()
69+
{
70+
return $this->settings;
71+
}
72+
73+
/**
74+
* @param string $typeName
75+
*
76+
* @return TypeConfig
77+
*
78+
* @throws \InvalidArgumentException
79+
*/
80+
public function getType($typeName)
81+
{
82+
if (!array_key_exists($typeName, $this->types)) {
83+
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
84+
}
85+
86+
return $this->types[$typeName];
87+
}
88+
89+
/**
90+
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
91+
*/
92+
public function getTypes()
93+
{
94+
return $this->types;
95+
}
96+
}

0 commit comments

Comments
 (0)