0

There's a search index for the Apache Solr module. However, I don't see any way to change this index. It seems that various fields and items are added to the index without rhyme or reason.

If I wanted to index a field called "keywords" how would I go about this? Do I have to use the method described in Adding Customized Fields to the Apache Solr index in Drupal?

Basically, there's a keyword field, which will have comma separated values of keywords, and I want to index this for Apache Solr search.

3 Answers 3

0

You should use hook_apachesolr_index_document_build in your custom module. This hook helps to extend basic SOLR Document object and adds custom fields for indexing.

If 'Keywords' field has machine name 'field_keywords' hook implementation may be as below

/**
 * Implements hook_apachesolr_index_document_build().
 */
function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  if ($entity_type == 'node') {
    // Adds 'Keywords' field as a separate field to Solr index.
    $document->keywords = isset($entity->field_keywords[LANGUAGE_NONE][0]['safe_value']) ?
      $entity->field_keywords[LANGUAGE_NONE][0]['safe_value'] : '';
  }
}
0

I'm not sure if the keywords field is a reference field (term or node entity). Please update the array key from the example, I used term reference as example

$term->field_keywords[LANGUAGE_NONE][0][tid]

Please try this, in your mymodule.module:

function mymodule_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type) {
  // I think you should do a foreach here, since you are using multi-valued field.
  foreach ($entity->field_keywords[LANGUAGE_NONE] as $key => $val) {
    $tid = $val['tid'];
    // The 1st letter indicates the data type and the last letter is
    // 'm' for multi-valued.
    $document->addField('im_field_keywords', $tid);
  }
}

function mymodule_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
  $query->addParam('fl', 'im_field_keywords');
}

Then, prepare the variable.

/**
 * Implements hook_apachesolr_process_results().
 */
function mymodule_apachesolr_process_results(&$results) {
  foreach ($results as $key => &$result) {
    $result['foo'] = $result['node']->im_field_keywords;
  }
}

Call this variable in you search-result.tpl.php e.g.

<?php print $result['foo']; ?>

I hope it helps.

0

Do you have the search_api module installed?

If the keyword field is already a part of your content-type bundle, you should be able to see it at /admin/config/search/search_api/index/default_node_index/fields

This page allows you to pick and choose which fields to index, and to boost the fields importance in the index.

1
  • if the included url doesn't lead anywhere you might have named it differently, go to this /admin/config/search/search_api/index/ and select the node index. Mar 3, 2017 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.