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