I’m trying to optimise my application by only updating my Algolia records (and therefore saving on thousands of unnecessary operations) when data that I care about changes.
I have a table that holds price information about various products that is updated every minute when the price changes. Currently, the searchable model in Algolia is kept in sync with this as intended but this is unnecessary as the search function on my site only provides search for the products themselves, not the prices.
Currently, Scout performs an operation in Algolia every time the update()
method is called on the model (once every minute on each model).
Is there a way that I could conditionally trigger Algolia to update only when the manufacturer, product, or variant of the model changes and not the price?
The toSearchableArray()
function on my model currently looks like this, if that helps at all:
public function toSearchableArray() { return [ 'id' => $this->id, 'manufacturer' => $this->manufacturer, 'product' => $this->product, 'variant' => $this->variant, ]; }
Advertisement
Answer
Is the withoutSyncingToSearch
method what you are looking for?
This would mean wrapping wherever you currently update your prices in this method.
Whilst this code is not provided in your question, I assume something along the lines of:
AppModel::withoutSyncingToSearch ( function() use ($item, $newPrice) { $item->price = $newPrice; $item->save(); });
could work for you?