1

I use Drupal 6 with the ApacheSolr search module.

On my website, all visitors can flag any node with a yes/no answer to the question "Did you find what you were looking for?". Based on those answers, I calculate a custom ranking score for each node which is stored in a separate DB table.

Now I would like to include this score when sorting/boosting the search results in Solr, so that those nodes with the best relevancy / score come out on top.

Can it be accomplished with hook_apachesolr_prepare_query()?

All suggestions are welcome :)

  • If your score changes frequently and you dont want to reindex the node for every score change you can use an external filefield. Take a look at drupal.org/node/1586320 – Mike Jun 29 '12 at 22:42
2

To provide the score as a sort option add it in the prepare_query hook:

function mymodule_apachesolr_prepare_query(&$query, &$params, $caller) {
  $query->set_available_sort('field_score', array('title' => t('Score'), 'default' => 'desc'));
}

To boost by it you'll need to either:

  • Go to Site settings -> Apachesolr -> Search fields and if the score field appears there give it a value bigger than all the other fields to make it the most relevant.

  • If the field doesn't appear (due to the type it might not appear) or you want more control (like calculate the boost value on every query) you can add the boost in the modify_query hook with something like this: $params['qf'][] = 'field_score^10';.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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