Hello.
I've created field 'Include to the search index' for node type 'publication'. It means that I want to exclude some nodes from index. I explored apachesolr code a little bit and found hook_apachesolr_entity_info_alter(). Implementing it I try to change status callback where I can check a value of my field. This is code below.

function inti_apachesolr_entity_info_alter(&$entity_info) { 
  $entity_info['node']['status callback'] = 'inti_index_node_status_callback';
}

function inti_index_node_status_callback($node, $type) {
  if ($type == 'publication') {
      $status = (!empty($node->field_do_search)) ? ($node->status && $node->field_do_search['und'][0]['value']) : $node->status;
  } else {
    $status = $node->status;
  }
  return $status;
}

But when I reindex my content, I still can see all publications.
As I could understand while "debugging", inti_index_node_status_callback() is never called, but I don't know why. Am I doing something wrong?

Comments

nick_vh’s picture

Can you verify this callback is being called?
apachesolr_index_node_status_callback?

I'm thinking we might have a mistake and that this callback probably should be called in apachesolr_entity_should_index.

Afaik these functions are currently only called whenever a entity is updated or inserted, perhaps you are trying to reindex all at once?

Georgique’s picture

Some your code:

/**
 * Add information to index other entities.
 * There are some modules in http://drupal.org that can give a good example of
 * custom entity indexing such as apachesolr_user_indexer, apachesolr_term
 * @param array $entity_info
 */
function hook_apachesolr_entity_info_alter(&$entity_info) {
  // REQUIRED VALUES
  // myentity should be replaced with user/node/custom entity
  $entity_info['node'] = array();
  // Set this entity as indexable
  $entity_info['node']['indexable'] = TRUE;
  // Validate each entity if it can be indexed or not
  $entity_info['node']['status callback'] = 'apachesolr_index_node_status_callback';
  // Build up a custom document.
  $entity_info['node']['document callback'][] = 'apachesolr_index_node_solr_document';
  // What to do when a reindex is issued. Most probably this will reset all the
  // items in the index_table
  $entity_info['node']['reindex callback'] = 'apachesolr_index_node_solr_reindex';

  // OPTIONAL VALUES
  // Index in a separate table? Useful for huge datasets.
  $entity_info['node']['index_table'] = 'apachesolr_index_entities_node';
  // Execute custom callback on each cron run.
  // See apachesolr_index_node_check_table
  $entity_info['node']['cron_check'] = 'apachesolr_index_node_check_table';
  // Specific output processing for the results
  $entity_info['node']['apachesolr']['result callback'] = 'apachesolr_search_node_result';
}

/**
 * Implements hook_entity_info_alter().
 */
function apachesolr_entity_info_alter(&$entity_info) {
  // Set those values that we know.  Other modules can do so
  // for their own entities if they want.
  $default_entity_info = array();
  $default_entity_info['node']['indexable'] = TRUE;
  $default_entity_info['node']['status callback'] = 'apachesolr_index_node_status_callback';
  $default_entity_info['node']['document callback'][] = 'apachesolr_index_node_solr_document';
  $default_entity_info['node']['reindex callback'] = 'apachesolr_index_node_solr_reindex';
  $default_entity_info['node']['index_table'] = 'apachesolr_index_entities_node';
  $default_entity_info['node']['cron_check'] = 'apachesolr_index_node_check_table';
  // apachesolr_search implements a new callback for every entity type
  // $default_entity_info['node']['apachesolr']['result callback'] = 'apachesolr_search_node_result';
  //Allow implementations of HOOK_apachesolr_entity_info to modify these default indexers
  drupal_alter('apachesolr_entity_info', $default_entity_info);

  // First set defaults so that we needn't worry about NULL keys.
  foreach (array_keys($entity_info) as $type) {
    if (!isset($entity_info[$type]['apachesolr'])) {
      $entity_info[$type]['apachesolr'] = array();
    }
    if (isset($default_entity_info[$type])) {
      $entity_info[$type]['apachesolr'] += $default_entity_info[$type];
    }
    $default = array(
      'indexable' => FALSE,
      'status callback' => '',
      'document callback' => '',
      'reindex callback' => '',
    );
    $entity_info[$type]['apachesolr'] += $default;
  }

  $env_id = apachesolr_default_environment();
  // For any supported entity type and bundle, flag it for indexing.
  foreach ($entity_info as $entity_type => $info) {
    if ($info['apachesolr']['indexable']) {
      $supported = apachesolr_get_index_bundles($env_id, $entity_type);
      foreach (array_keys($info['bundles']) as $bundle) {
        if (in_array($bundle, $supported)) {
          $entity_info[$entity_type]['bundles'][$bundle]['apachesolr']['index'] = TRUE;
        }
      }
    }
  }
}

/**
 * Status callback for ApacheSolr, for nodes.
 */
function apachesolr_index_node_status_callback($node, $type) {
  wtf($node);
  return $node->status;
}

What I see: firstly, I think that hook should be named hook_apache_solr_entity_info, also it should be added altering hook hook_apachesolr_entity_info_alter. In function apachesolr_entity_info_alter we can see info about HOOK_apachesolr_entity_info in comments but I didn't found it in code.
Secondly, as you can see, I added a call of wtf() function to callback apachesolr_index_node_status_callback; this functions writes content of its argument to file. After reindexing all content file was empty, so I think that callback had never been calling. Thirdly, I noted that implementations of hook_apachesolr_entity_info_alter are calling only when I clearing the cache. Is it write?

Georgique’s picture

Sorry for up, but are there any news?

nick_vh’s picture

Status: Active » Closed (duplicate)
sawtell’s picture

Hope this isn't off topic
I have the same issue that the status callback only takes effect when the node is updated.

What is the method to use if I want the reindex process to acknowledge the status callback?

seemas’s picture

I also was looking how to exclude some nodes from index.
Here is the module for that: https://drupal.org/project/apachesolr_exclude_node
7.dev just launched.