I copied and adapted the sample code for FileField integration, so now I have the mime type as facet; But I also would like to have the file name index for my custom queries, is it possible to have multiple indexes for the same field ?

I need more than one index per field, and because the field_name is the key in apachesolr_cck_apachesolr_cck_fields_alter() ($mappings['per-field'][$field_name] is the key) I can't do this.

Comments

swentel’s picture

Could this be related with #664884: Several php notices also ? There is a patch there.

pounard’s picture

I dont think both are linked. My post is more a feature request than a bug report. I want the ability to describe arbitrary indexes on more than one data.

In CCK, 1 field = n values = n * n technical data
What I need here is to create an index per technical data

The simpler example is FileField, there is a description, an filename, and a filemime. I want three indexes for my Solr queries (not much facets, I'm doing a custom module here).

robertDouglass’s picture

Please clarify what you mean when you say "index"?
In 6.2 there is a way to create multiple documents from every node, so it should also be possible to create separate documents for each stored value of a field. Please look at the comment search module.

ayalon’s picture

I have the same issue. I wanted to add the filemime and the filepath to the index as stored fields. (thx to pounard for the helper module).

In the callback function I can define only one array with a key=>value pair. How can I index more than one value per cck field?

Example callback for filefield (not working at all):


function apachesolr_cck_index_filemime_callback($node, $field_name) {
  $fields = array();
  foreach ($node->$field_name as &$value) {
    $fields[] = array('value' => $value['filepath']);
    $fields[] = array('value' => $value['filemime']);    
  }
  return $fields;
}

As a result, only the last value (in my case the filemime) will be added to the index.

ayalon’s picture

Can someone give a short repsonse to this?

I have the same case with the link cck field:

  • One filename (field_homepage)
  • Tow values to be indexed: URL and title
function apachesolr_cck_index_link_callback($node, $field_name) {
  $fields = array();
  foreach ($node->$field_name as &$value) {
    $fields[] = array('value' => $value['url']);
    $fields[] = array('value' => $value['title']);    
  }
  return $fields;
}

ayalon’s picture

Category: feature » bug

This issue is still open. It's possible to index multivalue fields. But it is not possible to index fields consisting of multiple values.

Example:
filefield: multiple entries containing field filepath, filemime, etc.
url: multiple urls containing fields url, title

apachesolr.index.inc


// Got a CCK field. See if it is to be indexed.
        $function = $cck_info['indexing_callback'];
        $fields = NULL;
        if ($cck_info['indexing_callback'] && function_exists($function)) {
          // NOTE: This function should always return an array.
          $fields = $function($node, $key, $cck_info);
        }
        // NOTE: This is now being done in the mandatory indexing callback.
        // $index_key = apachesolr_index_key($cck_info);
        if (is_array($fields)) {
          // NOTE: If this were done as $key => $value, and the $index_key were
          // done within the loop, could we support indexing one CCK field to
          // multiple Solr fields?
          foreach ($fields as $field) {
            if ($cck_info['multiple']) {

              //Multivalue = YES
              //BUT NOT with mutiple fields.
              
              $document->setMultiValue($field['key'], apachesolr_clean_text($field['value']));
            }
            else {
              $document->{$field['key']} = apachesolr_clean_text($field['value']);
            }
          }
        }
      }

very_random_man’s picture

One work around I'm currently playing with is to use the hook_apachesolr_cck_fields_alter() for quickly adding fields I want to facet by and adding further fields using hook_apachesolr_update_index().

For example if your searchable content is 'publication' and you wanted to facet by 'publisher', a CCK node reference, but also want to include some other field relating to publisher in your search response, you could use hook_apachesolr_cck_fields_alter() to add a per-field mapping for the publisher node ref (including the title, rather than the nid) and then use hook_apachesolr_update_index() to alter the index document and add the extra fields in.

function [custom]_apachesolr_update_index(&$document, $node) {
  switch($node->type) {

    case 'publication':
      $publisher_nid = $node->field_publisher_ref[0]['nid'];

      if ($publisher_node = node_load($publisher_nid)) {
        $document->addField('ss_some_publisher_field', $publisher_node->field_some_publisher_field[0]['value']);  
      } 
      break;
  }
}

Seems a bit clunky but works fine.

pounard’s picture

Yes it should work, actually doing this for a lot of custom indexes. For the case of CCK field, it would be better that the main API took care of this by itself.

3dloco’s picture

+1

ayalon’s picture

jep this is still an issue...

drewish’s picture

subscribing

jpmckinney’s picture

Category: bug » feature

Not a bug.

jpmckinney’s picture

Status: Active » Fixed

I think comment in #7 describes the way to go, going forward.

ayalon’s picture

Category: feature » bug
Status: Fixed » Active

Hey! This IS a bug and it is NOT fixed. The main API supports CCK but does not support every field properly. Please read the issue description and also the examples carefully. The file field ist the best example.

jpmckinney’s picture

The apachesolr module doesn't say anywhere "we support all CCK fields", so I don't consider this a bug. People have documented how to support some of the CCK fields that are not supported in core. Unless I see a patch, this is a support request, not even a feature request.

pounard’s picture

#15 @jpmckinney: You are totally right, supporting *all* CCK fields in a generic way seems impossible (metadata fields, file fields, etc..). But a cool feature would be a good base support for some well known CCK fields (including the original support request which was to handle nicely multivalued fields). Once this done, other CCK types than text and number basically could implement their own support as contrib modules.

jpmckinney’s picture

Sounds good, but I don't think any of the maintainers will write the patch, so this issue may be open for a while.

jpmckinney’s picture

Category: bug » feature
mguillermin’s picture

I was facing the same problem. I wanted to force a "multiple" type field in solr for a specific cck field. My solution was to authorize the setting of multiple from a hook with a small patch. It allows settings the field multiple even if it is not "multiple" in the cck content type.

In apachesolr.module, I've made this change on line 1549 :

<?php
          if (isset($mappings['per-field'][$field_name]['multiple'])) {
            $instance['multiple'] = (bool) $mappings['per-field'][$field_name]['multiple'];
          }
          elseif (isset($mappings[$field_type][$widget_type]['multiple'])) {
            $instance['multiple'] = (bool) $mappings[$field_type][$widget_type]['multiple'];
          }
          else { 
            $instance['multiple'] = (bool) $instance['multiple'];
          }
?>

With this modification, I can register the following mapping in my hook :

<?php
function sample_apachesolr_cck_fields_alter(&$mappings) {
  // On veut indexer ce champ à l'aide d'une callback et on force le type à "multiple"
  $mappings['per-field']['field_custom_multiple'] = array('callback' => 'sample_custom_index_callback', 'index_type' => 'string', 'multiple' => true);
  return $mappings;
}
?>

and use a custom code for indexation :

<?php
function sample_custom_index_callback($node, $fieldname) {
  $fields = array();
  foreach ($node->$fieldname as $field) {
    $fields[] = ...;
    $fields[] = ...;
  }
  return $fields;
}
?>

If you think this can be usefull, I can submit a "clean" patch.

jaykainthola’s picture

Hi,

Could you add complete patch for this?

Thanks

pwolanin’s picture

Status: Active » Closed (won't fix)
ayalon’s picture

Why was this issue closed without a comment?

pounard’s picture

I guess that it's rather old and the module so much evolved that it is not even revelant anymore, but yes I agree that a comment would be nice!

digi24’s picture

@pounard:
I do not think that this problem has disappeared yet, nor lost its relevancy.

I would be very thankful, if anyone find a way to handle multiple values in multivalue fileds in D6.