Preprocessing in Searchkick

Last updated on
4th
of February,
2016.

According to Searchkick’s documentation, one can control what data is indexed with the search_data method. It’s not apparent, but what actually happens is that if a search_data instance method is indeed present, the return value of it will be what goes into Elasticsearch when indexing.

With this in mind, we can use it to preprocess data before it goes into Elasticsearch. Searchkick’s documentation shows a trivial example:

But we can go further. For example, I have a serialized field in my example Post model called metadata which might take the following form (don’t go hating on the data modelling - sometimes one has no choice when taking on legacy code):