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 ONGR\ElasticsearchBundle\ORM\Manager; |
15
|
|
|
use ONGR\ElasticsearchBundle\Service\Json\JsonReader; |
16
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
17
|
|
|
use Symfony\Component\Console\Helper\ProgressHelper; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ImportService class. |
22
|
|
|
*/ |
23
|
|
|
class ImportService |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Imports Elasticsearch index data. |
27
|
|
|
* |
28
|
|
|
* @param Manager $manager |
29
|
|
|
* @param string $filename |
30
|
|
|
* @param bool $raw |
31
|
|
|
* @param OutputInterface $output |
32
|
|
|
* @param int $bulkSize |
33
|
|
|
* |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public function importIndex($manager, $filename, $raw, OutputInterface $output, $bulkSize = 1000) |
37
|
|
|
{ |
38
|
|
|
if (!$raw) { |
39
|
|
|
throw new \Exception('Currently only raw import is supported. Please set --raw flag to use it.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->executeRawImport($manager, $this->getFilePath($filename), $output, $bulkSize); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Executes a raw import. |
47
|
|
|
* |
48
|
|
|
* @param Manager $manager |
49
|
|
|
* @param string $filename |
50
|
|
|
* @param OutputInterface $output |
51
|
|
|
* @param int $bulkSize |
52
|
|
|
*/ |
53
|
|
|
protected function executeRawImport(Manager $manager, $filename, OutputInterface $output, $bulkSize) |
54
|
|
|
{ |
55
|
|
|
$reader = $this->getReader($manager, $filename, false); |
56
|
|
|
|
57
|
|
View Code Duplication |
if (class_exists('\Symfony\Component\Console\Helper\ProgressBar')) { |
|
|
|
|
58
|
|
|
$progress = new ProgressBar($output, $reader->count()); |
59
|
|
|
$progress->setRedrawFrequency(100); |
60
|
|
|
$progress->start(); |
61
|
|
|
} else { |
62
|
|
|
$progress = new ProgressHelper(); |
|
|
|
|
63
|
|
|
$progress->setRedrawFrequency(100); |
64
|
|
|
$progress->start($output, $reader->count()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($reader as $key => $document) { |
68
|
|
|
$data = $document['_source']; |
69
|
|
|
$data['_id'] = $document['_id']; |
70
|
|
|
|
71
|
|
|
$manager->getConnection()->bulk('index', $document['_type'], $data); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if (($key + 1) % $bulkSize == 0) { |
74
|
|
|
$manager->commit(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$progress->advance(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (($key + 1) % $bulkSize != 0) { |
|
|
|
|
81
|
|
|
$manager->commit(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$progress->finish(); |
85
|
|
|
$output->writeln(''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns a real file path. |
90
|
|
|
* |
91
|
|
|
* @param string $filename |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
98
|
|
|
return $filename; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return realpath(getcwd() . '/' . $filename); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Prepares JSON reader. |
106
|
|
|
* |
107
|
|
|
* @param Manager $manager |
108
|
|
|
* @param string $filename |
109
|
|
|
* @param bool $convertDocuments |
110
|
|
|
* |
111
|
|
|
* @return JsonReader |
112
|
|
|
*/ |
113
|
|
|
protected function getReader($manager, $filename, $convertDocuments) |
114
|
|
|
{ |
115
|
|
|
return new JsonReader($manager, $filename, $convertDocuments); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.