|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Isswp101\Persimmon\Collection\ElasticsearchCollection; |
|
8
|
|
|
use Isswp101\Persimmon\DAL\ElasticsearchDAL; |
|
9
|
|
|
use Isswp101\Persimmon\DAL\IDAL; |
|
10
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
|
11
|
|
|
use Isswp101\Persimmon\QueryBuilder\QueryBuilder; |
|
12
|
|
|
use Isswp101\Persimmon\Relationship\BelongsToRelationship; |
|
13
|
|
|
use Isswp101\Persimmon\Relationship\HasManyRelationship; |
|
14
|
|
|
use Isswp101\Persimmon\Traits\Elasticsearchable; |
|
15
|
|
|
use Isswp101\Persimmon\Traits\Paginationable; |
|
16
|
|
|
use Isswp101\Persimmon\Traits\Relationshipable; |
|
17
|
|
|
|
|
18
|
|
|
class ElasticsearchModel extends Model |
|
19
|
|
|
{ |
|
20
|
|
|
use Elasticsearchable, Paginationable, Relationshipable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ElasticsearchDAL |
|
24
|
|
|
*/ |
|
25
|
|
|
public $_dal; |
|
26
|
|
|
|
|
27
|
16 |
|
public function __construct(IDAL $dal, array $attributes = []) |
|
28
|
|
|
{ |
|
29
|
16 |
|
$this->validateModelEndpoint(); |
|
30
|
|
|
|
|
31
|
14 |
|
parent::__construct($dal, $attributes); |
|
32
|
14 |
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public static function findWithParentId($id, $parent, array $columns = ['*']) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$model = static::find($id, $columns, ['parent' => $parent]); |
|
37
|
|
|
|
|
38
|
|
|
if ($model) { |
|
39
|
|
|
$model->setParentId($parent); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $model; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Execute the query and get the result. |
|
47
|
|
|
* |
|
48
|
|
|
* @param QueryBuilder|array $query |
|
49
|
|
|
* @return ElasticsearchCollection|static[] |
|
50
|
|
|
*/ |
|
51
|
16 |
|
public static function search($query = []) |
|
52
|
|
|
{ |
|
53
|
16 |
|
if ($query instanceof QueryBuilder) { |
|
54
|
8 |
|
$query = $query->build(); |
|
55
|
8 |
|
} |
|
56
|
16 |
|
$model = static::createInstance(); |
|
57
|
|
|
return $model->_dal->search($query); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Execute the query and get the first result. |
|
62
|
|
|
* |
|
63
|
|
|
* @param QueryBuilder|array $query |
|
64
|
|
|
* @return static |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public static function first($query = []) |
|
67
|
|
|
{ |
|
68
|
2 |
|
if ($query instanceof QueryBuilder) { |
|
69
|
|
|
$query = $query->build(); |
|
70
|
|
|
} |
|
71
|
2 |
|
$query['from'] = 0; |
|
72
|
4 |
|
$query['size'] = 1; |
|
73
|
2 |
|
return static::search($query)->first(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Execute the query and get the first result or throw an exception. |
|
78
|
|
|
* |
|
79
|
|
|
* @param QueryBuilder|array $query |
|
80
|
|
|
* @throws ModelNotFoundException |
|
81
|
|
|
* @return static |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function firstOrFail($query = []) |
|
84
|
|
|
{ |
|
85
|
|
|
$model = static::first($query); |
|
86
|
|
|
if (is_null($model)) { |
|
87
|
|
|
throw new ModelNotFoundException(get_called_class()); |
|
88
|
|
|
} |
|
89
|
|
|
return $model; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Apply the callback to the documents of the given query. |
|
94
|
|
|
* |
|
95
|
|
|
* @param QueryBuilder|array $query |
|
96
|
|
|
* @param callable $callback |
|
97
|
|
|
* @param int $limit |
|
98
|
|
|
* @return int hits.total |
|
99
|
|
|
*/ |
|
100
|
4 |
|
public static function map($query = [], callable $callback = null, $limit = -1) |
|
101
|
|
|
{ |
|
102
|
4 |
|
if ($query instanceof QueryBuilder) { |
|
103
|
|
|
$query = $query->build(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
$query['from'] = Arr::get($query, 'from', 0); |
|
107
|
4 |
|
$query['size'] = Arr::get($query, 'size', 50); |
|
108
|
|
|
|
|
109
|
4 |
|
$i = 0; |
|
110
|
4 |
|
$models = static::search($query); |
|
111
|
|
|
$total = $models->getTotal(); |
|
112
|
|
|
while ($models) { |
|
113
|
|
|
foreach ($models as $model) { |
|
114
|
|
|
if ($callback) { |
|
115
|
|
|
$callback($model); |
|
116
|
|
|
} |
|
117
|
|
|
$i++; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$query['from'] += $query['size']; |
|
121
|
|
|
|
|
122
|
|
|
if ($i >= $total || ($limit > 0 && $i >= $limit)) { |
|
123
|
|
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$models = static::search($query); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $total; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Execute the query and get all items. |
|
134
|
|
|
* |
|
135
|
|
|
* @param QueryBuilder|array $query |
|
136
|
|
|
* @return Collection |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public static function all($query = []) |
|
139
|
|
|
{ |
|
140
|
2 |
|
if ($query instanceof QueryBuilder) { |
|
141
|
|
|
$query = $query->build(); |
|
142
|
|
|
} |
|
143
|
2 |
|
$collection = collect(); |
|
144
|
2 |
|
static::map($query, function (ElasticsearchModel $document) use ($collection) { |
|
145
|
|
|
$collection->put($document->getId(), $document); |
|
146
|
2 |
|
}); |
|
147
|
|
|
return $collection; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
protected function belongsTo($class) |
|
151
|
|
|
{ |
|
152
|
2 |
|
return new BelongsToRelationship($this, $class); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
2 |
|
protected function hasMany($class) |
|
156
|
|
|
{ |
|
157
|
2 |
|
return new HasManyRelationship($this, $class); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|