From 5d2bf477a4695044cedffbfe9e62dd273a02996a Mon Sep 17 00:00:00 2001 From: Federico Panini Date: Thu, 22 Nov 2018 07:10:57 +0100 Subject: [PATCH] Indices Recovery : provides insight into on-going index shard recoveries --- CHANGELOG.md | 1 + lib/Elastica/Index.php | 11 +++ lib/Elastica/Index/Recovery.php | 100 +++++++++++++++++++++++++++ test/Elastica/Index/RecoveryTest.php | 24 +++++++ 4 files changed, 136 insertions(+) create mode 100644 lib/Elastica/Index/Recovery.php create mode 100644 test/Elastica/Index/RecoveryTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fb1cfee5ec..0208cb282c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file based on the * Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients * [Field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) param for `Elastica\Query\FunctionScore::addRandomScoreFunction` +* [Index Recovery](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html) : the indices recovery API provides insight into on-going index shard recoveries. It was never been implemented into Elastica. [#1537](https://github.com/ruflin/Elastica/pull/1537) ### Improvements diff --git a/lib/Elastica/Index.php b/lib/Elastica/Index.php index af4c0d75a5..b9bcf878f0 100644 --- a/lib/Elastica/Index.php +++ b/lib/Elastica/Index.php @@ -5,6 +5,7 @@ use Elastica\Exception\ResponseException; use Elastica\Index\Settings as IndexSettings; use Elastica\Index\Stats as IndexStats; +use Elastica\Index\Recovery as IndexRecovery; use Elastica\ResultSet\BuilderInterface; use Elastica\Script\AbstractScript; use Elasticsearch\Endpoints\AbstractEndpoint; @@ -87,6 +88,16 @@ public function getStats() return new IndexStats($this); } + /** + * Return Index Recovery. + * + * @return \Elastica\Index\Recovery + */ + public function getRecovery() + { + return new IndexRecovery($this); + } + /** * Gets all the type mappings for an index. * diff --git a/lib/Elastica/Index/Recovery.php b/lib/Elastica/Index/Recovery.php new file mode 100644 index 0000000000..f2e86eb7b9 --- /dev/null +++ b/lib/Elastica/Index/Recovery.php @@ -0,0 +1,100 @@ + + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html + */ +class Recovery +{ + /** + * Response. + * + * @var \Elastica\Response Response object + */ + protected $_response; + + /** + * Recovery info. + * + * @var array Recovery info + */ + protected $_data = []; + + /** + * Index. + * + * @var \Elastica\Index Index object + */ + protected $_index; + + /** + * Construct. + * + * @param \Elastica\Index $index Index object + */ + public function __construct(BaseIndex $index) + { + $this->_index = $index; + $this->refresh(); + } + + /** + * Returns the index object. + * + * @return \Elastica\Index Index object + */ + public function getIndex() + { + return $this->_index; + } + + /** + * Returns response object. + * + * @return \Elastica\Response Response object + */ + public function getResponse() + { + return $this->_response; + } + + /** + * Returns the raw recovery info. + * + * @return array Recovery info + */ + public function getData() + { + return $this->_data; + } + + /** + * @return mixed + */ + protected function getRecoveryData() + { + $endpoint = new \Elasticsearch\Endpoints\Indices\Recovery(); + + $this->_response = $this->getIndex()->requestEndpoint($endpoint); + + return $this->getResponse()->getData(); + } + + /** + * Retrieve the Recovery data + * + * @return $this + */ + public function refresh() + { + $this->_data = $this->getRecoveryData(); + + return $this; + } +} \ No newline at end of file diff --git a/test/Elastica/Index/RecoveryTest.php b/test/Elastica/Index/RecoveryTest.php new file mode 100644 index 0000000000..5a6e478ab3 --- /dev/null +++ b/test/Elastica/Index/RecoveryTest.php @@ -0,0 +1,24 @@ +_getClient(); + $index = $client->getIndex($indexName); + $index->create([], true); + $recovery = $index->getRecovery(); + $this->assertInstanceOf(Recovery::class, $recovery); + + $this->assertTrue($recovery->getResponse()->isOk()); + } +}