1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
<?php
namespace Elastica\Tool;
use Elastica\Bulk;
use Elastica\Index;
use Elastica\Query\MatchAll;
use Elastica\ScanAndScroll;
use Elastica\Search;
use Elastica\Type;
/**
* Functions to move documents and types between indices.
*
* @author Manuel Andreo Garcia <andreo.garcia@gmail.com>
*/
class CrossIndex
{
/**
* Type option.
*
* type: string | string[] | \Elastica\Type | \Elastica\Type[] | null
* default: null (means all types)
*/
const OPTION_TYPE = 'type';
/**
* Query option.
*
* type: see \Elastica\Query::create()
* default: Elastica\Query\MatchAll
*/
const OPTION_QUERY = 'query';
/**
* Expiry time option.
*
* type: string (see Elastica\ScanAndScroll)
* default: '1m'
*/
const OPTION_EXPIRY_TIME = 'expiryTime';
/**
* Size per shard option.
*
* type: int (see Elastica\ScanAndScroll)
* default: 1000
*/
const OPTION_SIZE_PER_SHARD = 'sizePerShard';
/**
* Reindex documents from an old index to a new index.
*
* @link https://www.elastic.co/guide/en/elasticsearch/guide/master/reindex.html
*
* @param \Elastica\Index $oldIndex
* @param \Elastica\Index $newIndex
* @param array $options keys: CrossIndex::OPTION_* constants
*
* @return \Elastica\Index The new index object
*/
public static function reindex(
Index $oldIndex,
Index $newIndex,
array $options = array()
) {
// prepare search
$search = new Search($oldIndex->getClient());
$options = array_merge(
array(
self::OPTION_TYPE => null,
self::OPTION_QUERY => new MatchAll(),
self::OPTION_EXPIRY_TIME => '1m',
self::OPTION_SIZE_PER_SHARD => 1000,
),
$options
);
$search->addIndex($oldIndex);
if (isset($options[self::OPTION_TYPE])) {
$type = $options[self::OPTION_TYPE];
$search->addTypes(is_array($type) ? $type : array($type));
}
$search->setQuery($options[self::OPTION_QUERY]);
// search on old index and bulk insert in new index
$scanAndScroll = new ScanAndScroll(
$search,
$options[self::OPTION_EXPIRY_TIME],
$options[self::OPTION_SIZE_PER_SHARD]
);
foreach ($scanAndScroll as $resultSet) {
$bulk = new Bulk($newIndex->getClient());
$bulk->setIndex($newIndex);
foreach ($resultSet as $result) {
$action = new Bulk\Action();
$action->setType($result->getType());
$action->setId($result->getId());
$action->setSource($result->getData());
$bulk->addAction($action);
}
$bulk->send();
}
$newIndex->refresh();
return $newIndex;
}
/**
* Copies type mappings and documents from an old index to a new index.
*
* @see \Elastica\Tool\CrossIndex::reindex()
*
* @param \Elastica\Index $oldIndex
* @param \Elastica\Index $newIndex
* @param array $options keys: CrossIndex::OPTION_* constants
*
* @return \Elastica\Index The new index object
*/
public static function copy(
Index $oldIndex,
Index $newIndex,
array $options = array()
) {
// normalize types to array of string
$types = array();
if (isset($options[self::OPTION_TYPE])) {
$types = $options[self::OPTION_TYPE];
$types = is_array($types) ? $types : array($types);
$types = array_map(
function ($type) {
if ($type instanceof Type) {
$type = $type->getName();
}
return (string) $type;
},
$types
);
}
// copy mapping
foreach ($oldIndex->getMapping() as $type => $mapping) {
if (!empty($types) && !in_array($type, $types, true)) {
continue;
}
$type = new Type($newIndex, $type);
$type->setMapping($mapping['properties']);
}
// copy documents
return self::reindex($oldIndex, $newIndex, $options);
}
}
|