|
1
|
|
|
<?php |
|
2
|
|
|
namespace Elastica; |
|
3
|
|
|
|
|
4
|
|
|
use Elastica\Query\AbstractQuery; |
|
5
|
|
|
|
|
6
|
|
|
class Reindex |
|
7
|
|
|
{ |
|
8
|
|
|
const VERSION_TYPE = 'version_type'; |
|
9
|
|
|
const VERSION_TYPE_INTERNAL = 'internal'; |
|
10
|
|
|
const VERSION_TYPE_EXTERNAL = 'external'; |
|
11
|
|
|
const OPERATION_TYPE = 'op_type'; |
|
12
|
|
|
const OPERATION_TYPE_CREATE = 'create'; |
|
13
|
|
|
const CONFLICTS = 'conflicts'; |
|
14
|
|
|
const CONFLICTS_PROCEED = 'proceed'; |
|
15
|
|
|
const TYPE = 'type'; |
|
16
|
|
|
const SIZE = 'size'; |
|
17
|
|
|
const QUERY = 'query'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Index |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $_oldIndex; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Index |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_newIndex; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_options; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(Index $oldIndex, Index $newIndex, array $options = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_oldIndex = $oldIndex; |
|
37
|
|
|
$this->_newIndex = $newIndex; |
|
38
|
|
|
$this->_options = $options; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function run() |
|
42
|
|
|
{ |
|
43
|
|
|
$body = $this->_getBody($this->_oldIndex, $this->_newIndex, $this->_options); |
|
44
|
|
|
|
|
45
|
|
|
$reindexEndpoint = new \Elasticsearch\Endpoints\Reindex(); |
|
46
|
|
|
$reindexEndpoint->setBody($body); |
|
47
|
|
|
|
|
48
|
|
|
$this->_oldIndex->getClient()->requestEndpoint($reindexEndpoint); |
|
49
|
|
|
$this->_newIndex->refresh(); |
|
50
|
|
|
|
|
51
|
|
|
return $this->_newIndex; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function _getBody($oldIndex, $newIndex, $options) |
|
55
|
|
|
{ |
|
56
|
|
|
$body = array_merge([ |
|
57
|
|
|
'source' => $this->_getSourcePartBody($oldIndex, $options), |
|
58
|
|
|
'dest' => $this->_getDestPartBody($newIndex, $options) |
|
59
|
|
|
], $this->_resolveBodyOptions($options)); |
|
60
|
|
|
|
|
61
|
|
|
return $body; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function _getSourcePartBody(Index $index, array $options) |
|
65
|
|
|
{ |
|
66
|
|
|
$sourceBody = array_merge([ |
|
67
|
|
|
'index' => $index->getName(), |
|
68
|
|
|
], $this->_resolveSourceOptions($options)); |
|
69
|
|
|
|
|
70
|
|
|
$sourceBody = $this->_setSourceQuery($sourceBody); |
|
71
|
|
|
$sourceBody = $this->_setSourceType($sourceBody); |
|
72
|
|
|
|
|
73
|
|
|
return $sourceBody; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function _getDestPartBody(Index $index, array $options) |
|
77
|
|
|
{ |
|
78
|
|
|
return array_merge([ |
|
79
|
|
|
'index' => $index->getName(), |
|
80
|
|
|
], $this->_resolveDestOptions($options)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function _resolveSourceOptions(array $options) |
|
84
|
|
|
{ |
|
85
|
|
|
return array_intersect_key($options, [ |
|
86
|
|
|
self::TYPE => null, |
|
87
|
|
|
self::QUERY => null, |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function _resolveDestOptions(array $options) |
|
92
|
|
|
{ |
|
93
|
|
|
return array_intersect_key($options, [ |
|
94
|
|
|
self::VERSION_TYPE => null, |
|
95
|
|
|
self::OPERATION_TYPE => null, |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function _resolveBodyOptions(array $options) |
|
100
|
|
|
{ |
|
101
|
|
|
return array_intersect_key($options, [ |
|
102
|
|
|
self::SIZE => null, |
|
103
|
|
|
self::CONFLICTS => null, |
|
104
|
|
|
]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $sourceBody |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
private function _setSourceQuery(array $sourceBody) |
|
113
|
|
|
{ |
|
114
|
|
|
if (isset($sourceBody[self::QUERY]) && $sourceBody[self::QUERY] instanceof AbstractQuery) { |
|
115
|
|
|
$sourceBody[self::QUERY] = $sourceBody[self::QUERY]->toArray(); |
|
116
|
|
|
} |
|
117
|
|
|
return $sourceBody; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $sourceBody |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
private function _setSourceType(array $sourceBody) |
|
126
|
|
|
{ |
|
127
|
|
|
if (isset($sourceBody[self::TYPE]) && !is_array($sourceBody[self::TYPE])) { |
|
128
|
|
|
$sourceBody[self::TYPE] = [$sourceBody[self::TYPE]]; |
|
129
|
|
|
} |
|
130
|
|
|
if (isset($sourceBody[self::TYPE])) { |
|
131
|
|
|
foreach ($sourceBody[self::TYPE] as $key => $type) { |
|
132
|
|
|
if ($type instanceof Type) { |
|
133
|
|
|
$sourceBody[self::TYPE][$key] = $type->getName(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
return $sourceBody; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|