Skip to content

Commit e3cd672

Browse files
committed
Add readme documentation.
1 parent 9350b6a commit e3cd672

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Elasticsearch Index Rotator
2+
3+
A library to enable you to safely rotate indexes with no downtime to end users.
4+
5+
[![Build Status](https://travis-ci.com/zumba/elasticsearch-index-rotator.svg?token=zXFxge7zUgReaCncg1CL&branch=master)](https://travis-ci.com/zumba/elasticsearch-index-rotator)
6+
7+
### Why would I use this?
8+
9+
In many situations, Elasticsearch is used as an ephemeral datastore used to take structured or relational data, and make it fast to search on that data. Often this is achieved via scheduled jobs that read data from a permanent datastore (such as MySQL or Postgres) and translate it into an Elasticsearch index.
10+
11+
In many cases, rebuilding an index requires a clean slate so that the entire index is rebuilt. How do you do this without interrupting end users searching on that index? The answer is a rotating index.
12+
13+
![User search disrupted by rebuild](docs/disruption.png)
14+
15+
> Here the user's search is fully disrupted when the index is first removed, and only partially available while the index is being rebuilt. While the index is being rebuilt, users get incomplete data.
16+
17+
![User search contiguous](docs/rotation.png)
18+
19+
> Here the user's search is never disrupted because we construct a new index and after it is built/settled, we change the what index to search by the client.
20+
21+
## Installation
22+
23+
```
24+
composer require zumba\elasticsearch-index-rotator
25+
```
26+
27+
## Usage
28+
29+
```php
30+
<?php
31+
32+
use \Elasticsearch\Client;
33+
use \Zumba\ElastsearchRotator\IndexRotator;
34+
35+
class MyBuilder {
36+
37+
const INDEX_PREFIX = 'pizza_shops';
38+
39+
public function __constructor(\Elasticsearch\Client $client) {
40+
$this->client = $client;
41+
}
42+
43+
public function search($params) {
44+
$indexRotator = new IndexRotator($this->client, static::INDEX_PREFIX);
45+
return $client->search([
46+
'index' => $indexRotator->getPrimaryIndex(), // Get the current primary!
47+
'type' => 'shop',
48+
'body' => $params
49+
]);
50+
}
51+
52+
public function rebuildIndex() {
53+
$indexRotator = new IndexRotator($client, static::INDEX_PREFIX);
54+
$newlyBuiltIndexName = $this->buildIndex($client);
55+
$indexRotator->copyPrimaryIndexToSecondary();
56+
$indexRotator->setPrimaryIndex($newlyBuiltIndexName);
57+
// optionally remove the old index right now
58+
$indexRotator->deleteSecondaryIndexes();
59+
}
60+
61+
private function buildIndex(\Elasticsearch\Client $client) {
62+
$newIndex = static::INDEX_PREFIX . '_' . time();
63+
// get data and build index for `$newIndex`
64+
return $newIndex;
65+
}
66+
67+
}
68+
```

docs/disruption.png

24.6 KB
Loading

docs/rotation.png

35.5 KB
Loading

0 commit comments

Comments
 (0)