Removing individual nodes from the ApacheSolr search index

Last updated on
30 April 2025

By default the Apache Solr module has a setting to exclude content types from the search index. If you want more control and exclude certain individual nodes you can use hook_apachesolr_exclude for drupal 7 and hook_apachesolr_node_exclude() for drupal 6.

Example use of hook_apachesolr_exclude() for 7.x-1.1

function hook_apachesolr_exclude($entity_id, $entity_type, $row, $env_id) {
  // Never index media entities to core_1
  if ($entity_type == 'node' && $env_id == 'core_1') {
    return TRUE;
  }
  return FALSE;
}

The above code only prevents nodes from being indexed. To remove nodes already in index, you can use: apachesolr_remove_entity.

Example use of hook_apachesolr_node_exclude() for both 6.x-1.x and 6.x-2.x:

For the following example I first created a cck checkbox named "field_exclude_search" in the story content type to control whether the node should be indexed or not.

/**
 * Implementation of hook_apachesolr_node_exclude()
 */
function mymodule_apachesolr_node_exclude($node) {
  if ($node->type=="story" && $node->field_exclude_search[0]['value']) {
    apachesolr_delete_node_from_index($node);
    return TRUE;
  }
}

Note that hook_apachesolr_node_exclude() only prevents the node from getting indexed, it does not remove an already indexed node from the index. That's why we also call apachesolr_delete_node_from_index().

Help improve this page

Page status: Not set

You can: