How to remove data in a node's view from its search index

scb - October 18, 2006 - 08:48
Project:Drupal
Version:7.x-dev
Component:node system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

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).

<?php
//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:

<?php
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.

#1

scb - October 18, 2006 - 08:59

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

#2

scb - October 25, 2006 - 07:39

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:

<?php
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;
  }
}
?>

#3

killes@www.drop.org - November 12, 2006 - 17:18
Version:4.7.3» x.y.z

new features go into devel version.

#4

coreb - November 28, 2006 - 18:19
Version:x.y.z» 6.x-dev

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

#5

robertDouglass - April 15, 2008 - 13:29
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.

 
 

Drupal is a registered trademark of Dries Buytaert.