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\Service\Json\JsonWriter; |
17
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ExportService class. |
22
|
|
|
*/ |
23
|
|
|
class ExportService |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Exports es index to provided file. |
27
|
|
|
* |
28
|
|
|
* @param Manager $manager |
29
|
|
|
* @param string $filename |
30
|
|
|
* @param array $types |
31
|
|
|
* @param int $chunkSize |
32
|
|
|
* @param int $maxLinesInFile |
33
|
|
|
* @param OutputInterface $output |
34
|
|
|
*/ |
35
|
|
|
public function exportIndex( |
36
|
|
|
Manager $manager, |
37
|
|
|
$filename, |
38
|
|
|
$types, |
39
|
|
|
$chunkSize, |
40
|
|
|
OutputInterface $output, |
41
|
|
|
$maxLinesInFile = 300000 |
42
|
|
|
) { |
43
|
|
|
$params = [ |
44
|
|
|
'search_type' => 'scan', |
45
|
|
|
'scroll' => '10m', |
46
|
|
|
'size' => $chunkSize, |
47
|
|
|
'source' => true, |
48
|
|
|
'body' => [ |
49
|
|
|
'query' => [ |
50
|
|
|
'match_all' => [], |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
'index' => $manager->getIndexName(), |
54
|
|
|
'type' => $types, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$results = new SearchHitIterator( |
58
|
|
|
new SearchResponseIterator($manager->getClient(), $params) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$progress = new ProgressBar($output, $results->count()); |
62
|
|
|
$progress->setRedrawFrequency(100); |
63
|
|
|
$progress->start(); |
64
|
|
|
|
65
|
|
|
$counter = $fileCounter = 0; |
66
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
67
|
|
|
|
68
|
|
|
$date = date(\DateTime::ISO8601); |
69
|
|
|
$metadata = [ |
70
|
|
|
'count' => $count, |
71
|
|
|
'date' => $date, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$filename = str_replace('.json', '', $filename); |
75
|
|
|
$writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata); |
76
|
|
|
|
77
|
|
|
foreach ($results as $data) { |
78
|
|
|
if ($counter >= $maxLinesInFile) { |
79
|
|
|
$writer->finalize(); |
80
|
|
|
$writer = null; |
|
|
|
|
81
|
|
|
$fileCounter++; |
82
|
|
|
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
83
|
|
|
$metadata = [ |
84
|
|
|
'count' => $count, |
85
|
|
|
'date' => $date, |
86
|
|
|
]; |
87
|
|
|
$writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata); |
88
|
|
|
$counter = 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields'])); |
92
|
|
|
$writer->push($doc); |
93
|
|
|
$progress->advance(); |
94
|
|
|
$counter++; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$writer->finalize(); |
98
|
|
|
$progress->finish(); |
99
|
|
|
$output->writeln(''); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns real file path. |
104
|
|
|
* |
105
|
|
|
* @param string $filename |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
112
|
|
|
return $filename; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return getcwd() . '/' . $filename; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Prepares JSON writer. |
120
|
|
|
* |
121
|
|
|
* @param string $filename |
122
|
|
|
* @param array $metadata |
123
|
|
|
* |
124
|
|
|
* @return JsonWriter |
125
|
|
|
*/ |
126
|
|
|
protected function getWriter($filename, $metadata) |
127
|
|
|
{ |
128
|
|
|
return new JsonWriter($filename, $metadata); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int $resultsCount |
133
|
|
|
* @param int $maxLinesInFile |
134
|
|
|
* @param int $fileCounter |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
|
protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter) |
139
|
|
|
{ |
140
|
|
|
$leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile); |
141
|
|
|
if ($leftToInsert <= $maxLinesInFile) { |
142
|
|
|
$count = $leftToInsert; |
143
|
|
|
} else { |
144
|
|
|
$count = $maxLinesInFile; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $count; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.