I want search results to not show any comments on the results, or at least have a way of turning that behaviour on or off.

The problem is that on a high traffic site, with a lot of comments, when you try to search for the news stories themselves, it's very hard, because the search results are too often "polluted" with comments.

I tried Restricted Search and Search config, but, since comments are not a normal node type, I don't have access on those modules to turn off comment indexing.

I have been searching the forums for a while, and it seems I'm asking for the opposite of what everybody else is asking, but I think a search of just the content on the site, without comments, is a very valid search.

Comments

field4000’s picture

Hi all,

Has there been any resolution to this?

Cheers.

Socialisten’s picture

This is something I'd like to be able to do as well. The comments on my site are not as important as the content itself so I'd rather stop the search results getting clogged up like this.

I'm using D7 and the Search Configuration module, but have not found any way to achieve this. Can anyone help?

alan d.’s picture

This could be possibly by tricking the node search index function.

It should be possible to catch this event using the below hooks, then fake a not allowed flag on the comment_node_update_index()

  drupal_alter(array('node_view', 'entity_view'), $build, $type);
  $index_comments = &drupal_static('comment_node_update_index');
  $index_comments = FALSE;

So a hardcoded example using search config module as the base module:

function search_config_node_view_alter($build, $type) {
  if ($build['#view_mode'] == 'update_index') {
    $index_comments = &drupal_static('comment_node_update_index');
    $index_comments = FALSE;
  }
}

A cache flush is required. Note that you could replace "search_config_" with whatever module name that you paste this into. Take care in case that module already implements hook_node_view_alter() as you will end up with a fatal error.

If someone wants to test, I will consider adding this to the search configuration module :)


Alan Davison
tomogden’s picture

Thank you for pointing out the drupal_static variable. I have made some corrections and implemented this in my own custom module, but you ought to consider putting it into Search configuration.

/**
 * Implements hook_node_view_alter().
 *
 * Deactivate indexing of comments per node. 
 */
function commentsnoindex_node_view_alter($build) {
  if ($build['#view_mode'] == 'search_index') {
    $index_comments = &drupal_static('comment_node_update_index');
    $index_comments = FALSE;
  }
}

--
Tom/* Ogden