BulkData.php
2.0 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
<?php
namespace Lib\Es;
/**
* 批量添加数据的类
* @author:dc
* @time 2025/8/6 16:37
* Class BulkData
* @package Lib\Es
*/
class BulkData {
/**
* @var string
*/
private array $data = [];
/**
* 添加数据
* @param string $index 必须全小写
* @param string $_id 指定文档id
* @param array $data
* @author:dc
* @time 2025/8/6 16:41
*/
public function add(string $index,string $_id, array $data){
$index = strtolower($index);
if(empty($this->data[$index])){
$this->data[$index] = [];
}
$this->data[$index][] = [
'_id' => $_id,
'_source' => $data,
];
}
/**
* 获取所有索引名称
* @return array
* @author:dc
* @time 2025/8/6 16:45
*/
public function getIndexs(): array {
return array_keys($this->data);
}
/**
* 转换成es可识别的数据
* @return array
* @author:dc
* @time 2025/8/6 16:42
*/
public function toParams():array {
$params = [];
foreach ($this->data as $index=>$items){
foreach ($items as $item){
$params[] = [
'index' => [
'_index' => $index,
'_id' => $item['_id']
]
];
$params[] = $item['_source'];
}
}
return $params;
}
/**
* 清除 数据
* @author:dc
* @time 2025/8/7 9:55
*/
public function clear(){
$this->data = [];
}
/**
* 是否为空
* @return bool
* @author:dc
* @time 2025/8/7 9:54
*/
public function isEmpty():bool
{
return empty($this->data);
}
/**
* 统计数量
* @return int
* @author:dc
* @time 2025/8/7 9:56
*/
public function total():int
{
return count($this->data);
}
}