It seems that there's a pairing between a node's view and its indexing for the search module.

You may add new data to the index easily, through the 'update index' hook but I think you can't modify data present in the node's view, which is included in the index by default, and cannot be removed.

Node module's node_update_index hook calls the view hook and adds the result to the index. You may override this by implementing hook nodeapi 'view' but this will change the node's view.

So, how to change in the index parts of the node that belong to the view?

I have hacked node.module to get the functionality I needed. I'd like to know if there's a cleaner solution, though...
What I have done is creating a new hook 'rewrite index'. When node module is building indexes, it calls that hook, and, if any other module implementing it returns anything, overwrites the index with the new returned data (instead of appending it).

This goes in node.module, in the function node_update_index(), right before performing the search_index (around line 2127).

//Start hack
$indexData = node_invoke_nodeapi($node, 'rewrite index');
if(count($nodeData)) {
  $text = ''; //reset current index data
  foreach($indexData as $data) $text.=$data;
}
//end hack

//Update index
search_index($node->nid, 'node', $text);

And then, in my module:

function my_module_nodeapi(&$node, $op, $teaser, $page) {
  switch($op) {
    case 'rewrite index':
      if($node->type=='myCCKnode') { // Or any other condition
        $data = 'my custom data to index';
        return $data;
      }
      break;
  }
}

Is there a solution for this already? Will this happen with 5.0's new core cck-type nodes?

Thanks.

Comments

scb’s picture

Found a typo
Where it says if(count($nodeData)) { it should say if(count($indexData)) { of course...
Sorry :)

scb’s picture

Of course you may also place the hack's code before the real 'update index' hook call, so if any other module is implementing that hook (i.e. explicitly adding data to the index) the data will get indexed (the hack would just overwrite the previous hook view call).

Besides, you may also use this hack to avoid the indexing of any node type. You just have to return an empty string for that type in the rewrite index hook:

function my_module_nodeapi(&$node, $op, $teaser, $page) {
  switch($op) {
    case 'rewrite index':
      switch($node->type) { 
        //Rewrite index for myCCKnode
        case 'myCCKnode':
            $data = 'my custom data to index';
            break;
        //Avoid indexing of otherType nodes
        case 'otherType':
            $data = '';
      }
      return $data;
      break;
  }
}
killes@www.drop.org’s picture

Version: 4.7.3 » x.y.z

new features go into devel version.

coreb’s picture

Version: x.y.z » 6.x-dev

moving from x.y.z to 6.x-dev version queue

robertdouglass’s picture

Version: 6.x-dev » 7.x-dev

Would it be worthwhile considering theming the node rendering before going into the search index? This would be one way to expose some level of customization to people.

jhodgdon’s picture

Category: feature » support
Status: Active » Closed (fixed)

You can use hook_node_view_alter() and do something when $build['#view_mode'] == 'search_index'.
See:
http://api.drupal.org/api/function/hook_node_view_alter/7
http://api.drupal.org/api/function/node_view/7
http://api.drupal.org/api/function/_node_index_node/7

There are similar hooks in Drupal 6, I think. In any case, this is fixed in Drupal 7, and it would be a big API change to fix it in D6 if it isn't already possible there, so I'm closing this as a fixed support request. It's so old, I imagine the original person is satisfied...