SearchQuery::addScore() doesn't able to use expression value

Created on 9 November 2021, almost 4 years ago
Updated 15 August 2025, about 12 hours ago

Problem/Motivation

\Drupal\search\SearchQuery query extender, used for building Search queries, doesn't allow using expression values with its ::addScore() method.

Steps to reproduce

  1. Build query for doing search and add SearchQuery extender.
  2. Add expression to query.
  3. Try to use value of that expression with ::addScore()
  4. You will see exception:
Drupal\Core\Database\DatabaseExceptionWrapper : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sum' in 'field list': SELECT "i"."type" AS "type", "i"."sid" AS "sid", 2+2 AS "sum", SUM(sum) AS "calculated_score"
FROM
"test81709592search_index" "i"
INNER JOIN "test81709592search_total" "t" ON "i"."word" = "t"."word"
INNER JOIN "test81709592search_dataset" "d" ON "i"."sid" = "d"."sid" AND "i"."type" = "d"."type" AND "i"."langcode" = "d"."langcode"
WHERE (("i"."word" = :db_condition_placeholder_0) OR ("i"."word" = :db_condition_placeholder_1)) AND ("i"."type" = :db_condition_placeholder_2) AND (("d"."data" LIKE :db_condition_placeholder_3 ESCAPE '\\') AND ("d"."data" LIKE :db_condition_placeholder_4 ESCAPE '\\'))
GROUP BY i.type, i.sid
HAVING (COUNT(*) >= :matches)
ORDER BY "calculated_score" DESC; Array
(
    [:db_condition_placeholder_0] => foo
    [:db_condition_placeholder_1] => bar
    [:db_condition_placeholder_2] => test
    [:db_condition_placeholder_3] => % foo %
    [:db_condition_placeholder_4] => % bar %
    [:matches] => 2
)

Proposed resolution

Allow to use expression values for search result scoring.

Remaining tasks

  1. Fix the problem.
🐛 Bug report
Status

Closed: works as designed

Version

11.0 🔥

Component

search.module

Created by

🇷🇺Russia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024