It appears that Solr 3.5 is much more strict about which fields you are able sort by in a Solr Query. It will not let you sort by any field that is tagged as a multi-value field (e.g. sm_vid_resource_type).

The problem is that the ApacheSolr module assumes that all taxonomy fields are multi-value when building the document index (see apachesolr_term_reference_indexing_callback() in apachesolr.index.inc).

Has anyone else run into this and have any suggestions for how to resolve it?

I was thinking that it may be possible to alter apachesolr_term_reference_indexing_callback() so that it pulls the field definition and looks at the cardinality for the field. It could then correctly prefix the field in Solr as Single Value or Multi-value.

Steps to reproduce the error:

  • Setup Solr 3.5
  • Create a vacabulary and assign it to a node allowing only 1 value
  • Create some content with the terms from the vocabulary
  • Index the content
  • Directly query Solr, you'll see that the vocab fields are prefixed "im_" or "sm_" rather than "is_" or "ss_"
  • Implement hook_apachesolr_query_alter() to add a sort by the vocab field
  • Result is that the query now fails with a Solr error.

This may be related to the updates in issue #989658: Drupal 7 allows multiple term refernece fields for the same vocabulary.

Comments

Nick_vh’s picture

For fields, we index every first field as a single field also, so we can easily have sorts and multivalued fields.

The problem here is that a field that is assigned to only have 1 value, can easily be switched to a multivalued field in Drupal and solr would have to discard all his data. We already do this for default fields, so I'm ok with doing this for terms also.

Any other comments?

Nick_vh’s picture

Category: bug » feature
pwolanin’s picture

Sorting by taxonomy terms does seems to be a bit of an edge case. When would this be useful?

Nick_vh’s picture

Issue tags: +RC blocker

Seems more people has hit this bug. Marking it as RC blocker + bug

pwolanin’s picture

I still don't see the use case. If we want a single value for sorting only, maybe it should just be copied to ss_sort_* in Drupal?

Nick_vh’s picture

copying it to ss_sort_* would make sense to me, just hoping that it doesn't confuse users too much.
I'm also ok with just educating site builders that they cannot sort on multivalued fields and that they should use a little custom coding to make this work. We could even move this to the apachesolr_sort module where you could select which fields you need to have sortable and the module would do the rest magically

shawn_smiley’s picture

Here is the use case we had for sorting by a taxonomy field.

We have a Solr search page that allows searching on resources related to products. Those resources are categorized as Documents, Software Updates, How To Videos, etc. We need to sort by this category taxonomy so that we can group all of the resources in a given category together (e.g. All of the documents, then all of the how to videos, etc). Since we have several thousand resources in the potential result set, it doesn't make sense to bring back all of the results and try to sort them on the Drupal side.

You can see this by going to http://www.hunterindustries.com/resources. This page was properly sorting/grouping up until the site's hosting provider upgraded their Solr servers from 1.4 to 3.5.

Thanks for the mention of the apachesolr_sort module. I hadn't heard of that project before.

Also, can someone point me towards some info on the ss_sort_* field?

Nick_vh’s picture

Status: Active » Fixed
   <!-- Sortable version of the dynamic string field -->
   <dynamicField name="sort_*" type="sortString" indexed="true" stored="false"/>
   <copyField source="ss_*" dest="sort_*"/>

So every field in ss_* is copied to the exact same field in sort_*
ss_mytaxo = sort_mytaxo

This way you can enable sorts for the sort_mytaxo field and you should be good to go. Be aware that this conversion does not happen for the sm_* field since you cannot sort on multiple! You could, using some mapping alter convert your specific taxo field to a single field

/**
 *
 * @param type $mappings
 * @param type $entity_type
 */
function HOOK_apachesolr_field_mappings_alter(&$mappings, $entity_type) {
  // Add our per field mapping here so we can sort on the
  // taxonomy field by making it single. Solr cannot sort on multivalued fields
  // taxo_ref is our identifier of a custom field. Do check that this field is the correct name. 
  // We use the same properties as the list_text
  $mappings['per-field']['taxo_ref'] = $mappings['list_text'];
  // We set multiple explicitly as FALSE!
  $mappings['per-field']['taxo_ref']['multiple'] = FALSE;
}

Marking this as fixed until someone will reopen with a more ui friendly way of fixing this. But I hope some custom coding would suffice.

pwolanin’s picture

If you don't need the single value field e.g. for faceting, you might want to just copy on the Drupal side to a sort_ field.

Nick_vh’s picture

Status: Fixed » Closed (fixed)