<p><strong>Context</strong></p>

<p>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.</p>

<p>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).</p>

<p>I am currently working on a Craft 3 site using PostgreSQL and fear the same kind of issues.</p>

<p><strong>Solution</strong></p>

<p>But since Craft 3, and following <a href="https://craftcms.stackexchange.com/a/13495/131">this post</a>, there is now a <code>RegisterElementSearchableAttributesEvent</code> event that a plugin can listen to modify what gets saved to the search index table when an element is saved.</p>

<p><strong>Problem</strong></p>

<p>I tried to use this event in my <code>CustomModule.php</code> file using this simple test, in order to exclude every fields of the search index:</p>

<pre><code>Event::on(
    Element::class,
    Element::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES,
    function (RegisterElementSearchableAttributesEvent $event) {
        $event-&gt;attributes = [];
    }
);
</code></pre>

<p>But unfortunately, this change nothing: on save, all the same fields are still saved in the <code>searchindex</code> table.</p>

<p><strong>Question</strong></p>

<p>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)?</p>

<p><em>Disclaimer: I'm a new to Module development</em></p>

CC BY-SA 4.0
2018-10-17T17:19:52.320Z
Are you sure you can disable the behavior? Because I don't see a way to prevent indexing the fields to be honest. It goes straight through without any events. Your event is for attributes related to the element, but your question is about the field layout and that's stored no matter what
Robin Schambach
CC BY-SA 4.0
2018-10-18T07:22:11.033Z
Edit: Just for reference -> why don't you just uncheck the "Use this field’s values as search keywords?" Checkbox instead of trying to code something?
Robin Schambach
CC BY-SA 4.0
2018-10-18T08:04:37.357Z
I'm not sure I can disabled the behavior, but according to this [Brad's answer](https://craftcms.stackexchange.com/a/13495/131) this is what I guess about excluding some fields. What checkbox are you talking about? I don't see it when configuring / editing my fields.
Romain P.
CC BY-SA 4.0
2018-10-18T13:03:48.853Z
Ah sorry this is a new feature and does not exist yet in the current version of Craft. It will appear in the next major release
Robin Schambach
CC BY-SA 4.0
2018-10-18T13:07:59.937Z
Oh, good to hear! How can I get this beta version to see if it could solve my problem? And do you have any idea when this feature will be publicly released?
Romain P.
CC BY-SA 4.0
2018-10-18T13:22:44.763Z
https://craftcms.com/guides/craft-3-1-dev-preview-notes you can test if it works for you.. It will be released next year january
Robin Schambach
CC BY-SA 4.0
2018-10-20T02:32:17.677Z
@RomainPoirier Where are you adding it in your module and are you sure your module is being bootstrapped and executing?  I just tried your code from a plugin's init() method and it works as you'd expect.
Brad Bell
<plugin-development><search><events><modules>
How to exclude some fields from the searchindex using the RegisterElementSearchableAttributesEvent event?
# How to exclude some fields from the searchindex using the RegisterElementSearchableAttributesEvent event?

(Asked by: username_0 on 2018-10-17)

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

**Tags:** plugin-development, search, events, modules

> **Comments:**
> **username_1**: Are you sure you can disable the behavior? Because I don't see a way to prevent indexing the fields to be honest. It goes straight through without any events. Your event is for attributes related to the element, but your question is about the field layout and that's stored no matter what  
> **username_1**: Edit: Just for reference -> why don't you just uncheck the "Use this field’s values as search keywords?" Checkbox instead of trying to code something?  
> **username_0**: I'm not sure I can disabled the behavior, but according to this [Brad's answer](https://craftcms.stackexchange.com/a/13495/131) this is what I guess about excluding some fields. What checkbox are you talking about? I don't see it when configuring / editing my fields.  
> **username_1**: Ah sorry this is a new feature and does not exist yet in the current version of Craft. It will appear in the next major release  
> **username_0**: Oh, good to hear! How can I get this beta version to see if it could solve my problem? And do you have any idea when this feature will be publicly released?  
> **username_1**: https://craftcms.com/guides/craft-3-1-dev-preview-notes you can test if it works for you.. It will be released next year january  
> **username_2**: @RomainPoirier Where are you adding it in your module and are you sure your module is being bootstrapped and executing?  I just tried your code from a plugin's init() method and it works as you'd expect.  

---

## 0 answers

