-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDumpIndex.php
More file actions
110 lines (87 loc) · 2.18 KB
/
DumpIndex.php
File metadata and controls
110 lines (87 loc) · 2.18 KB
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
<?php declare(strict_types = 1);
namespace Spameri\Elastic\Model;
class DumpIndex
{
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;
/**
* @var string
*/
private $bulkData;
/**
* @var \Spameri\Elastic\Model\Scroll
*/
private $scroll;
public function __construct(
\Spameri\Elastic\Model\Scroll $scroll
)
{
$this->bulkData = '';
$this->scroll = $scroll;
}
public function setOutput(
\Symfony\Component\Console\Output\OutputInterface $output
): void
{
$this->output = $output;
}
public function execute(
string $index,
string $filename,
?string $type = NULL
): void
{
if ( ! $type) {
$type = $index;
}
$continue = TRUE;
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery();
$elasticQuery->options()->changeSize(5000);
$elasticQuery->options()->startScroll('10m');
$this->output->writeln('Starting dump.');
$progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output);
$progressBar->setFormat('debug');
\Nette\Utils\FileSystem::createDir(\dirname($filename));
while ($continue) {
$this->bulkData = '';
$result = $this->scroll->execute($elasticQuery, $index, $type);
if ($progressBar->getMaxSteps() === 0) {
$progressBar->start($result->stats()->total());
}
/** @var \Spameri\ElasticQuery\Response\Result\Hit $hit */
foreach ($result->hits() as $hit) {
$this->processHit($hit);
}
$progressBar->advance(5000);
$this->writeToFile($filename);
if ($result->stats()->total() <= $progressBar->getProgress()) {
$continue = FALSE;
}
}
$this->scroll->closeScroll($elasticQuery);
$progressBar->finish();
$this->output->writeln('Dump done and result is in file ' . $filename);
}
public function processHit(
\Spameri\ElasticQuery\Response\Result\Hit $hit
): void
{
$bulkData = [
'index' => [
'_index' => $hit->index(),
'_type' => $hit->type(),
'_id' => $hit->id(),
],
];
$this->bulkData .= \json_encode($bulkData) . "\r\n";
$this->bulkData .= \json_encode($hit->source()) . "\r\n";
}
public function writeToFile(
string $filename
): void
{
@\file_put_contents($filename, $this->bulkData, \FILE_APPEND);
}
}