I have a CCK field in content type=bigbook called field_company. It is a text field. I would like to make sure it is being indexed, and then add a facet for it. I think it is already being indexed, but not sure.

I have looked at hook_apachesolr_cck_field_mappings and have looked at a coule examples, but nothing explains how to do it well.

Anyone with a solid example? Does the field need to be a selection/checkbox widget? Can I do it with text field?

Thanks.

Comments

robertdouglass’s picture

Status: Active » Fixed

Have you seen this article that I wrote? http://acquia.com/blog/understanding-apachesolr-cck-api

Reopen if there are outstanding questions not covered in the article.

pwolanin’s picture

There is a patch here to change (but improve) this behavior: http://drupal.org/node/271753

billnbell’s picture

Yes. the article is good if you have a field type across all CCK and you need to create a facet. It does not address my question. I have a simple CCK field and want to make a facet on it. Howe about a facet for a field in CCK?

/**
* Implementation of hook_apachesolr_cck_field_mappings
*/
function example_apachesolr_cck_field_mappings() {
  $mappings = array();
  // how do I get a text field named $node->field_employer_type[0]['view'] ?
  $mappings['filefield'] = array(
    'callback' => 'example_callback',
    // Common types are 'text', 'string', 'integer', 
    // 'double', 'float', 'date', 'boolean'
    'index_type' => 'text',
    // what type is it? How do I find out?
    'widget_types' => array(
      'filefield_widget' => TRUE, // nope
      'imagefield_widget' => TRUE, // nope
    ),
  );
  return $mappings;
}

/**
* A function that gets called during indexing.
* @node The current node being indexed
* @fieldname The current field being indexed
*
* @return an array of arrays. Each inner array is a value, and must be
* keyed 'safe' => $value
*/
function example_callback($node, $fieldname) {
  $fields = array();
  // My field is already indexed. I just need to add it to facet? why loop in my case?
  foreach ($node->$fieldname as $field) {
    $fields[] = array('safe' => check_plain($field['filemime'])); // $node->field_employer_type[0]['safe']; ??
  }
  return $fields;
}
robertdouglass’s picture

Status: Fixed » Active

Good questions. We need to consider these when evaluating the patch Peter linked to above.

billnbell’s picture

Can you just walk me through how it works? How do I add a facet on the page (through caching I guess). These 2 functions are not clear if you have a text field.

Thank you.

pwolanin’s picture

@billinbell - see above and linked issue - currently you cannot specify per field to make facets, only per widget type.

pwolanin’s picture

Status: Active » Closed (duplicate)