I am using the solr backend for search_api on a drupal 8 site.

I have added the value of a taxonomy term reference field (field_promoted_search) to the index.

If an item of content has field_promoted_search with value matching the search term, I would like it to get a significant boost (eg ^1000), pushing it to the top of the search results.

Adding a high 'boost' value at admin/config/search/search-api/index/content/fields worked as expected when the server was using solr <6, but since moving to 6.1.0, this does not work as expected. This may be because search_api_solr/solarium is using a multiplicative boost rather than additive. This is not seem as useful to me as this multiplies the relevance achieved by other fields, rather than solely privileging field_promoted_search, though I am uncertain of this aspect.

I have tried adding a boost query for this promoted search field to the solarium query using hook_search_api_solr_query_alter. This requires me to pass each search key as a boosted query, and the effect seem to be affected by the order of the keys (and also my result does not end up top).

$keys = $query->getKeys();
// Boost results where search term matches promoted search field.
$boost_keys = [];
$boost_value = 1000;
foreach ($keys as $key) {
  $boost_keys[] = '"' . $key . '"^' . $boost_value;
$solarium_query->getEDisMax()->setBoostQuery('ss_promoted_search:' . implode(' OR ', $boost_keys));

When I attempt to use a boost function instead, I get an error from back from Solr and the query does not run (I have tried several functions).

// Format is: "funcA(arg1,arg2)^1.2 funcB(arg3,arg4)^2.2"

Any advice much appreciated.

My reference for additive vs multiplicative boost methods is this very helpful article.

  • Related: "Because of multiplying inside recursion, HTML Filter's tag boost can produce astronomical boost values, which besides being nonsensical, breaks the Solr boost_term in Search API Solr by exceeding PHP_FLOAT_MAX" drupal.org/project/search_api/issues/3209167
    – mona lisa
    Aug 12, 2022 at 12:59


Your Answer

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

Browse other questions tagged or ask your own question.