If you use Elasticsearch for a end user search, you never want to interrupt your service - even when you "reindex" your data from source.
This small scripts solves this issue. Your end users will always be able to search and you can reindex in parallel your updated data.
How does it work? It's quiet simple! You just need to search over an alias which points to the current full index. If you want to reindex your data, this script will change the alias when the indexing has finished and the user will search over the new data.
$hosts = [
[
'host' => '...',
'port' => '9200',
'scheme' => 'http',
'user' => '...',
'pass' => '...'
]
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$es = new EsIndexSwitcher($client, 'test_alias', 'testing');
/*
* Create the index itself
*/
$result = $es->createNewIndex();
/*
* Add your documents to the index!
*/
$params = [
'index' => $es->getNewIndexName(),
'type' => 'my_document',
'body' => [
'field1' => 'test'
]
];
$response = $client->index($params);
/*
* Create/update alias and remove all old indices
*/
$es->finish();
$hosts = [
[
'host' => '...',
'port' => '9200',
'scheme' => 'http',
'user' => '...',
'pass' => '...'
]
];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$es = new EsIndexSwitcher($client, 'test_alias', 'testing');
/*
* Add more documents to the old index (by using the alias)
*/
$params = [
'index' => $es->getAlias(),
'type' => 'my_document',
'body' => [
]
];
$response = $client->search($params);
var_dump($response);
Maybe you don't want to create a new index on every small change.
Just add your document over the alias
$client = ClientBuilder::create()->setHosts($hosts)->build();
$es = new EsIndexSwitcher($client, 'test_alias', 'testing');
/*
* Add more documents to the old index (by using the alias)
*/
$params = [
'index' => $es->getAlias(),
'type' => 'my_document',
'body' => [
'field1' => 'test2'
]
];
$response = $client->index($params);
var_dump($response);