function tripal_search_update_default_index

2.x tripal_core.search.inc tripal_search_update_default_index()
3.x tripal_core.search.inc tripal_search_update_default_index()

The Search API provides a default node index which has a number of node-specific fields enabled by default. We want to ensure our chado fields are also enabled by default thus making for easier enabling of Tripal search.

This function should be called whenever new nodes might have been added to ensure that their fields are added as well.

We should only modify the default node index if it has no database service yet. That way we ensure we don't override user changes!

1 call to tripal_search_update_default_index()

File

tripal_core/includes/tripal_core.search.inc, line 361
Adds support for Drupal indexing of Chado. It's important to note that not all of Chado is indexed but instead Only fields indicated in hook_search_include_chado_fields().

Code

function tripal_search_update_default_index() {

  // First we need the index object for the "Default node index".
  $index_id = db_query('SELECT id FROM search_api_index WHERE machine_name=:name', 
  array(':name' => 'default_node_index'))->fetchField();
  if (!$index_id) {
    // ERROR
    return FALSE;
  }
  $index = search_api_index_load($index_id);

  // Collect all the fields already added to the search index.
  $changes = array('options' => $index->options);

  // Now we only want to update the index if it's both enabled and has no server indicated.
  // That way we can be reasonably sure that it was been untouched by admin users.
  if ($index->enabled == FALSE AND $index->server == NULL) {

    // We need information about all the fields available to nodes before we can
    // go crazy enabling them... That information is stored as properties of nodes
    // so we'll grab that.
    $info = entity_get_property_info('node');

    // Now we want to loop through each node type and add all the properties for the
    // chado node types.
    // Assumption #1: We are assuming that all chado node types are prefixed 'chado_'.
    foreach ($info['bundles'] as $node_type => $details) {
      if (preg_match('/^chado_/', $node_type)) {

        // Now add each chado fields to the index but only if they are not already added.
        foreach ($details['properties'] as $field_name => $field_details) {
          if (!isset($changes['options']['fields'][$field_name])) {
            $changes['options']['fields'][$field_name]['type'] = ($field_details['type'] == 'varchar') ? 'text' : $field_details['type'];

            // Furthermore if this is a name then we want to add a boost to ensure it carries
            // more weight in the search results.
            if (preg_match('/name/', $field_name)) {
              $changes['options']['fields'][$field_name]['boost'] = '3.0';
            }
          }
        }

      }
    }

    // We also want to enable highlighting to ensure an excerpt is generated since this
    // will be used in the default search view distributed with Tripal.
    if (!isset($index->options['processors']['search_api_highlighting'])) {
      $changes['options']['processors']['search_api_highlighting'] = array(
        'status' => 1,
        'weight' => 35,
        'settings' => array(
          'prefix' => '<strong>',
          'suffix' => '</strong>',
          'excerpt' => 1,
          'excerpt_length' => 256,
          'exclude_fields' => array(),
          'highlight' => 'always',
        ),
      );
    }
    else {
      $changes['options']['processors']['search_api_highlighting']['status'] = 1;
      $changes['options']['processors']['search_api_highlighting']['settings']['excerpt'] = 1;
    }

    // Finally we save all of our changes :-).
    search_api_index_edit($index_id, $changes);
    drupal_set_message('The Search API "Default Node Index" was updated.');
  }
  else {
    tripal_report_error(
    'tripal_search', 
    TRIPAL_NOTICE, 
    'The Search API "Default Node Index" was not updated with Tripal Fields. If you would like to enable more Tripal/Chado fields to be indexed, edit the Field Listing for the "Default Node Index" now.'
    );
  }
}