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
|
|
|
|
47
|
|
|
$search = new Search(); |
48
|
|
|
$search->addQuery(new MatchAllQuery()); |
49
|
|
|
$search->setSize($chunkSize); |
50
|
|
|
|
51
|
|
|
$queryParameters = [ |
52
|
|
|
'_source' => true, |
53
|
|
|
'scroll' => '10m', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$searchResults = $manager->search($types, $search->toArray(), $queryParameters); |
57
|
|
|
|
58
|
|
|
$results = new RawIterator( |
59
|
|
|
$searchResults, |
|
|
|
|
60
|
|
|
$manager, |
61
|
|
|
[ |
62
|
|
|
'duration' => $queryParameters['scroll'], |
63
|
|
|
'_scroll_id' => $searchResults['_scroll_id'], |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$progress = new ProgressBar($output, $results->count()); |
68
|
|
|
$progress->setRedrawFrequency(100); |
69
|
|
|
$progress->start(); |
70
|
|
|
|
71
|
|
|
$counter = $fileCounter = 0; |
72
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
73
|
|
|
|
74
|
|
|
$date = date(\DateTime::ISO8601); |
75
|
|
|
$metadata = [ |
76
|
|
|
'count' => $count, |
77
|
|
|
'date' => $date, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$filename = str_replace('.json', '', $filename); |
81
|
|
|
$writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata); |
82
|
|
|
|
83
|
|
|
$file = []; |
84
|
|
|
foreach ($results as $data) { |
85
|
|
|
if ($counter >= $maxLinesInFile) { |
86
|
|
|
$writer->finalize(); |
87
|
|
|
$writer = null; |
|
|
|
|
88
|
|
|
$fileCounter++; |
89
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
90
|
|
|
$metadata = [ |
91
|
|
|
'count' => $count, |
92
|
|
|
'date' => $date, |
93
|
|
|
]; |
94
|
|
|
$writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata); |
95
|
|
|
$counter = 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source'])); |
99
|
|
|
$writer->push($doc); |
100
|
|
|
$file[] = $doc; |
101
|
|
|
$progress->advance(); |
102
|
|
|
$counter++; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$writer->finalize(); |
106
|
|
|
$progress->finish(); |
107
|
|
|
$output->writeln(''); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns real file path. |
112
|
|
|
* |
113
|
|
|
* @param string $filename |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
120
|
|
|
return $filename; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return getcwd() . '/' . $filename; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Prepares JSON writer. |
128
|
|
|
* |
129
|
|
|
* @param string $filename |
130
|
|
|
* @param array $metadata |
131
|
|
|
* |
132
|
|
|
* @return JsonWriter |
133
|
|
|
*/ |
134
|
|
|
protected function getWriter($filename, $metadata) |
135
|
|
|
{ |
136
|
|
|
return new JsonWriter($filename, $metadata); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $resultsCount |
141
|
|
|
* @param int $maxLinesInFile |
142
|
|
|
* @param int $fileCounter |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
|
|
protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter) |
147
|
|
|
{ |
148
|
|
|
$leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile); |
149
|
|
|
if ($leftToInsert <= $maxLinesInFile) { |
150
|
|
|
$count = $leftToInsert; |
151
|
|
|
} else { |
152
|
|
|
$count = $maxLinesInFile; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $count; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: