How can I stop specific CCK fields from showing up in search results?

I have a simple drop box for the edited status of a node with the options 'Draft, To be edited, Edited' and so on. As you might guess, this is not shown on the final published node. Unfortunately, whenever you search for words near the beginning of the node, it'll return something like this:

Original Node Title Goes Wild
Status: No changes; ready Here begins the node's written content...

I'd rather it didn't. Is this possible?

Comments

meatbites’s picture

Title: Hide CCK field/s from search » Hide CCK field/s from search results and indexing
Category: support » feature

I found a way to get exactly what I'm after, but I was forced to modify node.module.

Basically, you need to disable indexing of the CCK field and stop the search result from showing the field as text padding. Unfortunately, hacking core seems to be the only option for this.

Modified node_search function (near the end of case 'search'):

// Build the node body.
$node = node_load($item->sid);
$node = node_build_content($node, FALSE, FALSE);

  // Begin edit.
  unset(
    $node->field_fieldtohide,
    $node->content['field_fieldtohide']
  );
  // End edit.

$node->body = drupal_render($node->content);

Modified node_update_index function:

$node = node_load($node->nid);

// Build the node body.
$node = node_build_content($node, FALSE, FALSE);

  // Begin edit.
  unset(
    $node->field_fieldtohide,
    $node->content['field_fieldtohide']
  );
  // End edit.

$node->body = drupal_render($node->content);

So, I guess this is a feature request.

kardave’s picture

Hey, this feature's opposit is what i need :)

I would like hidden CCK field to show up in search results.

I use CCK fields for subtitles, and IDs for big amount imported stories.
These fields are set to hidden, but shown by my custom theme, except the imported stories' previous IDs.

I made this old id field just to let visitors find the old articles. (search404 module).

Until I can't set CCK fields to have indexed or not, I must do the known CSS hide trick.
(set the fiels to visible, but hide by css)

Thanks your work so much!

K. David

TimeBandit’s picture

You could consider using the Views Fast Search module to help modify search: http://drupal.org/project/views_fastsearch
Then use phptemplate_views_fastsearch_display function in template.php and you won't have to modify node.module.

example (after installing and setting up fast search):

function phptemplate_views_fastsearch_display(&$view, &$items, $type) {
  drupal_add_css(drupal_get_path('module', 'search') .'/search.css', 'module', 'all', FALSE);

  if (isset($items) && is_array($items) && count($items)) {
    // NOTE: using global to pass values from
    // views_fastsearch_views_handler_search_index
    global $_vfs_search_keys;
    if (isset($_vfs_search_keys)) {
      $keys = array();
      foreach ($_vfs_search_keys as $value) {
        $keys = array_merge($keys, $value);
      }
      $excerpt_keys = implode(' ', $keys);
    }

    //$output = 'asd<h2>'. t('Search Results') .'</h2>';
    $output .= '<ol class="search-results">';
    foreach ($items as $item) {
      // Build the node body.
      $node = node_load($item->nid);
      $node = node_build_content($node, FALSE, FALSE);
      
		
  // Begin edit.
  unset(
    $node->field_date,
    $node->content['field_date'],
    $node->field_company,
    $node->content['field_company']
  );
  // End edit.
		
		
		$node->body = drupal_render($node->content);

      // Fetch comments for snippet
      $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');

      // Fetch terms for snippet
      $node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');

      $extra = node_invoke_nodeapi($node, 'search result');
      $entry = array(
        'link' => url('node/'. $item->nid, NULL, NULL, TRUE),
        'type' => node_get_types('name', $node),
        'title' => $node->title,
        'user' => theme('username', $node),
        'date' => $node->changed,
        'node' => $node,
        'view' => $view,
        'extra' => $extra,
        'score' => $item->score,
        'snippet' => search_excerpt($excerpt_keys, $node->body)
      );
      $output .= theme('views_fastsearch_item', $entry, $type);
    }
    $output .= '</ol>';
    return $output;
  }
}

I also changed the results to an ordered list so you may want to use the original function from views_fastsearch.module...

deltab’s picture

+1

nally’s picture

Has anyone made any progress with this issue?

Presumably, having the search system ignore certain fields could make for a lighter, faster search.

Also, can anyone with specific search expertise mention anything on the possibility of having a search index for each role?

Also, in particular, are we still only talking about D5?

TimeBandit’s picture

I did the following in a more recent project. I don't use fastsearch any longer, and this is for Drupal 6. It basically disregards the actual search result ($snippet variable) and looks at the item's body['#value']. Then I strip out carriage returns, spaces, yadda. Your mileage may vary... you may need to strip other stuff out of the result.

Put this in search-result.tpl.php in your theme. Of course, I have extraneous code and commented-out code so clean it up as you need to:

<?php //print "<pre>"; print_r($result['node']->content['body']['#value']); print "</pre>";?>
<?php $keys=arg(2); $text=$result['node']->content['body']['#value']; $no_cck_result=search_excerpt($keys, $text); $no_cck_result = ereg_replace("[\n\r\t]", " ", $no_cck_result); $no_cck_result = preg_replace('/\s+/', ' ', $no_cck_result); ?>
<?php //print "<pre>"; print_r($no_cck_result); print "</pre>";?>
<dt class="title">
  <a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
  <?php if ($snippet) : ?>
    <p class="search-snippet"><?php print $no_cck_result; ?></p>
  <?php endif; ?>
  <?php if ($info) : ?>
  <!--<p class="search-info"><?php print $info; ?></p>-->
  <?php endif; ?>
</dd>
nally’s picture

Thanks TimeBandit.

I'm interested in the possibility of preventing (specific, certain) CCK fields from being indexed at all.

Is it true that your solution filters search hits out at theme time? I'm looking for something that would keep that node out of the search result set in the first place by preventing that field from being in the search index at all.

(I'm also trying to get that to work with Solr integration.)

Gerhard Killesreiter’s picture

Status: Active » Fixed

CCK has a setting for this:

Go to your content type, then "display fields", then "search", and then you can chose whether fields should be excluded during indexing and/or display.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ceci123’s picture

CCK has a setting for this:

Go to your content type, then "display fields", then "search", and then you can chose whether fields should be excluded during indexing and/or display.

I tried this solution and it doesn't work. See My configuration.

Am I don't it right?