Hello,

This support request is more or less related to what the Date Module offers, but I thought it might be interesting to mention here.
By default in Drupal Core, there are two search ranking factors: Keywords Relevance, and Post Date.

I was wondering if anybody achieved to use the CCK Date field as a ranking factor instead of the Post date of a node. I know something similar to a search could be achieved through Views, but I was wondering specifically about the Core Search Module and how it could use a Date field.

Thanks,

Comments

arlinsandbulte’s picture

Status: Active » Closed (won't fix)

Hi,
I am just indiscriminately closing all support requests with no activity for more than 1 year.
If you consider this to still be a valid issue / support request, feel free to re-open.
BUT,
if you are re-opening this issue, please provide specific details on how this issue can move forward.

Thanks.

coredumperror’s picture

Status: Closed (won't fix) » Fixed

I apologize for re-opening a long-closed ticket, but this page is the top result on google for "drupal date search ranking", so I figured that those who come searching for this solution in the future would appreciate an answer.

Here's how to implement hook_ranking() to add a new ranking score which uses a Date field:

function caltech_ranking() {
  $ranking = array();
  $ranking['date_field'] = array(
    'title' => t('Date field'),
    
    // Note that we use i.sid, the search index's search item id, rather than n.nid.
    'join' => array(
      'type' => 'LEFT',
      'table' => 'field_data_DATE_FIELD_NAME',
      'alias' => 'date_field',
      'on' => 'i.sid = date_field.entity_id',
    ),
    
    // The score must fall between 0 and 1, so dvide the timestamp by :max_timestamp.
    'score' => 'UNIX_TIMESTAMP(date_field.DATE_FIELD_NAME_value) / CAST(:max_timestamp AS DECIMAL)',
    
    // The timestamp 2000000000 is in 2033. If this code is still in use in 2033, there will be much bigger
    // problems than event search results being out of order.
    'arguments' => array(':max_timestamp' => 2000000000),
  );
  return $ranking;
}

Replace DATE_FIELD_NAME with the machine name of the date field that you want to rank by. Once you clear your site's cache, "Date field" will appear in the Content Ranking section on the admin/config/search/settings page of your site.

The algorithm implemented through the score and arguments values sorts results in descending date order. You could get ascending date order by adding 1 - in front of UNIX_TIMESTAMP.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.