Drupal Search : Index All Fields of a CCK Node

The Drupal Search API is powerful and pretty intuitive, but finding details on how to do something as simple as enabling searching on fields other than title and body could be a little daunting.

It's actually quite a simple concept. Anything exposed to your node view will be indexed. You can easily accomplish this by editing your content type and setting the Display Fields setting to Plain Text.

But what if you have a node view which you don't want everything in $node->body? i.e. inside node-mynodetype.tpl.php, you separate out the author field from the body field.

So here's the scenario: You're creating a node template for a CCK type 'quote'. A quote contains text fields for title, author, and body. Well Drupal handles the title and body already so we just need to add a text field for author.

Your template looks something like this:

Author: <?php print $node->field_author[0]['value']; ?><hr /><p><?php print $node->body; ?></p>

The problem now is that Author will show up twice. The easy way to fix this is to just use the content member of the $node variable. CCK will automatically add the fields and you can access them like this:

$node->content['field_author']['#value'];
$node->content['body']['#value'];

So now just apply that format to your template and you can isolate the node fields while allowing all fields to be indexed by the Drupal search engine.

Author: <?php print $node->content['field_author']['#value']; ?><hr /><p><?php print $node->content['body']['#value']; ?></p>

If you're not using CCK, just make sure your node exposes the content in the view hook.

Alternatively, you can either utilize hook_update_index (or hook_nodeapi with $op='update index'), or you can manually update an items index with search_index.