Skip to content

Commit 1d94af2

Browse files
committed
ChunkById sanitation and fixed bridge bugs
1 parent a7f2fa8 commit 1d94af2

File tree

3 files changed

+138
-68
lines changed

3 files changed

+138
-68
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,44 @@ Schema::dsl('open',['index' => 'my_index'])
12811281
Behind the scenes it uses the official elasticsearch PHP client, it will call `$client->indices()->{$method}($params);`
12821282

12831283

1284+
Chunking
1285+
--------
1286+
1287+
If you need to run a query that will return a large number of results, you can use the `chunk()` method to retrieve the
1288+
results in chunks. You can chunk as you normally do in Laravel:
1289+
1290+
```php
1291+
Product::chunk(1000, function ($products) use (&$prodIds) {
1292+
foreach ($products as $product) {
1293+
//Increase price by 10%
1294+
$currentPrice = $product->price;
1295+
$product->price = $currentPrice * 1.1;
1296+
$product->save();
1297+
}
1298+
});
1299+
```
1300+
1301+
**Note**: Elasticsearch's default settings will fail when you try to chunk with the following
1302+
error: `Fielddata access on the _id field is disallowed`
1303+
1304+
To counter this you have two options:
1305+
1306+
1. Update Elasticsearch's settings to allow fielddata access on the _id field: Set the value
1307+
of `indices.id_field_data.enable` to `true` in elasticsearch.yml
1308+
2. Use the `chunkById()` method instead of `chunk()` - This will chunk on a field other than `_id`, but you would need a
1309+
field that's unique for each record.
1310+
1311+
```php
1312+
Product::chunkById(1000, function ($products) use (&$prodIds) {
1313+
foreach ($products as $product) {
1314+
//Increase price by 10%
1315+
$currentPrice = $product->price;
1316+
$product->price = $currentPrice * 1.1;
1317+
$product->save();
1318+
}
1319+
}, 'product_sku.keyword');
1320+
```
1321+
12841322
Queues
12851323
----------
12861324
_[Coming]_

src/DSL/Bridge.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ public function processDeleteAll($wheres, $options = []): Results
301301
'id' => $wheres['_id'],
302302
];
303303
try {
304-
$response = $this->client->delete($params);
304+
$responseObject = $this->client->delete($params);
305+
$response = $responseObject->asArray();
305306
$response['deleteCount'] = $response['result'] === 'deleted' ? 1 : 0;
306307

307308
return $this->_return($response['deleteCount'], $response, $params, $this->_queryTag(__FUNCTION__));
@@ -311,13 +312,14 @@ public function processDeleteAll($wheres, $options = []): Results
311312
}
312313
try {
313314
$params = $this->buildParams($this->index, $wheres, $options);
314-
$response = $this->client->deleteByQuery($params);
315+
$responseObject = $this->client->deleteByQuery($params);
316+
$response = $responseObject->asArray();
315317
$response['deleteCount'] = $response['deleted'] ?? 0;
316318

317319
return $this->_return($response['deleteCount'], $response, $params, $this->_queryTag(__FUNCTION__));
318320
} catch (Exception $e) {
319-
$result = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
320-
throw new Exception($result->errorMessage);
321+
$error = $this->_returnError($e->getMessage(), $e->getCode(), [], $this->_queryTag(__FUNCTION__));
322+
throw new Exception($error->errorMessage);
321323
}
322324

323325
}
@@ -366,11 +368,11 @@ public function processIndexMappings($index): mixed
366368
{
367369
$params = ['index' => $index];
368370
try {
369-
$response = $this->client->indices()->getMapping($params);
371+
$responseObject = $this->client->indices()->getMapping($params);
372+
$response = $responseObject->asArray();
370373
$result = $this->_return($response, $response, $params, $this->_queryTag(__FUNCTION__));
371374

372-
373-
return $result->data->asArray();
375+
return $result->data;
374376
} catch (Exception $e) {
375377
$result = $this->_returnError($e->getMessage(), $e->getCode(), $params, $this->_queryTag(__FUNCTION__));
376378
throw new Exception($result->errorMessage);

0 commit comments

Comments
 (0)