<p>Is it possible for us to manually inject our own <code>keywords</code> into the <code>searchindex</code> via an event or something? eg.</p>
<pre class="lang-php prettyprint-override"><code>Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (ModelEvent $event) {
    $event-&gt;sender-&gt;addKeywords('blah blah blah');
});
</code></pre>
<p>We want to be able to automatically append extra keywords to some posts to increase the searchability, e.g. The Author name, API results etc.</p>
<p>Thanks</p>

CC BY-SA 4.0
2020-10-09T15:26:56.110Z
We add a plain text field and add comma separated values to that, which is probably the most suitable method.
Joshua M
CC BY-SA 4.0
2020-10-13T11:45:40.163Z
This would be visible to the user though? and wouldn't automatically populate with internal values?
owenmelbz
CC BY-SA 4.0
2020-10-15T14:11:23.590Z
Visible to the client user, or visible to the customer on the front end? It wont be visible on the front end unless you output it. You can populate it with internal values using the AFTER_SAVE function and `setFieldValues()`.
Joshua M
CC BY-SA 4.0
2020-10-15T17:02:04.453Z
We don't want anybody to be able to see it, it should purely exist in code-land really
owenmelbz
CC BY-SA 4.0
2020-10-20T12:45:10.373Z
Also this would create an infinite loop. When i save the element, it will trigger AFTER_SAVE, which will run my code etc etc
owenmelbz
CC BY-SA 4.0
2020-10-20T14:35:45.313Z
True–you should do it on `BEFORE_SAVE`. Not sure regarding hiding the field–if we need to do something like this, we generally expose it anyway because in our experience the user sometimes wants to add their own keywords beyond the defaults.
Joshua M
CC BY-SA 4.0
2020-10-20T15:40:20.310Z
If we try use before_save it seems to do nothing, when we modify the values it doesn't persist them.
owenmelbz
CC BY-SA 4.0
2020-10-21T09:31:15.447Z
That's probably for a different question :)
Joshua M
CC BY-SA 4.0
2020-10-21T09:48:41.133Z
I don't feel it is xD The question was "how to add keywords" and if the answer is "add a custom input and populate it" it doesn't explain the "how" just "what to do" - e.g. "What must we do to end world hunger?- Get food and money to everybody" - How though... How can you do that xD
owenmelbz
CC BY-SA 4.0
2020-10-21T09:58:28.700Z
Right.. but your question is 'How do I add keywords' of which the 'how' is create a custom field and use an event to push data to it. Having problems with using `BEFORE_SAVE` is a question of 'How do I push data to a custom field using `BEFORE_SAVE`'
Joshua M
CC BY-SA 4.0
2020-10-21T13:24:53.807Z
I think thats the "what" xD What you need to do is... create a custom field.. but How :P
owenmelbz
<search>
Adding custom "keywords" to the search index
# Adding custom "keywords" to the search index

(Asked by: username_0 on 2020-10-09)

Is it possible for us to manually inject our own keywords into the searchindex via an event or something? eg.
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (ModelEvent $event) {
    $event->sender->addKeywords('blah blah blah');
});

We want to be able to automatically append extra keywords to some posts to increase the searchability, e.g. The Author name, API results etc.
Thanks

**Tags:** search
> **Comments:**
> **username_1**: We add a plain text field and add comma separated values to that, which is probably the most suitable method.  
> **username_0**: This would be visible to the user though? and wouldn't automatically populate with internal values?  
> **username_1**: Visible to the client user, or visible to the customer on the front end? It wont be visible on the front end unless you output it. You can populate it with internal values using the AFTER_SAVE function and `setFieldValues()`.  
> **username_0**: We don't want anybody to be able to see it, it should purely exist in code-land really  
> **username_0**: Also this would create an infinite loop. When i save the element, it will trigger AFTER_SAVE, which will run my code etc etc  
> **username_1**: True–you should do it on `BEFORE_SAVE`. Not sure regarding hiding the field–if we need to do something like this, we generally expose it anyway because in our experience the user sometimes wants to add their own keywords beyond the defaults.  
> **username_0**: If we try use before_save it seems to do nothing, when we modify the values it doesn't persist them.  
> **username_1**: That's probably for a different question :)  
> **username_0**: I don't feel it is xD The question was "how to add keywords" and if the answer is "add a custom input and populate it" it doesn't explain the "how" just "what to do" - e.g. "What must we do to end world hunger?- Get food and money to everybody" - How though... How can you do that xD  
> **username_1**: Right.. but your question is 'How do I add keywords' of which the 'how' is create a custom field and use an event to push data to it. Having problems with using `BEFORE_SAVE` is a question of 'How do I push data to a custom field using `BEFORE_SAVE`'  
> **username_0**: I think thats the "what" xD What you need to do is... create a custom field.. but How :P  

---

## 0 answers

