Consider the following index in config/statamic/search.php
:
'videos' => [ 'driver' => 'algolia', 'searchables' => 'collection:videos', 'fields' => ['title', 'artwork', 'tags', 'description', 'id', 'author'],],
Here we have 6 fields that we wish to be in our Algolia index. In this instance author
is a related entry in another collection. If we were to update the index currently, all we'd see is the entry id from the related collection which isn't very helpful to our Algolia search results.
Enter transformers
- "robots in disguise", I know you just sang that in your head!
If we update our index to include transformers:
'videos' => [ 'driver' => 'algolia', 'searchables' => 'collection:videos', 'fields' => ['title', 'artwork', 'tags', 'description', 'id', 'author'], 'transformers' => [ 'author' => function ($author) { $entry = Entry::find($author); return [ 'author_name' => "{$entry->author_first_name} {$entry->title}", ]; } ]],
We pass the entry id into a function, lookup the entry by its id, we can then push the author name - where our blueprint has author_first_name
as a field of course.
Happy transforming.
Contributed by Steven Grant