thadafinser / es-index-switcher
切换索引,无需搜索中断
v0.1.0
2017-02-22 12:22 UTC
Requires
- php: ~7.0
- elasticsearch/elasticsearch: ^5.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.1
- phpunit/phpunit: ^6.0.7
This package is not auto-updated.
Last update: 2024-09-09 15:16:15 UTC
README
如果您为最终用户搜索使用 Elasticsearch,您绝不想中断您的服务 - 即使您“重新索引”源数据。
这个小脚本解决了这个问题。您的最终用户将始终能够搜索,您可以在并行地重新索引更新的数据的同时进行搜索。
它如何工作?非常简单!您只需在指向当前完整索引的别名上进行搜索。如果您想重新索引数据,当索引完成后,此脚本将更改别名,用户将搜索新数据。
最小示例
索引数据
$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);