1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @author Mihkel Viilveer <[email protected]> |
5
|
|
|
* @date 25.08.2014 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace opus\elastic\components; |
9
|
|
|
use yii\base\ErrorException; |
10
|
|
|
use yii\caching\Cache; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Command |
15
|
|
|
* |
16
|
|
|
* @author Mihkel Viilveer <[email protected]> |
17
|
|
|
* @package common\components\elasticsearch |
18
|
|
|
*/ |
19
|
|
|
class Command extends \yii\elasticsearch\Command |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param $index |
23
|
|
|
* @param $type |
24
|
|
|
* @param $data |
25
|
|
|
* @param array $options |
26
|
|
|
* @return mixed |
27
|
|
|
* @throws ErrorException |
28
|
|
|
*/ |
29
|
|
|
public function bulk($index, $type, $data, $options = []) |
30
|
|
|
{ |
31
|
|
|
if (empty($data)) { |
32
|
|
|
$body = '{}'; |
33
|
|
|
} else { |
34
|
|
|
$body = is_array($data) ? $this->formatBulkData($index, $type, $data) : $data; |
35
|
|
|
} |
36
|
|
|
return $this->db->post([$index, $type, '_bulk'], $options, $body); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $index |
41
|
|
|
* @param $type |
42
|
|
|
* @param $data |
43
|
|
|
* @return string |
44
|
|
|
* @throws ErrorException |
45
|
|
|
*/ |
46
|
|
|
private function formatBulkData($index, $type, $data) |
47
|
|
|
{ |
48
|
|
|
$formattedData = []; |
49
|
|
|
|
50
|
|
|
foreach ($data as $operation => $objects) { |
51
|
|
|
foreach ($objects as $object) { |
52
|
|
|
switch ($operation) { |
53
|
|
|
case 'INDEX' : |
|
|
|
|
54
|
|
|
$formattedData[] = Json::encode( |
55
|
|
|
[ |
56
|
|
|
'index' => [ |
57
|
|
|
'_index' => $index, |
58
|
|
|
'_type' => $type, |
59
|
|
|
'_id' => $object['id'] |
60
|
|
|
] |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
$formattedData[] = Json::encode($object); |
64
|
|
|
break; |
65
|
|
|
|
66
|
|
|
case 'DELETE' : |
|
|
|
|
67
|
|
|
$formattedData[] = Json::encode( |
68
|
|
|
[ |
69
|
|
|
'delete' => [ |
70
|
|
|
'_index' => $index, |
71
|
|
|
'_type' => $type, |
72
|
|
|
'_id' => $object['id'] |
73
|
|
|
] |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
break; |
77
|
|
|
default: |
78
|
|
|
throw new ErrorException( |
79
|
|
|
'Unknown operation ' . $operation |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
return implode("\n", $formattedData) . "\n"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* getMapping with caching support |
89
|
|
|
* |
90
|
|
|
* @param string $index |
91
|
|
|
* @param string $type |
92
|
|
|
* @param string $data |
93
|
|
|
* @throws \yii\base\InvalidConfigException |
94
|
|
|
* @return array |
95
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html |
96
|
|
|
*/ |
97
|
|
|
public function getCachedMapping($index = '_all', $type = '_all', $data = 'properties') |
98
|
|
|
{ |
99
|
|
|
$command = \Yii::$app->elasticsearch; |
100
|
|
|
/* @var $cache Cache */ |
101
|
|
|
$cache = is_string($command->cache) ? \Yii::$app->get($command->cache, false) : $command->cache; |
102
|
|
|
if ($cache instanceof Cache) { |
103
|
|
|
$key = sprintf('%s_%s_%s', $index, $type, $data); |
104
|
|
|
if (($mapping = $cache->get($key)) === false) { |
105
|
|
|
$mapping = $this->getMapping($index, $type, $data); |
106
|
|
|
if ($mapping !== null) { |
107
|
|
|
$cache->set($key, $mapping); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
else { |
112
|
|
|
$mapping = $this->getMapping($index, $type, $data); |
113
|
|
|
} |
114
|
|
|
return $mapping; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Overridden to return exact type mapping if possible |
119
|
|
|
* |
120
|
|
|
* @param string $index |
121
|
|
|
* @param string $type |
122
|
|
|
* @param string $data Type of data to be retrieved |
123
|
|
|
* @return array |
124
|
|
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html |
125
|
|
|
*/ |
126
|
|
|
public function getMapping($index = '_all', $type = '_all', $data = 'properties') |
127
|
|
|
{ |
128
|
|
|
$mapping = parent::getMapping($index, $type); |
129
|
|
|
if ($index !== '_all' && $type !== '_all') { |
130
|
|
|
$mapping = $mapping[$index]['mappings'][$type][$data]; |
131
|
|
|
} |
132
|
|
|
return $mapping; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.