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