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 = ['*']) |
|
44 | |||
45 | /** |
||
46 | * Execute the query and get the result. |
||
47 | * |
||
48 | * @param QueryBuilder|array $query |
||
49 | * @return ElasticsearchCollection|static[] |
||
50 | */ |
||
51 | 22 | public static function search($query = []) |
|
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 = []) |
|
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 = []) |
||
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 | 2 | $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 | 4 | $total = $models->getTotal(); |
|
112 | 4 | while ($models) { |
|
113 | 4 | foreach ($models as $model) { |
|
114 | 4 | if ($callback) { |
|
115 | 4 | $callback($model); |
|
116 | 4 | } |
|
117 | 4 | $i++; |
|
118 | 4 | } |
|
119 | |||
120 | 4 | $query['from'] += $query['size']; |
|
121 | |||
122 | 4 | if ($i >= $total || ($limit > 0 && $i >= $limit)) { |
|
123 | 4 | break; |
|
124 | } |
||
125 | |||
126 | $models = static::search($query); |
||
127 | } |
||
128 | |||
129 | 4 | 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 = []) |
|
149 | |||
150 | 6 | protected function belongsTo($class) |
|
154 | |||
155 | 4 | protected function hasMany($class) |
|
159 | } |
||
160 |