Expanding indexed content with Search API Data Alterations

The Search API module for Drupal 7 offers a lot of cool features for configuring and creating search pages. For developers of complex applications, it also offers some great functionality through its data alteration capability that can be easy to overlook. The search_api_attachments module is a good example of how to use data alterations to expand the data available for indexing.

Chris Johnson, VP of Engineering
#Drupal | Posted

The Search API module for Drupal 7 offers a lot of cool features for configuring and creating search pages. For developers of complex applications, it also offers some great functionality through its data alteration capability that can be easy to overlook. The search_api_attachments module is a good example of how to use data alterations to expand the data available for indexing.

The key hook is hook_search_api_alter_callback_info() and it is documented in search_api.api.php. This hook lets you declare a data alteration that site administrators can enable when configuring an index. After installing a module that offers data alterations they will show on the workflow section of the index configuration where they will need to be enabled. Once enabled, each data item the alteration provides will show on the fields section of the index configuration where the desired items can be selected for inclusion in the index. In the case of search_api_attachments each file field for the entity being indexed will have an option for being included in the index.

Writing your own data alteration is relatively straightforward. Simply create a class implementing the SearchApiAlterCallbackInterface and use the search_api_alter_callback_info hook to tell search_api about it. The interface is defined in the includes/callback.inc file of the search_api module and each required method is well commented there.

If you haven't had a chance to check out the search_api module give it a shot. There is a good walkthrough in the Search API section of the Site Building Guide and some pointers to other techniques for adding data for searching on the Search API project page

Chris Johnson

VP of Engineering