Skip to content

Instantly share code, notes, and snippets.

@phroggyy
Created May 19, 2016 20:42
Show Gist options
  • Save phroggyy/219ef4f709988e698848328f90810f1c to your computer and use it in GitHub Desktop.
Save phroggyy/219ef4f709988e698848328f90810f1c to your computer and use it in GitHub Desktop.
A trait to ease the process of migrating to a new elastic search index
<?php
namespace Sparky\Domain;
use Carbon\Carbon;
use Sparky\ElasticSearchModel;
trait MigratesElasticSearchIndices
{
/**
* Migrate a new version of an index.
*
* @param \Sparky\ElasticSearchModel $model
* @param int $version
* @param array $properties
*/
public function migrateIndex(ElasticSearchModel $model, int $version, array $properties)
{
$client = app('elasticsearch');
$alias = $model->getElasticIndex();
$type = $model->getElasticType();
$index = $alias.'-'.$version;
$currentIndex = $alias.'-'.$version-1;
$description = [
'mappings' => [
$type => [
'properties' => $properties,
],
],
];
$client->indices()->create($description);
// Reindex the existing data
$timestamp = null;
while (true) {
if ($timestamp) {
$query = [
'filtered' => [
'query' => [
'match_all' => [],
],
'filter' => [
'range' => [
'created_at' => [
'gte' => $timestamp,
],
],
],
],
];
} else {
$query = ['match_all' => []];
}
$timestamp = Carbon::now()->format('Y-m-d H:i:s');
$search = $client->search([
'search_type' => 'scan',
'scroll' => '1m',
'size' => 1000,
'index' => $currentIndex,
'sort' => ['_doc'],
'body' => [
'query' => [
$query
]
]
]);
$scrollId = $search['_scroll_id'];
while (true) {
$response = $client->scroll([
'scroll_id' => $scrollId,
'scroll' => '1m',
]);
if (! count($response['hits']['hits'])) {
break;
}
$scrollId = $response['_scroll_id'];
$results = array_map(function ($result) {
return ['create' => $result['_source']];
}, $response['hits']['hits']);
$client->bulk([
'index' => $index,
'type' => $type,
'body' => $results,
]);
}
if ($scrollId == $search['_scroll_id']) {
break;
}
}
$client->indices()->deleteAlias([
'index' => $currentIndex,
'name' => $alias,
]);
$client->indices()->putAlias([
'index' => $index,
'name' => $alias,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment