I've been wanting to include node comments in my node index for quite some time. There are basically two ways to do it. One way simply requires a small change to the Entity API module. However, that seems to have stalled: #1587912: Include reference to comments in Node metadata

The other way is fairly simple also, but I can't seem to pull it off. All we need is a small module that adds comments as a custom field to the index. There is a tutorial here, that I've tried following: http://ygerasimov.com/add-custom-field-search-api-apachesolr-index

I was able to follow that tutorial, but having never actually coded much for Drupal, I couldn't figure out how to get the comments and put them into the correct form for indexing.

Could some kind soul who knows how to do this please whip up a module that adds comments as a custom field? Or even if somebody could just give me some help on IRC or something to figure out how to lookup the comments from a node and put them into a fulltext field, I'd be very grateful.

I'm not the only one asking for this functionality (see above issue), and I think it would make a nice addition to the list of extensions for this module.

Thanks,
-Joseph

Comments

askibinski’s picture

Try the entity patch at #8 here: #1414688: Add the comments property in entity_metadata_node_entity_property_info()

Then you will be able to add a related comments field. After that you need to add an additional field for the comment body.

tonybaqain’s picture

try using hook_searchapi_entity_property_info_alter(&$info) hook from search_api module, which will let you alter information of what should be indexed and sent to to search server.

for example :

function mymodule_searchapi_get_tags($entity, $options, $name, $entity_type, &$info) {
    // return the comments here, as you have everything in the function variables - i.e. use return.
}

function mymodule_searchapi_entity_property_info_alter(&$info) {
    if (isset($info['node']['bundles'])) {
        // For each content type.
        foreach ($info['node']['bundles'] as $bundle_type => $bundle) {
                $info['node']['bundles'][$bundle_type]['properties']['comments'] = array(
                    'label' => 'node comments',
                    'description' => 'the comments of the node',
                    'type' => 'text', // or whatever you want here 
                    'getter callback' => 'mymodule_searchapi_get_tags',
                    'computed' => TRUE
                );
            }
        }
    }
}

Hope this helps.

pmol123’s picture

Priority: Normal » Major
Issue summary: View changes
Issue tags: +solr, +comments, +computed field, +Search API

Here is a pretty simple solution for you:

I usually always install the "Computed Field" to my Drupal installs.

I create a Computed Field in my Content-Type / Bundle with the PHP Code below. Then add the field to the SOLR fields and you are done.

STEPS:
1) Enable the "Computed Field" in your modules.

2) Add a "Computed Field" to your content-type/bundle

3) Use the Code below for your computed field

4) Add the field to the SOLR index.

Hope this helps. :)

//-----"Computed Code (PHP)" Section ------------------------------------------

$entity_field[0]['value'] = "";
//dsm( $entity );

$query = db_select( 'comment', 'c' );
$query -> join( 'field_data_comment_body', 'b', 'c.cid = b.entity_id' );
$query -> fields( 'c', array( 'subject' )  );
$query -> fields( 'b', array( 'comment_body_value' ) );
$query -> condition( 'c.nid', $entity->nid, '=' );
$query->condition( 'b.entity_type', 'comment', '=' );
$results = $query->execute() ->fetchAll();
//dsm( $results );

$comments_str = "";
foreach( $results as $key => $value )
     $comments_str .=  sprintf( " %s \n %s \n ",  $value->subject, $value->comment_body_value );

//dsm( $comments_str );

$entity_field[0]['value'] = $comments_str;

//---------------------------------------------
//-----"Display Code (PHP)" Section ------------------------------------------
$display_output = $entity_field_item['value'];
//---------------------------------------------
jtbayly’s picture

Priority: Major » Normal
Status: Active » Fixed

Thanks everybody. pmol, that's ingenious.

The patch for the entity module mentioned in #1 took care of it nicely for us.

Cheers,
-Joseph

drunken monkey’s picture

Issue tags: -solr, -comments, -computed field, -Search API
pmol123’s picture

Thank you, the above worked well if the node was updated. However, it did not work if a comment was added. ;(
I tried doing the same thing with the comment fields but the Search API fields would not see the fields added to the comments.

Hence, I wrote a simple hook_cron module to do the same thing whereby I gathered the comments and stuffed them into the node.
This approach is working great!.

Patrick

jtbayly’s picture

Hi Patrick,

I don't know how I missed that problem. It was affecting us too. Thanks for mentioning it!

I did a bit of searching, and here is the official issue for that problem: #2007692: Changes in related entities don't lead to proper re-indexing

As suggested there, I just fixed it with Rules. I created a rule to re-index the node if a comment was added or updated. This method is also tested and working fine for us.

Thanks again,
-Joseph

Status: Fixed » Closed (fixed)

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