0

I updated the search index display for my content type (eg, from admin/structure/types/manage/page/display/search_index), but the old index is still being used.

How can I fix this?

0

When you save the settings for the search index display, nodes are not marked as needing to be reindexed. You can either reindex the full site, or add in your own submit handler to mark them as dirty:

/** 
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_field_ui_display_overview_form_alter(&$form, $form_state) {
  if ($form['#view_mode'] == 'search_index' && module_exists('search')) {
    $form["#submit"][] = 'MYMODULE_mark_search_index_dirty';
  }
}

function MYMODULE_mark_search_index_dirty($form, &$form_state) {
  $entity_type = $form["#entity_type"];

  $query = new EntityFieldQuery();

  $query->entityCondition('entity_type', $entity_type, '=');

  if (!empty($form["#bundle"])) {
    $query->entityCondition('bundle', $form["#bundle"]);
  }

  $entities = $query->execute();

  $sids = array_keys($entities[$entity_type]);

  $num = db_update('search_dataset')
    ->fields(array('reindex' => REQUEST_TIME))
    ->condition('type', $entity_type)
    ->condition('sid', $sids, 'IN')
    ->execute();

  if ($num > 0) {  
    drupal_set_message(format_plural($num,
      '1 item needs to be indexed.',
      '@count items needs to be indexed.'));
  }
}

This will alter the form used to edit the display settings. It checks to see if you are on the tab for changing the display of the search index view mode, and also have the Search module enabled. If so, it adds on a new submit handler.

The submit hanlder does an EntityFieldQuery to get all of the entity IDs for the type/bundle pair specified by the $form. It used EFQ instead of db_query() to be entity-type agnostic.

Then, based off of the code for search_touch_node() (which is called from node-related insert and update update hooks), it updates the search index to mark those entities as needing to be reindexed. This will leave the old indexed data in place, and subsequent cron runs will reindex.

If you want to remove those from the index until reindexing, then you can replace the query with a loop like:

foreach ($sids as $sid) {
  search_reindex($sid, $entity_type);
}

$num = count($sids);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.