Hi
I have created a custom content type using the date module. I want the search results to be sorted according to this custom date field instead of the created/modified date.

Can anyone point me in the right direction?

best regards
Libby

Comments

libbyy’s picture

Assigned: libbyy » Unassigned
sawtell’s picture

You can add a sort in hook_apachesolr_query_prepare().

function mymodule_apachesolr_query_prepare($query) {
  // add a new sort
  $query->setAvailableSort('ds_my_custom_field', array(
    'title' => t('Custom date'), // set the title
    'default' => 'asc', // set the default sort order (asc or desc)
  ));

You can find the list of fields in the solr index at http://example.com/admin/reports/apachesolr/SERVER_NAME

The field you want to add as a sort needs to be single valued, multivalued fields cannot be used to sort results.

Yorgg’s picture

I can't seem to create a single valued "ds_" field using date module.
It's being stored as a multivalued field even if I don't tick the "collect end date" or change the number of values to multiple. The workaround was to use published date to perform the sort.
How can I make this the default sort? I don't want to expose the sort block.

ressa’s picture

Thanks for the tips @ls206. I wanted to sort by a Price field, and had to use 'its_field_price' for it to work. There was also a 'itm_field_price' which didn't work.

I really think your explanation on how to add a custom sort field should be added to the README.txt file.

subhojit777’s picture

Issue summary: View changes

@j_nunes The point is apache solr module considers custom field as multivalued, nevertheless the field is single valued. You can see apachesolr_get_field_mappings($entity_type) in apachesolr.module. What you can do is alter field mapping for the custom field. See the code:

/**
 * Implements hook_apachesolr_field_mappings_alter().
 */
function my_module_apachesolr_field_mappings_alter(array &$mappings, $entity_type) {
  // By default every custom field is considered as a multivalued solr field.
  // @see apachesolr_get_field_mappings($entity_type)
  // We check here whether `field_date` is set to multivalued or not, and
  // according to that we change the cardinality of solr field.
  $field_date = field_info_field('field_date');
  if (!empty($field_date) && $field_date['cardinality'] == 1) {
    $mappings['per-field']['field_date'] = $mappings['date'];
    $mappings['per-field']['field_date']['multiple'] = FALSE;
  }
}

This way you can alter the cardinality of a solr field.

sarikak’s picture

How to name the fields to be used in sorting.As @ressa mentioned in above comment I am confused in naming the fields.
Also while using a custom fields date and year for sorting, I am getting results in wrong sort order.
Any help will be appreciated.Thanks