Saving Models
Using Laravel-Elasticsearch integration allows developers to save models in a way that aligns with the typical Eloquent patterns used in Laravel, making it easier to switch from or use alongside traditional relational models.
Save a New Model
Section titled “Save a New Model”Option A: Attribute Assigning
Section titled “Option A: Attribute Assigning”Create a new model instance, assign attributes individually, and then save it to the Elasticsearch index. This method is direct and reflects common Laravel ORM practices.
$log = new UserLog;$log->user_id = $userId;$log->title = $title;$log->status = 1;$log->save();
Demonstrates creating and saving a new model instance by setting each attribute separately.
Option B: Mass Assignment via create()
Section titled “Option B: Mass Assignment via create()”Use the create()
method for mass assigning model attributes through an associative array, streamlining the creation and saving of a model instance.
$log = UserLog::create([ 'user_id' => $userId, 'title' => $title, 'status' => 1,]);
Updating a Model
Section titled “Updating a Model”Update models in Elasticsearch following Eloquent’s methodology: fetch a model, modify its attributes, and then call save()
.
$log = UserLog::where('status', 1)->first();$log->status = 2;$log->save();
Fetch the first User Log with a status of 1, change its status to 2, then save the updated model.
For updates affecting multiple documents, employ the update()
method on a query builder instance, which allows for bulk modifications based on query conditions.
Product::where('status', 1)->update(['status' => 4]);
Inserting Models
Section titled “Inserting Models”Insert models similarly to Eloquent’s approach, where you pass an array of arrays, each representing a document to be created.
UserLog::insert([ ['user_id' => 'John_Doe'], ['user_id' => 'Jane_Doe'],]);
UserLog
index. For performance optimization, utilize withoutRefresh()
to skip waiting for the index to refresh after insertions.
UserLog::withoutRefresh()->insert([ ['user_id' => 'John_Doe'], ['user_id' => 'Jane_Doe'],]);
bulkInsert()
Section titled “bulkInsert()”bulkInsert()
is identical to insert()
but will continue on errors and return an array of the results.
People::bulkInsert([ [ 'id' => '_edo3ZUBnJmuNNwymfhJ', // Will update (if id exists) 'name' => 'Jane Doe', 'status' => 1, ], [ 'name' => 'John Doe', // Will Create 'status' => 2, ], [ 'name' => 'John Dope', 'status' => 3, 'created_at' => 'xxxxxx', // Will fail ],]);
Returns:
{ "hasErrors": true, "total": 3, "took": 0, "success": 2, "created": 1, "modified": 1, "failed": 1, "errors": [ { "id": "Y-dp3ZUBnJmuNNwy7vkF", "type": "document_parsing_exception", "reason": "[1:45] failed to parse field [created_at] of type [date] in document with id 'Y-dp3ZUBnJmuNNwy7vkF'. Preview of field's value: 'xxxxxx'" } ]}
Fast Saves
Section titled “Fast Saves”Elasticsearch provides a near real-time index, meaning there is a brief delay between indexing a document and its availability for search. Optimize write-heavy operations by skipping the wait with withoutRefresh()
.
$log->withoutRefresh()->save();
and
UserLog::withoutRefresh()->create($attributes);
If you need to update attributes after saving then do not use withoutRefresh()
as subsequent updates will not be saved.
$log = new UserLog;$log->user_id = $userId;$log->title = $title;$log->status = 1;
$log->withoutRefresh()->save();
$log->company_id = 'ABC-123';$log->withoutRefresh()->save(); // company_id will not be saved
First Or Create
Section titled “First Or Create”The firstOrCreate()
method either retrieves a model matching the specified attributes or creates a new one if no match exists, with distinct arrays for search attributes and new values.
$book = Book::firstOrCreate( ['title' => $title, 'author' => $author], // search attributes ['description' => $description, 'stock' => 0] // attributes for creation);
Searches for a book with specified title and author. If found, returns it; if not, creates and returns a new book with additional details.
First Or Create Without Refresh
Section titled “First Or Create Without Refresh”Enhance firstOrCreate()
with withoutRefresh()
to forgo index refreshing when speed is prioritized over immediate data retrieval.
$book = Book::withoutRefresh()->firstOrCreate( ['title' => $title,'author' => $author], //$attributes ['description' => $description, 'stock' => 0] //values );
Searches and creates without delay, making the book available in the index with a slight delay.
Update Or Create Without Refresh
Section titled “Update Or Create Without Refresh”Enhance updateOrCreate()
with withoutRefresh()
to forgo index refreshing when speed is prioritized over immediate data retrieval.
$book = Book::withoutRefresh()->updateOrCreate( ['title' => $title,'author' => $author], //$attributes ['description' => $description, 'stock' => 0] //values );
Searches and updates without delay, making the book available in the index with a slight delay.