1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Service; |
13
|
|
|
|
14
|
|
|
use Elasticsearch\Helper\Iterators\SearchHitIterator; |
15
|
|
|
use Elasticsearch\Helper\Iterators\SearchResponseIterator; |
16
|
|
|
use ONGR\ElasticsearchBundle\Result\RawIterator; |
17
|
|
|
use ONGR\ElasticsearchBundle\Service\Json\JsonWriter; |
18
|
|
|
use ONGR\ElasticsearchDSL\Query\MatchAllQuery; |
19
|
|
|
use ONGR\ElasticsearchDSL\Search; |
20
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ExportService class. |
25
|
|
|
*/ |
26
|
|
|
class ExportService |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Exports es index to provided file. |
30
|
|
|
* |
31
|
|
|
* @param Manager $manager |
32
|
|
|
* @param string $filename |
33
|
|
|
* @param array $types |
34
|
|
|
* @param int $chunkSize |
35
|
|
|
* @param int $maxLinesInFile |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
*/ |
38
|
|
|
public function exportIndex( |
39
|
|
|
Manager $manager, |
40
|
|
|
$filename, |
41
|
|
|
$types, |
42
|
|
|
$chunkSize, |
|
|
|
|
43
|
|
|
OutputInterface $output, |
44
|
|
|
$maxLinesInFile = 300000 |
45
|
|
|
) { |
46
|
|
|
// $params = [ |
|
|
|
|
47
|
|
|
//// 'search_type' => 'scroll', |
48
|
|
|
// 'scroll' => '10m', |
49
|
|
|
//// 'size' => $chunkSize, |
50
|
|
|
// '_source' => true, |
51
|
|
|
// 'body' => [ |
52
|
|
|
// 'query' => [ |
53
|
|
|
// 'match_all' => new \stdClass(), |
54
|
|
|
// ], |
55
|
|
|
// ], |
56
|
|
|
// 'index' => $manager->getIndexName(), |
57
|
|
|
// 'type' => $types, |
58
|
|
|
// ]; |
59
|
|
|
|
60
|
|
|
$search = new Search(); |
61
|
|
|
$search->addQuery(new MatchAllQuery()); |
62
|
|
|
$queryParameters = [ |
63
|
|
|
'_source' => true, |
64
|
|
|
'scroll' => '10m', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$searchResults = $manager->search($types, $search->toArray(), $queryParameters); |
68
|
|
|
|
69
|
|
|
$results = new RawIterator( |
70
|
|
|
$searchResults, |
|
|
|
|
71
|
|
|
$manager, |
72
|
|
|
[ |
73
|
|
|
'duration' => $queryParameters['scroll'], |
74
|
|
|
'_scroll_id' => $searchResults['_scroll_id'], |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
// $results = new SearchHitIterator( |
|
|
|
|
79
|
|
|
// new SearchResponseIterator($manager->getClient(), $params) |
80
|
|
|
// ); |
81
|
|
|
|
82
|
|
|
$progress = new ProgressBar($output, $results->count()); |
83
|
|
|
$progress->setRedrawFrequency(100); |
84
|
|
|
$progress->start(); |
85
|
|
|
|
86
|
|
|
$counter = $fileCounter = 0; |
87
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
88
|
|
|
|
89
|
|
|
$date = date(\DateTime::ISO8601); |
90
|
|
|
$metadata = [ |
91
|
|
|
'count' => $count, |
92
|
|
|
'date' => $date, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$filename = str_replace('.json', '', $filename); |
96
|
|
|
$writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata); |
97
|
|
|
|
98
|
|
|
$file = []; |
99
|
|
|
foreach ($results as $data) { |
100
|
|
|
if ($counter >= $maxLinesInFile) { |
101
|
|
|
$writer->finalize(); |
102
|
|
|
$writer = null; |
|
|
|
|
103
|
|
|
$fileCounter++; |
104
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
105
|
|
|
$metadata = [ |
106
|
|
|
'count' => $count, |
107
|
|
|
'date' => $date, |
108
|
|
|
]; |
109
|
|
|
$writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata); |
110
|
|
|
$counter = 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source'])); |
114
|
|
|
$writer->push($doc); |
115
|
|
|
$file[] = $doc; |
116
|
|
|
$progress->advance(); |
117
|
|
|
$counter++; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$writer->finalize(); |
121
|
|
|
$progress->finish(); |
122
|
|
|
$output->writeln(''); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns real file path. |
127
|
|
|
* |
128
|
|
|
* @param string $filename |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
135
|
|
|
return $filename; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return getcwd() . '/' . $filename; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Prepares JSON writer. |
143
|
|
|
* |
144
|
|
|
* @param string $filename |
145
|
|
|
* @param array $metadata |
146
|
|
|
* |
147
|
|
|
* @return JsonWriter |
148
|
|
|
*/ |
149
|
|
|
protected function getWriter($filename, $metadata) |
150
|
|
|
{ |
151
|
|
|
return new JsonWriter($filename, $metadata); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $resultsCount |
156
|
|
|
* @param int $maxLinesInFile |
157
|
|
|
* @param int $fileCounter |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter) |
162
|
|
|
{ |
163
|
|
|
$leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile); |
164
|
|
|
if ($leftToInsert <= $maxLinesInFile) { |
165
|
|
|
$count = $leftToInsert; |
166
|
|
|
} else { |
167
|
|
|
$count = $maxLinesInFile; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $count; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.