1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Console; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkAction; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkQuery; |
5
|
|
|
|
6
|
|
|
abstract class IndexCommand extends AbstractCommand |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'elastic:index'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Indexes data to an Elasticsearch index.'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
abstract public function getData(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract public function getIndex(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
abstract public function getType(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param mixed $item |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
abstract public function getItemBody($item); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param mixed $item |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
abstract public function getItemId($item); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $item |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
abstract public function getItemParent($item); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function handle() |
63
|
|
|
{ |
64
|
|
|
$this->info(sprintf('Indexing data of type "%s" into "%s"', $this->getType(), $this->getIndex())); |
65
|
|
|
|
66
|
|
|
$data = $this->getData(); |
67
|
|
|
|
68
|
|
|
$bar = $this->output->createProgressBar($this->getCount()); |
69
|
|
|
|
70
|
|
|
$bulkQuery = new BulkQuery($this->getBulkSize()); |
71
|
|
|
|
72
|
|
|
foreach ($data as $item) { |
73
|
|
|
$action = new BulkAction(); |
74
|
|
|
|
75
|
|
|
$meta = [ |
76
|
|
|
'_index' => $this->getIndex(), |
77
|
|
|
'_type' => $this->getType(), |
78
|
|
|
'_id' => $this->getItemId($item), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
if (($parent = $this->getItemParent($item)) !== null) { |
82
|
|
|
$meta['_parent'] = $parent; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$action->setAction(BulkAction::ACTION_INDEX, $meta) |
86
|
|
|
->setBody($this->getItemBody($item)); |
87
|
|
|
|
88
|
|
|
$bulkQuery->addAction($action); |
89
|
|
|
|
90
|
|
|
if ($bulkQuery->isReady()) { |
91
|
|
|
$this->elasticsearchService->bulk($bulkQuery->toArray()); |
92
|
|
|
$bulkQuery->reset(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$bar->advance(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($bulkQuery->hasItems()) { |
99
|
|
|
$this->elasticsearchService->bulk($bulkQuery->toArray()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$bar->finish(); |
103
|
|
|
|
104
|
|
|
$this->info("\nDone!"); |
105
|
|
|
|
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return int the bulk size (for bulk indexing) |
111
|
|
|
*/ |
112
|
|
|
protected function getBulkSize() |
113
|
|
|
{ |
114
|
|
|
return BulkQuery::BULK_SIZE_DEFAULT; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the total count. |
119
|
|
|
* |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
protected function getCount() |
123
|
|
|
{ |
124
|
|
|
return count($this->getData()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|