[6.x] Improve search indexing performance #13126
Open
+369
−81
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request aims to improve the performance of updating search indexes in Statamic.
TL;DR We're now passing around references, chunking documents and inserting them via the queue.
The Issue
On larger sites, with lots of entries, clicking the "Update" button in the search utility or running the
search:updatecommand would cause page timeouts and significant memory issues.Even though we're returning
LazyCollectioninstances from our searchables, some collection methods, like->each()evaluate the underlying array, even when the closure is empty. We believe this was happening due the collection containing lots ofEntryobjects.Changes
entry::the-entries-id), instead of collections ofEntryobjects.query_scopeon search indexes, which is much more performant than thefilteroption.insertDocumentsmethod.queue,queue_connectionandchunk_sizecan be configured in thesearch.phpconfig file.As a result of these changes, I was able to re-index a site with 100k entries in ~25 seconds (using the Rad Pack Typesense driver).
Breaking Changes
Custom Searchables
Searchables should now return collections of references (eg.
entry::the-entry-id) instead of objects.If your searchable allows for it, you may want to consider adding support for query scopes, which we now recommend over filtering.
If you support filtering, you may want to split the "with filter" & "without filter" cases into separate return statements.
The
->filter()method evaluates every item in the collection, which is an unnecessary performance hit when no filter is configured:Example PR: statamic-rad-pack/runway#748
Custom Search Drivers
The
insertDocumentmethod is now public:If you were previously overriding the
insertMultiplemethod to chunk documents, you don't need to do that anymore (chunking is now handled by the base method).If you need to manipulate the fields array before it gets sent to your index, you may define a
fieldsmethod:Example PRs:
Fixes #13122