Skip to content

ThaDafinser/es-index-switcher

Repository files navigation

Elasticsearch index switcher

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

About

Elasticsearch index switcher

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages