thadafinser/es-index-switcher

Switch your index, without search downtime

v0.1.0 2017-02-22 12:22 UTC

This package is not auto-updated.

Last update: 2024-04-22 13:40:05 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

Latest Stable Version Latest Unstable Version License Total Downloads

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.

Minimal example

Index your 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();

Search

$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);

Update document on the current used index

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);