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\ElasticsearchDSL\Query\MatchAllQuery; |
18
|
|
|
use ONGR\ElasticsearchBundle\Service\Json\JsonWriter; |
19
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ExportService class. |
24
|
|
|
*/ |
25
|
|
|
class ExportService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Exports es index to provided file. |
29
|
|
|
* |
30
|
|
|
* @param Manager $manager |
31
|
|
|
* @param string $filename |
32
|
|
|
* @param array $types |
33
|
|
|
* @param int $chunkSize |
34
|
|
|
* @param OutputInterface $output |
35
|
|
|
*/ |
36
|
|
|
public function exportIndex(Manager $manager, $filename, $types, $chunkSize, OutputInterface $output) |
37
|
|
|
{ |
38
|
|
|
$params = [ |
39
|
|
|
'search_type' => 'scan', |
40
|
|
|
'scroll' => '5m', |
41
|
|
|
'size' => $chunkSize, |
42
|
|
|
'source' => true, |
43
|
|
|
'body' => [ |
44
|
|
|
'query' => [ |
45
|
|
|
'match_all' => [], |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
'index' => $manager->getIndexName(), |
49
|
|
|
'type' => $types, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$results = new SearchHitIterator( |
53
|
|
|
new SearchResponseIterator($manager->getClient(), $params) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$progress = new ProgressBar($output, $results->count()); |
57
|
|
|
$progress->setRedrawFrequency(100); |
58
|
|
|
$progress->start(); |
59
|
|
|
|
60
|
|
|
$metadata = [ |
61
|
|
|
'count' => $results->count(), |
62
|
|
|
'date' => date(\DateTime::ISO8601), |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$writer = $this->getWriter($this->getFilePath($filename), $metadata); |
66
|
|
|
|
67
|
|
|
foreach ($results as $data) { |
68
|
|
|
$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields'])); |
69
|
|
|
$writer->push($doc); |
70
|
|
|
$progress->advance(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$writer->finalize(); |
74
|
|
|
$progress->finish(); |
75
|
|
|
$output->writeln(''); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns real file path. |
80
|
|
|
* |
81
|
|
|
* @param string $filename |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
88
|
|
|
return $filename; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return getcwd() . '/' . $filename; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Prepares JSON writer. |
96
|
|
|
* |
97
|
|
|
* @param string $filename |
98
|
|
|
* @param array $metadata |
99
|
|
|
* |
100
|
|
|
* @return JsonWriter |
101
|
|
|
*/ |
102
|
|
|
protected function getWriter($filename, $metadata) |
103
|
|
|
{ |
104
|
|
|
return new JsonWriter($filename, $metadata); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.