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\Service\Json\JsonReader; |
15
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ImportService class. |
20
|
|
|
*/ |
21
|
|
|
class ImportService |
22
|
|
|
{ |
23
|
|
|
public function importIndex( |
24
|
|
|
IndexService $index, |
25
|
|
|
$filename, |
26
|
|
|
OutputInterface $output, |
27
|
|
|
$options |
28
|
|
|
) { |
29
|
|
|
$reader = $this->getReader($index, $this->getFilePath($filename), $options); |
30
|
|
|
|
31
|
|
|
$progress = new ProgressBar($output, $reader->count()); |
32
|
|
|
$progress->setRedrawFrequency(100); |
33
|
|
|
$progress->start(); |
34
|
|
|
|
35
|
|
|
$bulkSize = $options['bulk-size']; |
36
|
|
|
foreach ($reader as $key => $document) { |
37
|
|
|
$data = $document['_source']; |
38
|
|
|
$data['_id'] = $document['_id']; |
39
|
|
|
|
40
|
|
|
if (array_key_exists('fields', $document)) { |
41
|
|
|
$data = array_merge($document['fields'], $data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$index->bulk('index', $data); |
45
|
|
|
|
46
|
|
|
if (($key + 1) % $bulkSize == 0) { |
47
|
|
|
$index->commit(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$progress->advance(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$index->commit(); |
54
|
|
|
|
55
|
|
|
$progress->finish(); |
56
|
|
|
$output->writeln(''); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a real file path. |
61
|
|
|
* |
62
|
|
|
* @param string $filename |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
protected function getFilePath($filename) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($filename[0] == '/' || strstr($filename, ':') !== false) { |
69
|
|
|
return $filename; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return realpath(getcwd() . '/' . $filename); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getReader(IndexService $manager, $filename, $options): JsonReader |
76
|
|
|
{ |
77
|
|
|
return new JsonReader($manager, $filename, $options); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.