-
-
Notifications
You must be signed in to change notification settings - Fork 540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow transforming of search index values #2462
Conversation
Thanks for the PR. In this example, is your If so, I'd prefer to have the transformer on the fieldtype level, so we could implement this: statamic/ideas#321 |
Ah, yeah it is. But I'd still prefer to have some control. For example in a Bard field, I'd want to strip everything not text related to the search index. That would be hard to do in "userland" since you can't just edit the Bard fieldtype class |
@jasonvarga Is this implementation something you want to continue with? Then I'm willing to add tests as well, otherwise we can close this |
I think I initially misunderstood this PR. This does look like a good solution! The only thing a little unclear is that you need to return an array of key/value pairs. If you wanted to perform just a simple transform, I would have initially just thought you could return the value. e.g. This: 'transformers' => [
'title' => function ($title) {
return strtoupper($title);
}
] instead of this: 'transformers' => [
'title' => function ($title) {
return ['title' => strtoupper($title)];
}
] I wonder if there's a solution that's the best of both worlds? |
I suppose we could check if the return value is an array, treat it how you are doing it right now, otherwise assume it's a single value. If you want the returned value to be an array, you'd need to wrap it. 'transformers' => [
'array_field' => function ($arr) {
return ['array_field' => $arr];
}
] |
@jasonvarga Added support for both cases + tests! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you.
This PR allows you to add a
transformers
key to your search index configuration:For example, when you have a location field that saves all raw data in the entry:
This would result in a document like this:
Which is important for Algolia, because the
_geoloc
data has to be top-level.