You are here

function apachesolr_search_add_boost_params in Apache Solr Search 8

Same name and namespace in other branches
  1. 5.2 apachesolr_search.module \apachesolr_search_add_boost_params()
  2. 6.3 apachesolr_search.module \apachesolr_search_add_boost_params()
  3. 6 apachesolr_search.module \apachesolr_search_add_boost_params()
  4. 6.2 apachesolr_search.module \apachesolr_search_add_boost_params()
  5. 7 apachesolr_search.module \apachesolr_search_add_boost_params()
1 call to apachesolr_search_add_boost_params()
apachesolr_search_run in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.

File

./apachesolr_search.module, line 1169
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_add_boost_params(DrupalSolrQueryInterface $query) {
  $env_id = $query
    ->solr('getId');
  $params = array();
  $defaults = array(
    'content' => '1.0',
    'ts_comments' => '0.5',
    'tos_content_extra' => '0.1',
    'label' => '5.0',
    'tos_name' => '3.0',
    'taxonomy_names' => '2.0',
    'tags_h1' => '5.0',
    'tags_h2_h3' => '3.0',
    'tags_h4_h5_h6' => '2.0',
    'tags_inline' => '1.0',
    'tags_a' => '0',
  );
  $qf = apachesolr_environment_variable_get($env_id, 'field_bias', $defaults);
  $fields = $query
    ->solr('getFields');
  if ($qf && $fields) {
    foreach ($fields as $field_name => $field) {
      if (!empty($qf[$field_name])) {
        $prefix = substr($field_name, 0, 3);
        if ($field_name == 'content' || $prefix == 'ts_' || $prefix == 'tm_') {

          // Normed fields tend to have a lower score. Multiplying by 40 is
          // a rough attempt to bring the score in line with fields that are
          // not normed.
          $qf[$field_name] *= 40.0;
        }
        $params['qf'][$field_name] = $field_name . '^' . $qf[$field_name];
      }
    }
  }
  $date_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_date_boost', '0:0');
  $comment_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_comment_boost', '0:0');
  $changed_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_changed_boost', '0:0');
  $sticky_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_sticky_boost', '0');
  $promote_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_promote_boost', '0');

  // For the boost functions for the created timestamp, etc we use the
  // standard date-biasing function, as suggested (but steeper) at
  // http://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_boost_the_score_of_newer_documents
  // ms() returns the time difference in ms between now and the date
  // The function is thus: $ab/(ms(NOW,date)*$steepness + $ab).
  list($date_steepness, $date_boost) = explode(':', $date_settings);
  if ($date_boost) {
    $ab = 4 / $date_steepness;
    $params['bf'][] = "recip(ms(NOW,ds_created),3.16e-11,{$ab},{$ab})^{$date_boost}";
  }

  // Boost on comment count.
  list($comment_steepness, $comment_boost) = explode(':', $comment_settings);
  if ($comment_boost) {
    $params['bf'][] = "recip(div(1,max(is_comment_count,1)),{$comment_steepness},10,10)^{$comment_boost}";
  }

  // Boost for a more recent comment or node edit.
  list($changed_steepness, $changed_boost) = explode(':', $changed_settings);
  if ($changed_boost) {
    $ab = 4 / $changed_steepness;
    $params['bf'][] = "recip(ms(NOW,ds_changed),3.16e-11,{$ab},{$ab})^{$changed_boost}";
  }

  // Boost for nodes with sticky bit set.
  if ($sticky_boost) {
    $params['bq'][] = "bs_sticky:true^{$sticky_boost}";
  }

  // Boost for nodes with promoted bit set.
  if ($promote_boost) {
    $params['bq'][] = "bs_promote:true^{$promote_boost}";
  }

  // Modify the weight of results according to the node types.
  $type_boosts = apachesolr_environment_variable_get($env_id, 'apachesolr_search_type_boosts', array());
  if (!empty($type_boosts)) {
    foreach ($type_boosts as $type => $boost) {

      // Only add a param if the boost is != 0 (i.e. > "Normal").
      if ($boost) {
        $params['bq'][] = "bundle:{$type}^{$boost}";
      }
    }
  }
  $query
    ->addParams($params);
}