Hello Solr team. Thanks in advance for the module to date, it is excellent. This is hopefully a small request for some additional documentation for #271753: More Granular cck_field_mappings, Please. The closed issue at hand implemented support for granular use of CCK fields as part of indexes or facts. Specifically, I have many text fields and other fields that need to be indexed that aren't. In many cases I don't want them to be faceted, but I do want them to be indexed. A good example is a D9 node profile with two different text fields for first name and last name, which I don't want to facet but I DO want to index for searching purposes.

The references I have located to date are:

Understanding ApacheSolr CCK (which describes how to do this for a field_type, but not an individual field.
http://acquia.com/blog/understanding-apachesolr-cck-api#comment-form

The patch for 271753, here:
http://drupal.org/files/issues/cck-more-granular-271753-17-D6.patch

This specifies a few examples, such as:
// A field-specific mapping would look like:
// $mappings['per-field']['field_model_name'] = array('callback' => '', 'index_type' => 'string');
// or
// $mappings['per-field']['field_model_price'] = array('callback' => '', 'index_type' => 'float');

My request is for something similar to the blob of code that Robert posted in the first link, but specific to doing this at the field level. Thanks in advance.

Comments

rjbrown99’s picture

More documentation, none of which I have been able to get working yet:

#436324: How to use hook_apachesolr_cck_field_mappings
#267367: Document apachesolr_cck_field_mappings
#336157: CCK field indexing broken

Here is a non-working try to get it to index a textfield called 'givenname'.

function mymodule_apachesolr_cck_field_mappings() {
  $mappings = array();
  $mappings['text'] = array(                        
    'callback' => 'mymodule_solr_callback',
    'index_type' => 'string',
    'widget_types' => array(
      'text_textfield' => TRUE,
    ),
  );
  return $mappings;
}

function mymodule_solr_callback($node, $fieldname) {
  $fields = array();
  foreach ($node->$fieldname as $field) {
    $fields[] = array('safe' => check_plain($field['givenname']));
  }
  return $fields;
}

Here is another non-working try based on the other documentation links.

function mymodule_apachesolr_cck_field_mappings() {
  $mappings = array();
  $mappings['per-field']['field_givenname'] = array(
    'index_type' => 'string',
    'callback' => 'mymodule_solr_callback',
    'facet' => FALSE,
  );
  return $mappings;
}

function mymodule_solr_callback($node, $key) {
  $to_explode = array_pop($node->$key);
  $field_parts = explode(" ", $to_explode['view']);
  foreach ($field_parts as $field) {
    $fields[] = array('view' => $field);
  }
  return $fields;
}

Just to clarify - this is with D6, latest Solr code, indexing for a single CCK field called "field_givenname" of type text with a widget type of text field. It is a required field with a maximum of 1 value.

pwolanin’s picture

The rendered content of all cck fields is already indexed as part of the node body.

Are you trying to index these separately in order to effect the score of search results?

rjbrown99’s picture

Title: Quick code snippit for granular cck_field_mappings » Documentation - All CCK fields are indexed by default.
Status: Active » Closed (fixed)

First of all, thank you for the reply. This actually helped clarify that I don't need to do what I was trying to do. The documentation was not clear to me that all fields were indexed as I was thinking only certain ones were.

I have something going on with my system. I had some intermittent issues indexing CCK fields of a content profile. The fields were not showing up in search results. I have since re-built the search index and they are working at the moment, but I have no clue yet why they weren't. The symptoms were that I'd run a search on a CCK field, say first_name which is a text field, and no results would come back.

Hopefully this will help others. I changed the title and market this as closed.