[Drupal] How to prevent particular page from being indexed in apache solr

| | 1 min read

Sometimes, we may come across a situation where we need to exclude a particular page from being indexed. We had a similar requirement in one of our projects where we had to exclude home page from being indexed by apache solr search. Read on to know how we executed it using hooks provide by apachesolr.

Apache solr provides a hook called hook_apachesolr_exclude() which inspects each entity added to the apache solr index. If the hook returns true, then the entity would not get indexed. This hook could be used for excluding home page from being indexed.

Please have a look at the codebase.

function your_module_apachesolr_search_apachesolr_exclude($entity_id, $entity_type, $row, $env_id) {
  $frontpage = end(explode('/', variable_get('site_frontpage', 'node')));
  // Never index page not found node.
  if ($entity_id == $page_not_found) {
    return TRUE;
  }
  return FALSE;
}

You would have to re-index your site for the changes to reflect.

Please let us know if we can be of any further help.