1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard\Business\Repository; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Triadev\Leopard\Business\Helper\IsModelSearchable; |
7
|
|
|
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract; |
8
|
|
|
use Triadev\Leopard\Facade\Leopard; |
9
|
|
|
use Triadev\Leopard\Searchable; |
10
|
|
|
|
11
|
|
|
class ElasticsearchRepository implements ElasticsearchRepositoryContract |
12
|
|
|
{ |
13
|
|
|
use IsModelSearchable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Save |
17
|
|
|
* |
18
|
|
|
* @param Model|Searchable $model |
19
|
|
|
* @return array |
20
|
|
|
* |
21
|
|
|
* @throws \InvalidArgumentException |
22
|
|
|
*/ |
23
|
6 |
|
public function save(Model $model): array |
24
|
|
|
{ |
25
|
6 |
|
$this->isModelSearchable($model); |
26
|
|
|
|
27
|
6 |
|
return Leopard::indexStatement([ |
28
|
6 |
|
'index' => $model->getDocumentIndex(), |
29
|
6 |
|
'type' => $model->getDocumentType(), |
30
|
6 |
|
'id' => $model->getKey(), |
31
|
6 |
|
'body' => $model->getDocumentData() |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Update |
37
|
|
|
* |
38
|
|
|
* @param Model|Searchable $model |
39
|
|
|
* @return array |
40
|
|
|
* |
41
|
|
|
* @throws \InvalidArgumentException |
42
|
|
|
*/ |
43
|
1 |
|
public function update(Model $model): array |
44
|
|
|
{ |
45
|
1 |
|
$this->isModelSearchable($model); |
46
|
|
|
|
47
|
1 |
|
return Leopard::updateStatement([ |
48
|
1 |
|
'index' => $model->getDocumentIndex(), |
49
|
1 |
|
'type' => $model->getDocumentType(), |
50
|
1 |
|
'id' => $model->getKey(), |
51
|
|
|
'body' => [ |
52
|
1 |
|
'doc' => $model->getDocumentData() |
53
|
|
|
] |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Delete |
59
|
|
|
* |
60
|
|
|
* @param Model|Searchable $model |
61
|
|
|
* @return bool |
62
|
|
|
* |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
*/ |
65
|
1 |
|
public function delete(Model $model): bool |
66
|
|
|
{ |
67
|
1 |
|
$this->isModelSearchable($model); |
68
|
|
|
|
69
|
|
|
$params = [ |
70
|
1 |
|
'index' => $model->getDocumentIndex(), |
71
|
1 |
|
'type' => $model->getDocumentType(), |
72
|
1 |
|
'id' => $model->getKey(), |
73
|
|
|
]; |
74
|
|
|
|
75
|
1 |
|
if (Leopard::existStatement($params)) { |
76
|
1 |
|
Leopard::deleteStatement($params); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Bulk save |
84
|
|
|
* |
85
|
|
|
* @param array|Collection $models |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
2 |
|
public function bulkSave($models) : array |
89
|
|
|
{ |
90
|
2 |
|
$params = []; |
91
|
|
|
|
92
|
2 |
|
$defaultIndex = Leopard::getEsDefaultIndex(); |
93
|
|
|
|
94
|
2 |
|
foreach ($models as $model) { |
95
|
|
|
/** @var Model|Searchable $model */ |
96
|
2 |
|
$this->isModelSearchable($model); |
97
|
|
|
|
98
|
2 |
|
$params['body'][] = [ |
99
|
|
|
'index' => [ |
100
|
2 |
|
'_index' => $model->getDocumentIndex() ?: $defaultIndex, |
101
|
2 |
|
'_type' => $model->getDocumentType(), |
102
|
2 |
|
'_id' => $model->getKey() |
103
|
|
|
] |
104
|
|
|
]; |
105
|
|
|
|
106
|
2 |
|
$params['body'][] = $model->getDocumentData(); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return Leopard::bulkStatement($params); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Bulk delete |
114
|
|
|
* |
115
|
|
|
* @param array $models |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
1 |
|
public function bulkDelete(array $models): array |
119
|
|
|
{ |
120
|
1 |
|
$params = []; |
121
|
|
|
|
122
|
1 |
|
$defaultIndex = Leopard::getEsDefaultIndex(); |
123
|
|
|
|
124
|
1 |
|
foreach ($models as $model) { |
125
|
|
|
/** @var Model|Searchable $model */ |
126
|
1 |
|
$this->isModelSearchable($model); |
127
|
|
|
|
128
|
1 |
|
$params['body'][] = [ |
129
|
|
|
'delete' => [ |
130
|
1 |
|
'_index' => $model->getDocumentIndex() ?: $defaultIndex, |
131
|
1 |
|
'_type' => $model->getDocumentType(), |
132
|
1 |
|
'_id' => $model->getKey() |
133
|
|
|
] |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return Leopard::bulkStatement($params); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|