Convert an existing view to search api

Last updated on
24 March 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Use case

You have a bunch of views, completed and running, but for performance and/or design reasons, or just because you just discover the magic of Search API and how easy is to use facets with it... well, it would be nice to convert all these views for enjoying the benefits of Search API.

The problem

Views based on Search API use as base table a search_api_index_* table, not the usual node table or other ones supported by Views. That is why the Search views are selected at the views' creation moment, and it can not be changed later.

What to do

If the views to be converted use the row plugins Content (node), Fields or Rendered entity, the conversion may be easier (see further). If not (the selected row plugins were others), then unfortunately the views should be fully recreated manually: create a brand new search_api view and recreate all the elements from your old view by hand.

For views using Content, Fields or Rendered entity row plugins:

  1. Export the view to be converted. Save this code as a backup.
  2. Modify the view, removing all the Filters (contextual or not), all the Sort criteria and all the Relationships. Write down the removed items: you will need to recreate them manually using the indexed versions.
  3. When the view is clean of filters and sort criteria, export it again.
  4. In the export code, the 5th line will contain the
    $view->base_table

    variable. Change it to the search api index to be used in the view (the index must have been created before):

    $view->base_table = 'search_api_index_<name_of_your_index>';
    
  5. Maybe fiddle around with fields (see below).
  6. Go to admin/structure/views/import and import the modified code. Do not forget to select the option Replace an existing view if one exists with the same name. (NOTE: currently there is a bug in Ctools 1.x that makes the Import option not to be shown for administrator users different than uid=1, here is the issue: #870938: Add new permission for controlling imports ).
  7. Add the previously removed Filters, Sort criteria and Relationships, selecting them from the now available indexed versions.

Process will be easy if the views to be converted used the Rendered entity row plugin, since no changes will be required for the row contents.

Field fiddling

For fields there is no general recipe, although these hints should catch most cases:

  • Care that your index contains all needed fields or create them when import errors bug you.
  • Look for lines that look like this:
    $handler->display->display_options['fields']['field_FOO']['table'] = 'TABLE'
  • If TABLE is 'views', don't change.
  • If TABLE is 'node' (or your base table) or 'field_data_FOO', change to 'search_api_index_<name_of_your_index>'

Help improve this page

Page status: No known problems

You can: