1

I would like to exclude some nodes from being indexed based on one field value (not content-type) on a drupal 7 installation, I can't find a hook letting me doing it, please help ! thank you

3

As said in the API, using hook_search_api_index_items_alter is not the best choice. You should use hook_search_api_alter_callback_info

Example :

function mymodule_search_api_alter_callback_info() {
  $callbacks['exclude_some_nodes'] = array(
    'name' => t('Exclude some nodes'), 
    'description' => t('Exclude some nodes from being indexed based on one field value'),
    'class' => 'SearchApiExcludeSomeNodes',
  );
  return $callbacks;
}

class SearchApiExcludeSomeNodes extends SearchApiAbstractAlterCallback {

  public function supportsIndex(SearchApiIndex $index) {
    return $index->getEntityType() === 'node';
  }

  public function alterItems(array &$items) {
    foreach ($items as $k => $item) {
      if($item->field_your_field['und'][0]['value'] == 'do_not_index') {
        unset($items[$k]);
      }
    }
  }

}

This will appear as an option in the UI of Search API, in the Data alterations section (admin/config/search/search_api/index/your_index/workflow)

|improve this answer|||||
  • This does not work. – asiby Nov 27 '17 at 20:21
1

Use hook_apachesolr_node_exclude(). An example implementation is given in the API doc:

/**
 * This is invoked by apachesolr.module for each node to be added to the index.
 * If any module returns TRUE, the node is skipped for indexing. Note that nodes
 * which are already present in the index and subsequently qualify to be
 * excluded will not be removed from the index automatically. This hook can be
 * used to remove them prior to returning TRUE.
 *
 * @param object $node
 *   The node object which is being indexed.
 * @param string $namespace
 *   Usually the calling module (eg. 'apachesolr_search').
 *
 * @return bool
 *   Return TRUE to skip the indexing of the node.
 */
function hook_apachesolr_node_exclude($node, $namespace) {
  // Exclude nodes from uid 1.
  if ($node->uid == 1) {
    apachesolr_delete_node_from_index($node);
    return TRUE;
  }
}
|improve this answer|||||
  • Thanks, I will try this hook, I have found this hook as well : hook_search_api_index_items_alter() which does the job, but I have noticed it is called several times for the same node, so maybe it is called for each fields of the node being indexed..? yours looks a better solution, will come back to give the right answer – Gueno Jan 22 '12 at 14:03
  • oups, my bad, I don't have the apachesolr module installed, so I could not test your hook, thank you anyway ;) – Gueno Jan 25 '12 at 10:13
1

The hook to use is the following one:

/**
 * Implements hook_search_api_index_items_alter().
 */
function CUSTOM_MODULE_search_api_index_items_alter(array &$items, SearchApiIndex $index) {
    foreach($items as $id => $item){
        if(isset($item->YOUR_FIELD) && $item->YOUR_FIELD[key($item->YOUR_FIELD)][0]['value'] == 'FORBIDDEN_VALUE') unset($items[$id]);
    }
}
|improve this answer|||||
  • To make it clear to future readers: This code requires the Search API module. – kiamlaluno Sep 6 '12 at 6:06
  • I have tried this with Search API enabled and it does not work one bit. – asiby Dec 4 '17 at 2:49
1

Try to use the Finder module.

Finder allows Drupal site administrators to create flexible faceted search forms to find entities such as nodes or users based on the values of fields and database attributes.

NOTE: Finder module is an Unsupported Project due to security issue as of October 14, 2008

|improve this answer|||||

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.