PHP code example of thadafinser / es-index-switcher

1. Go to this page and download the library: Download thadafinser/es-index-switcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thadafinser / es-index-switcher example snippets


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


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