Problem/Motivation

The current code makes all fields multi-valued by default in apachesolr_get_field_mappings()

  // Field API allows any field to be multi-valued.
  'multiple' => TRUE,

This renders void the ability to sort on a single-valued field which seems an undesirable side effect to say the least. While the comment correctly points out that any field may be multi-valued, its use as a default is harmful.

The following code in apachesolr_entity_fields() gives the impression that it is changing the 'multiple' key to TRUE if cardinality != 1. However, because of the default being TRUE, this code is of no effect (save a contributed or custom module had overwritten this default).

if ($row['field']['cardinality'] != 1) {
  $row['multiple'] = TRUE;
}

Proposed resolution

Set 'multiple' key in search field information based on field cardinality.

Remaining tasks

Reveiew and test.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

solotandem created an issue. See original summary.

solotandem’s picture

Status: Active » Needs review
FileSize
1.65 KB

Attached patch implements proposed resolution.

q0rban’s picture

Status: Needs review » Needs work

This issue can cause some interesting bugs. For example, if you assume a bs_ prefixed (boolean, single value) solr document field is what it says it is, you may do a simple if statement on the field without realizing that it is an array of array(0 => 0), which would evaluate to TRUE.

The previously attached patch doesn't fix the whole problem. In field_apachesolr_index_document_build(), $document->setMultiValue() is used for all fields with the comment "// It's fine to use this method also for single value fields."

solotandem’s picture

What does what you wrote have to do with this issue? You make statements but what is their point?

q0rban’s picture

My points are:

  1. The bug doesn't just affect sorting. It can also affect conditional statements on single value fields on the Solr document.
  2. The attached patch in #2 doesn't fix the problem everywhere, since field_apachesolr_index_document_build() calls $document->setMultiValue(), without checking if the field is multiple or not.

:)