Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.05% |
27 / 38 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
| ReindexRequest | |
71.05% |
27 / 38 |
|
54.55% |
6 / 11 |
20.46 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| setRemoteInfo | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setScript | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setSize | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setRequestsPerSecond | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setSlices | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| reindexTask | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| reindex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| request | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
3.79 | |||
| toArray | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
| asSourceDest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Elastica; |
| 4 | |
| 5 | use Elastica\Client; |
| 6 | use Elastica\Index; |
| 7 | use Elastica\Request; |
| 8 | use Elastica\Response; |
| 9 | use InvalidArgumentException; |
| 10 | use RuntimeException; |
| 11 | |
| 12 | class ReindexRequest { |
| 13 | /** @var array */ |
| 14 | private $source; |
| 15 | /** @var array */ |
| 16 | private $dest; |
| 17 | /** @var array|null */ |
| 18 | private $script; |
| 19 | /** @var int|null */ |
| 20 | private $size; |
| 21 | /** @var int */ |
| 22 | private $requestsPerSecond = -1; |
| 23 | /** @var int */ |
| 24 | private $slices = 1; |
| 25 | /** @var Client */ |
| 26 | private $client; |
| 27 | |
| 28 | /** |
| 29 | * @param Index $source |
| 30 | * @param Index $dest |
| 31 | * @param int $chunkSize |
| 32 | */ |
| 33 | public function __construct( Index $source, Index $dest, $chunkSize = 100 ) { |
| 34 | $this->source = $this->asSourceDest( $source ); |
| 35 | $this->source['size'] = $chunkSize; |
| 36 | $this->dest = $this->asSourceDest( $dest ); |
| 37 | $this->client = $dest->getClient(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param array $remote |
| 42 | * @return $this |
| 43 | */ |
| 44 | public function setRemoteInfo( array $remote ) { |
| 45 | $this->source['remote'] = $remote; |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param array $script |
| 51 | * @return $this |
| 52 | */ |
| 53 | public function setScript( array $script ) { |
| 54 | $this->script = $script; |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The number of documents to reindex |
| 60 | * |
| 61 | * @param int $size |
| 62 | * @return $this |
| 63 | */ |
| 64 | public function setSize( $size ) { |
| 65 | $this->size = $size; |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param int $rps |
| 71 | * @return $this |
| 72 | */ |
| 73 | public function setRequestsPerSecond( $rps ) { |
| 74 | $this->requestsPerSecond = $rps; |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param int $slices |
| 80 | * @return $this |
| 81 | */ |
| 82 | public function setSlices( $slices ) { |
| 83 | $this->slices = $slices; |
| 84 | return $this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return ReindexTask |
| 89 | */ |
| 90 | public function reindexTask() { |
| 91 | $response = $this->request( [ |
| 92 | 'wait_for_completion' => 'false', |
| 93 | ] ); |
| 94 | |
| 95 | return new ReindexTask( $this->client, $response->getData()['task'] ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return ReindexResponse |
| 100 | */ |
| 101 | public function reindex() { |
| 102 | return new ReindexResponse( $this->request()->getData() ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param array $query |
| 107 | * @return Response |
| 108 | */ |
| 109 | private function request( array $query = [] ) { |
| 110 | $query['requests_per_second'] = $this->requestsPerSecond; |
| 111 | $query['slices'] = $this->slices; |
| 112 | $response = $this->client->request( '_reindex', Request::POST, $this->toArray(), $query ); |
| 113 | |
| 114 | if ( !$response->isOK() ) { |
| 115 | throw new RuntimeException( $response->hasError() |
| 116 | ? 'Failed reindex request: ' . $response->getErrorMessage() |
| 117 | : 'Unknown reindex failure: ' . $response->getStatus() |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | return $response; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @return array |
| 126 | */ |
| 127 | public function toArray() { |
| 128 | $request = [ |
| 129 | 'source' => $this->source, |
| 130 | 'dest' => $this->dest, |
| 131 | ]; |
| 132 | if ( $this->script ) { |
| 133 | $request['script'] = $this->script; |
| 134 | } |
| 135 | if ( $this->size ) { |
| 136 | $request['size'] = $this->size; |
| 137 | } |
| 138 | |
| 139 | return $request; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param Index $input |
| 144 | * @return array |
| 145 | * @throws InvalidArgumentException |
| 146 | */ |
| 147 | private function asSourceDest( Index $input ) { |
| 148 | return [ 'index' => $input->getName() ]; |
| 149 | } |
| 150 | |
| 151 | } |