Updates and Inserts
Inserts
When using elastic bridge, not only can we retrieve documents from our elastic index, we can also index new documents. Elastic bridge makes this super easy. To index a new document, just create an instance of the bridge class, set your attributes and call the save method on the bridge index.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Bridges\Log;
use Illuminate\Http\Request;
class LogController extends Controller
{
/**
* Index a new log.
*/
public function store(Request $request)
{
$log = new Log();
$log->status = $request->status_code;
$log->message = $request->message;
$log->save();
return response()->json([
'message' => "log indexed successfully"
], 201);
}
}TIP
Alternatively you can use the create method to index a new document. save() returns a boolean, while create() returns the created document _id.
<?php
use App\Bridges\Log;
$id = Log::create([
'status' => 500,
'message' => 'this is a log message'
]);
dump($id); // e.g. "9cac02e7-d063-456f-90ad-0b145bb04fde"IMPORTANT
Just as like elastic search, whenever you set the id on a new bridge instance, it will be set as the _id of the document.
Updates
The save method may be used to update documents that already exist in elasticsearch. To Update a document, you should first retrieve it and set the attributes you wish to update and then call the save method.
use App\Bridges\HotelRoom;
$room = HotelRoom::find(1);
$room->price = 50;
$room->save();In addition, you can use the increment or decrement methods to increase or decrease numeric fields via an update script. Both accept an optional counter (default 1). Note that these methods update Elasticsearch, not the in-memory object; refresh or re-fetch the document to see updated values.
use App\Bridges\HotelRoom;
$room = HotelRoom::find(1);
// increase price by 3
$room->increment('price', 3);
// then re-fetch to reflect changes locally
$room = HotelRoom::find(1);
echo $room->price;Bulk Insert & Upsert
To write many documents at once, use bulk() (index) or upsert() (update-or-insert). Each row is an associative array; a row's id becomes the document _id.
use App\Bridges\HotelRoom;
// Bulk index — creates or overwrites by _id
$result = HotelRoom::bulk([
['id' => 1, 'price' => 100, 'advertiser' => 'booking.com'],
// no id → _id is auto-generated
['price' => 200, 'advertiser' => 'expedia'],
]);
// Bulk upsert — updates existing documents or
// inserts them. An id is required on every row.
$result = HotelRoom::upsert([
['id' => 1, 'price' => 150],
['id' => 2, 'price' => 250],
]);Inspecting the result
Both methods return a Lacasera\ElasticBridge\DTO\BulkResult. Search-engine bulk responses report per-item outcomes (and don't include the document body), so the result exposes what succeeded and what failed rather than hydrated models:
// number of documents written successfully
$result->count();
// number of documents attempted
$result->total();
// true if any item failed
$result->hasErrors();
// Collection of successful item metadata (_id, result, status)
$result->successful();
// Collection of failed items (including their error)
$result->failed();
// raw merged items
$result->toArray();Limits and chunking
Large payloads are chunked into multiple bulk requests automatically, and a hard cap guards against oversized calls. Both are configurable in config/elasticbridge.php:
'bulk' => [
// throws BulkLimitExceeded when the input exceeds this
'max' => env('SEARCH_BULK_MAX', 10000),
'chunk_size' => env('SEARCH_BULK_CHUNK_SIZE', 500),
],You can override the chunk size per call:
HotelRoom::bulk($rows, chunkSize: 1000);IMPORTANT
upsert() requires an id on every row. Exceeding bulk.max throws Lacasera\ElasticBridge\Exceptions\BulkLimitExceeded.