Skip to content

Commit ad9cab9

Browse files
committed
Merge pull request #902 from oleg-andreyev/populate-events-md
adding cookbook for populate events
2 parents 6628a22 + 541a8d1 commit ad9cab9

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Populate Events
2+
===============
3+
4+
Often when you need import large amount data and keep performance of your storage,
5+
you'll use [bulk insert](https://en.wikipedia.org/wiki/Bulk_insert) and Elasticsearch is not an exception.
6+
But in case of production Elasticsearch, you may suffer of high load on data-node,
7+
because Elasticsearch performs async refresh of shard(s).
8+
9+
According to [official documentation about bulk indexing](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html#bulk)
10+
to squeeze out more performance out of bulk indexing, you must set `refresh_interval` to `-1` be executing:
11+
12+
```bash
13+
curl -XPUT localhost:9200/test/_settings -d '{
14+
"index" : {
15+
"refresh_interval" : "-1"
16+
} }'
17+
```
18+
19+
Then, once bulk indexing is done, settings can be updated (back to default)
20+
21+
```bash
22+
curl -XPUT localhost:9200/test/_settings -d '{
23+
"index" : {
24+
"refresh_interval" : "1s"
25+
} }'
26+
```
27+
28+
And _optimize should be called:
29+
30+
```bash
31+
curl -XPOST 'http://localhost:9200/test/_optimize?max_num_segments=5'
32+
```
33+
34+
Everything seems to be straightforward, but you'll how this can be achieved with FOSElasticaBundle?
35+
For this purpose `PRE_INDEX_POPULATE`, `POST_INDEX_POPULATE`, `PRE_TYPE_POPULATE` and `POST_TYPE_POPULATE` were introduced, they allow you to monitor and hook into the process.
36+
37+
Now let's implement `PopulateListener` by creating `AppBundle\EventListener\PopulateListener`:
38+
39+
```php
40+
<?php
41+
namespace AppBundle\EventListener;
42+
43+
use FOS\ElasticaBundle\Event\IndexPopulateEvent;
44+
use FOS\ElasticaBundle\Index\IndexManager;
45+
46+
class PopulateListener
47+
{
48+
/**
49+
* @var IndexManager
50+
*/
51+
private $indexManager;
52+
53+
/**
54+
* @param IndexManager $indexManager
55+
*/
56+
public function __construct(IndexManager $indexManager)
57+
{
58+
$this->indexManager = $indexManager;
59+
}
60+
61+
public function preIndexPopulate(IndexPopulateEvent $event)
62+
{
63+
$index = $this->indexManager->getIndex($event->getIndex());
64+
$settings = $index->getSettings();
65+
$settings->setRefreshInterval(-1);
66+
}
67+
68+
public function postIndexPopulate(IndexPopulateEvent $event)
69+
{
70+
$index = $this->indexManager->getIndex($event->getIndex());
71+
$settings = $index->getSettings();
72+
$index->optimize(['max_num_segments' => 5]);
73+
$settings->setRefreshInterval('1s');
74+
}
75+
}
76+
```
77+
78+
Declare your listener and register event(s):
79+
80+
```xml
81+
<service id="app.event_listener.populate_listener" class="AppBundle\EventListener\PopulateListener">
82+
<tag name="kernel.event_listener" event="elastica.index.index_pre_populate" method="preIndexPopulate"/>
83+
<tag name="kernel.event_listener" event="elastica.index.index_post_populate" method="postIndexPopulate"/>
84+
<argument type="service" id="fos_elastica.index_manager"/>
85+
</service>
86+
```
87+
88+
and pretty much that's it!
89+
90+
Other events that were indroduced: `PRE_INDEX_RESET`, `POST_INDEX_RESET`, `PRE_TYPE_RESET` and `POST_TYPE_RESET` are working in similar way

0 commit comments

Comments
 (0)