1 | <?php |
||
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 = []) |
|
33 | |||
34 | 4 | public static function findWithParentId($id, $parent, array $columns = ['*']) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
||
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 | 4 | 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 | 4 | $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 | 2 | $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|static[] |
||
131 | */ |
||
132 | 2 | public static function all($query = []) |
|
143 | |||
144 | 6 | protected function belongsTo($class) |
|
148 | |||
149 | 4 | protected function hasMany($class) |
|
153 | } |
||
154 |