Hi!

I tried as many solutions I could found here and on google, but nothing works for me. For example I want to add an image field to the search index. I tried this from here:

function customsearch_apachesolr_update_index($document, $node, $namespace) {
  // When indexing nodes, add field from my custom content type
  if ($node->type == 'content_page') {
    // grab the image URL
    $image_uri = $node->field_content_image['und'][0]['uri'];
    // add the image and description to the document in the solr index
    $document->addField('ss_my_content_page_image', $image_uri);
  }
}
function customsearch_apachesolr_query_prepare($query) {
  // Add the image
  $query->addParam('fl', 'ss_content_page_image');
}

field_conent_image is the name of my field, content_page my conent_type
The image is a visible field in my content type.

I delete the whole index and re-index all, but never my custom field is shown in the apache solr report. Can somebody help me? I don't know what I am doing wrong.

Comments

Nick_vh’s picture

Status: Active » Fixed

Have you tried reading apachesolr.api.php?
Use one of those two hooks, one of them is a generic one, the other is entity_type specific.

/**
 * Build the documents before sending them to Solr.
 * The function is the follow-up for apachesolr_update_index
 *
 * @param integer $document_id
 * @param array $entity
 * @param string $entity_type
 */
function hook_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {

}

/**
 * Build the documents before sending them to Solr.
 *
 * Supports all types of
 * hook_apachesolr_index_document_build_' . $entity_type($documents[$id], $entity, $env_id);
 *
 * The function is the follow-up for apachesolr_update_index but then for
 * specific entity types
 *
 * @param $document
 * @param $entity
 * @param $entity_type
 */
function hook_apachesolr_index_document_build_ENTITY_TYPE(ApacheSolrDocument $document, $entity, $env_id) {
  // Index book module data.
  if (!empty($entity->book['bid'])) {
    // Hard-coded - must change if apachesolr_index_key() changes.
    $document->is_book_bid = (int) $entity->book['bid'];
  }
}
stayontherun’s picture

Thx for your help, now it works with the function you posted. I haven't tried reading the api file before, now I know that hook_apachesolr_update_index is deprecated since 7.x-1.0-beta15 of this module.

Status: Fixed » Closed (fixed)

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

moonshdw8’s picture

Sorry to revisit this, but it sounds like you were in the same place I am right now.
Could you please show the code that ended up working for you?

And just to be sure, what file are you adding the "function hook_apachesolr_index_document_build..." to?

Thanks so much for any help you can give.

sankuratri’s picture

I used following code and that worked to index my custom field(field_your_filed_name)

function your_module_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
if (!empty($entity->field_your_filed_name['und'][0]['value'])) {
$document->ss_field_your_filed_name= $entity->field_your_filed_name['und'][0]['value'];
}
}

moonshdw8’s picture

Thanks for this code. Did you add this to your template.php file, did you create a new module to do this, or did you add this directly to a template file?