1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Triadev\Leopard\Business\Helper\IsModelSearchable; |
9
|
|
|
use Triadev\Leopard\Contract\ElasticsearchManagerContract; |
10
|
|
|
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract; |
11
|
|
|
use Triadev\Leopard\Facade\Leopard; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait Searchable |
15
|
|
|
* @package Triadev\Leopard |
16
|
|
|
* |
17
|
|
|
* @property string $documentIndex |
18
|
|
|
* @property string $documentType |
19
|
|
|
* @property bool $syncDocument |
20
|
|
|
* @property array $searchable |
21
|
|
|
* @property array $syncRelationships |
22
|
|
|
* |
23
|
|
|
* @method static ElasticsearchManagerContract search() |
24
|
|
|
* @methdo static ElasticsearchManagerContract suggest() |
25
|
|
|
* @method array buildDocument() |
26
|
|
|
* @method array toArray() |
27
|
|
|
* @method string getTable() |
28
|
|
|
*/ |
29
|
|
|
trait Searchable |
30
|
|
|
{ |
31
|
|
|
use IsModelSearchable; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
public $isDocument = false; |
35
|
|
|
|
36
|
|
|
/** @var float|null */ |
37
|
|
|
public $documentScore; |
38
|
|
|
|
39
|
|
|
/** @var int|null */ |
40
|
|
|
public $documentVersion; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Searchable boot model. |
44
|
|
|
*/ |
45
|
135 |
|
public static function bootSearchable() |
46
|
|
|
{ |
47
|
|
|
static::saved(function (Model $model) { |
48
|
|
|
/** @var Model|Searchable $model */ |
49
|
1 |
|
if ($model->shouldSyncDocument()) { |
50
|
1 |
|
$model->repository()->save($model); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$model->syncRelationships(); |
54
|
135 |
|
}); |
55
|
|
|
|
56
|
|
|
static::deleted(function (Model $model) { |
57
|
|
|
/** @var Model|Searchable $model */ |
58
|
|
|
if ($model->shouldSyncDocument()) { |
59
|
|
|
$model->repository()->delete($model); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$model->syncRelationships(); |
63
|
135 |
|
}); |
64
|
135 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Repository |
68
|
|
|
* |
69
|
|
|
* @return ElasticsearchRepositoryContract |
70
|
|
|
*/ |
71
|
1 |
|
public function repository(): ElasticsearchRepositoryContract |
72
|
|
|
{ |
73
|
1 |
|
return app(ElasticsearchRepositoryContract::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get document index |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
17 |
|
public function getDocumentIndex(): ?string |
82
|
|
|
{ |
83
|
17 |
|
if (property_exists($this, 'documentIndex')) { |
84
|
16 |
|
return $this->documentIndex; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get document type |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
14 |
|
public function getDocumentType(): string |
96
|
|
|
{ |
97
|
14 |
|
if (property_exists($this, 'documentType')) { |
98
|
13 |
|
return (string)$this->documentType; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return (string)$this->getTable(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Should sync document |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
2 |
|
public function shouldSyncDocument(): bool |
110
|
|
|
{ |
111
|
2 |
|
if (property_exists($this, 'syncDocument')) { |
112
|
2 |
|
return (bool)$this->syncDocument; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sync relationships |
120
|
|
|
*/ |
121
|
1 |
|
public function syncRelationships() |
122
|
|
|
{ |
123
|
1 |
|
if (property_exists($this, 'syncRelationships')) { |
124
|
1 |
|
foreach ($this->syncRelationships as $syncRelationshipClass) { |
125
|
|
|
/** @var Model $syncRelationship */ |
126
|
1 |
|
$syncRelationship = new $syncRelationshipClass(); |
127
|
|
|
|
128
|
|
|
/** @var BelongsTo $relation */ |
129
|
1 |
|
$relation = $this->belongsTo( |
|
|
|
|
130
|
1 |
|
$syncRelationshipClass, |
131
|
1 |
|
$syncRelationship->getForeignKey() |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
/** @var Model|Searchable $parent */ |
135
|
1 |
|
$parent = $relation->first(); |
136
|
|
|
|
137
|
1 |
|
if (!$parent || !$this->isModelSearchable($parent)) { |
|
|
|
|
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
$parent->repository()->save($parent); |
142
|
|
|
} |
143
|
|
|
} |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get document data |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
9 |
|
public function getDocumentData() : array |
152
|
|
|
{ |
153
|
9 |
|
if (method_exists($this, 'buildDocument')) { |
154
|
7 |
|
return $this->buildDocument(); |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
if (property_exists($this, 'searchable') && is_array($this->searchable)) { |
158
|
1 |
|
return $this->buildDocumentFromArray($this->searchable); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $this->toArray(); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
private function buildDocumentFromArray(array $searchable) |
165
|
|
|
{ |
166
|
1 |
|
$document = []; |
167
|
|
|
|
168
|
1 |
|
foreach ($searchable as $value) { |
169
|
1 |
|
$result = $this->$value; |
170
|
|
|
|
171
|
1 |
|
if ($result instanceof Collection) { |
172
|
|
|
$result = $result->toArray(); |
173
|
1 |
|
} elseif ($result instanceof Carbon) { |
174
|
|
|
$result = $result->toDateTimeString(); |
175
|
|
|
} else { |
176
|
1 |
|
$result = $this->$value; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
$document[$value] = $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return $document; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $name |
187
|
|
|
* @param $arguments |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
7 |
|
public function __call($name, $arguments) |
191
|
|
|
{ |
192
|
7 |
|
if ($name == 'search') { |
193
|
|
|
return Leopard::search()->model($this); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
7 |
|
if ($name == 'suggest') { |
197
|
|
|
return Leopard::suggest(); |
198
|
|
|
} |
199
|
|
|
|
200
|
7 |
|
return parent::__call($name, $arguments); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|