I want to remove unused nodes from indexing in solr and also I need to make sure that they are not indexed again. I will fetch the list of the unused node IDs using a query.

If any of the nodes is getting updated, then I need to index that node again.

How can I achieve this?

3 Answers 3


For d7 you are looking for the hook: hook_apachesolr_exclude

Example from the apachesolr.api.php

 * This is the place to look for the replacement to hook_apachesolr_node_exclude
 * You should define a replacement for the status callback and return
 * FALSE for entities which you do not want to appear in the index and TRUE for
 * those that you want to include

 * This is invoked for each entity that is being inspected to be added to the
 * index. if any module returns TRUE, the entity is skipped for indexing.
 * @param integer $entity_id
 * @param string $entity_type
 * @param integer $row
 *   A complete set of data from the indexing table.
 * @param string $env_id
 * @return boolean
function hook_apachesolr_exclude($entity_id, $entity_type, $row, $env_id) {
  // Never index media entities to core_1
  if ($entity_type == 'media' && $env_id == 'core_1') {
    return TRUE;
  return FALSE;

See here: http://drupalcontrib.org/api/drupal/contributions%21apachesolr%21apachesolr.api.php/7


For deleting certain nodes (entities) you can use apachesolr_remove_entity.

  • Thanks for providing a Drupal 7 version. However I just want to point out again, as I said in my post, if nodes are supposed to be added and excluded from the queue as they are updated, this function alone will not suffice, since it does not remove already existing nodes from the index and that seems like it would be a constant need in Mathankumar's scenario. I think the explanation in my answer should make sense... Commented Oct 26, 2012 at 23:55
  • Hi Boriana, thanks for noticing this. I edited my answer to inculde this.
    – Mike
    Commented Oct 28, 2012 at 6:56
  • That's not a problem. Would have been nice if you had given me a chance to update my own answer by leaving the Drupal 7 function modification in a comment, so we didn't end up having almost identical answers, but oh well! I may delete my original answer to avoid confusion... Commented Oct 29, 2012 at 13:24
  • I would certainly do what you are saying if I were to make a suggestion or an improvement to an otherwise correct answer.
    – Mike
    Commented Oct 29, 2012 at 14:40

If you're comfortable writing your own modules, there's a hook that can help you exclude certain nodes from the index: hook_apachesolr_node_exclude($node, $namespace).

This is invoked by the apachesolr module for each node that qualifies to be added to the index. If the function returns TRUE, the node is skipped for indexing. However, nodes which are already index will not just be removed because of the TRUE return value. That only prevents nodes from being added. You'll have to clean up your index by calling apachesolr_delete_node_from_index().

See the here for additional information: http://api.drupalhelp.net/api/apachesolr/apachesolr.api.php/function/hook_apachesolr_node_exclude/7

Hope this is enough to get you started!

There were some modules in Drupal 6 to help with this, such as http://drupal.org/project/apachesolr_exclude, but I don't know of any good ones for Drupal 7.

  • Your api reference is outdated. hook_apachesolr_node_exclude does not exist in the lates d7 version module
    – Mike
    Commented Oct 26, 2012 at 20:28

In Solr configuration at "admin/settings/apachesolr/content-bias" you can set the content types to exclude from the search index.


  • 1
    in 7 it's set in admin/config/search/apachesolr/settings/solr/index
    – uwe
    Commented Oct 25, 2012 at 15:40
  • 2
    It looks like he's trying to avoid indexing specific nodes that he can pull up via a query rather than excluding entire content types.... Commented Oct 25, 2012 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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