| Conditions | 5 |
| Paths | 10 |
| Total Lines | 29 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | public static function index(Client $client, array $models, int $chunk = 1000): void |
||
| 18 | { |
||
| 19 | $i = 1; |
||
| 20 | $params = ['body' => []]; |
||
| 21 | foreach ($models as $model) { |
||
| 22 | try { |
||
| 23 | $id = $model->getPrimaryKey(); |
||
| 24 | } catch (Exception $e) { |
||
| 25 | $id = $i++; |
||
| 26 | } |
||
| 27 | $collection = new ElasticsearchCollectionParser($model::getCollection()); |
||
| 28 | $params['body'][] = [ |
||
| 29 | 'index' => [ |
||
| 30 | '_index' => $collection->getIndex(), |
||
| 31 | '_type' => $collection->getType(), |
||
| 32 | '_id' => $id |
||
| 33 | ] |
||
| 34 | ]; |
||
| 35 | $params['body'][] = $model->toArray(); |
||
| 36 | if ($i % $chunk == 0) { |
||
| 37 | $responses = $client->bulk($params); |
||
| 38 | $params = ['body' => []]; |
||
| 39 | unset($responses); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | if (!empty($params['body'])) { |
||
| 43 | $client->bulk($params); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |