2

We have replaced Drupal's core search with SolR but we have noticed a recursiveness issue regarding the search results, for ex. when searching “executive committee”, documents created in 2006 and 2007 are listed before newer documents created in 2010. (http://www.etuc.org/search-page)

What we would like to do is to sort the articles by date and by relevance.

We have tried to alter this at the View level (date + Search:relevance) but it short circuits SolR.

We have looked at the search api settings and it seems that one cannot boost date field. Only fulltext fields are ‘boostable’ (?)

We have also found a few articles related to the subject:

but they weren’t much help.

We are using Drupal 7, SolR 3 and the following modules

  • Search API 7.x-1.11
  • Search API autocomplete 7.x-1.0
  • Search facets 7.x-1.11
  • Search views 7.x-1.11
  • SolR search 7.x-1.4

Does anybody has an idea how to fix this issue? Thank you very much in advance.

1
  • did u finally get a solution ? if so pls share
    – vishal
    Sep 3, 2014 at 20:01

2 Answers 2

1

To do this (Drupal 7), one needs to implement the below hook:

hook_search_api_solr_query_alter(&$call_args, SearchApiQueryInterface $query)

This hook can go in the theme or module layer.

Example code could be:

/**
 * Implements hook_search_api_solr_query_alter().
 *
 * @see https://www.drupal.org/node/2009760#find-out-field-id
 * @see https://www.drupal.org/project/search_api_solr/issues/2194055
 * @see https://www.metaltoad.com/blog/date-boosting-solr-drupal-search-results
 * @see https://www.drupal.org/forum/support/module-development-and-code-questions/2014-12-18/extending-search-api-solr-search-with
 * @see https://www.drupal.org/project/search_api_solr/issues/2392499
 * @see https://www.drupal.org/project/search_api/issues/1694698
 * @see https://books.google.com.au/books?id=othpBgAAQBAJ&pg=PA157&lpg=PA157&dq=3.16e-11+solr
 */
function mytheme_search_api_solr_query_alter(&$call_args, SearchApiQueryInterface $query) {
  // Boost more recently created content. This is taken directly from an
  // apachesolr query with +1 boost (+1 for every 100).
  $fields_names = $query->getIndex()->server()->getFieldNames($query->getIndex());
  if (isset($fields_names['created'])) {
    // If using node property 'created', get solr name of it  e.g. 'dm_created'.
    $created_field_name = $fields_names['created'];
    // @codingStandardsIgnoreStart
    // Field name version.
    // $created_field_name = $fields_names['field_date'];
    // @codingStandardsIgnoreEnd
    // Ensure '$call_args['params']['bf']' exists.
    if (!isset($call_args['params']['bf'])) {
      $call_args['params']['bf'] = [];
    }
    $call_args['params']['bf'][] = "recip(ms(NOW,{$created_field_name}),3.16e-11,0.5,0.5)^100.0";
  }
}
0

I'm not 100% sure this will work, but definitely worth a try.

Go to /admin/config/search/search-api/index/your_index/edit

And then under this heading: CONFIGURE THE DEFAULT TRACKER There are two options to set the "Default index tracker which uses a simple database table for tracking items."

Select "most recent items first". You will need to reindex the site after saving the settings for the change to take effect.

 Index items in the same order in which they were saved
 Index the most recent items first```
1
  • This doesn't work. This only modifies in what order Drupal will add items to the index. It will not modify the order in which items will show up in the search results.
    – vrijdenker
    Dec 10, 2020 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.