Context
I had big issues on a Craft 2 multilingual site with dozens of thousands of entries that were using Matrix fields. The site had become extremely slow (almost unusable), especially when recording new data.
This was certainly caused by the searchindex table which handled very massive records (that weren't particularly necessary for the search features of this project).
I am currently working on a Craft 3 site using PostgreSQL and fear the same kind of issues.
Solution
But since Craft 3, and following this post, there is now a RegisterElementSearchableAttributesEvent event that a plugin can listen to modify what gets saved to the search index table when an element is saved.
Problem
I tried to use this event in my CustomModule.php file using this simple test, in order to exclude every fields of the search index:
Event::on(
Element::class,
Element::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES,
function (RegisterElementSearchableAttributesEvent $event) {
$event->attributes = [];
}
);
But unfortunately, this change nothing: on save, all the same fields are still saved in the searchindex table.
Question
For a given Section, how can I exclude certains fields (specifically Matrix fields) from the search index on save (both from a CP or a front end entry save)?
Disclaimer: I'm a new to Module development