Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save szeidler/272560bd75968e4a0ed23b81853c0e9e to your computer and use it in GitHub Desktop.
Save szeidler/272560bd75968e4a0ed23b81853c0e9e to your computer and use it in GitHub Desktop.
Add a boost function, to prioritize recent content in Search API Solr queries. More recent articles will get boosted heavily, older ones less and less.
<?php
/**
* Boost published_at field to prioritize newer content.
*
* @see https://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_boost_the_score_of_newer_documents
* Implements hook_search_api_solr_query_alter().
*/
function my_module_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query) {
// Boost more recently created articles. Boost the field with score +8 shrinking during 365 days.
$boost_field = 'ds_published_at';
$call_args['params']['bf'][] = 'recip(ms(NOW/HOUR,' . $boost_field . '),3.16e-11,0.5,0.5)^800.0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment