Implementing "manual priority" for search results

I’m trying to implement a search where in addition to the search fields, I have a flag in my contents “prioritize” and if it’s set to true, that result should score higher than an equivalent where the flag is false.

I tried with a boolean search like

“query” => {
“bool” => {
“must” => [
{
“match” => {<list of search fields> => <search text> },
},
],
“should” => [
{
“equals” => {
“prioritize” => 1,
},
}
],

but this query fails, like so:

{“took”:1,“timed_out”:false,“hits”:{“total”:0,“total_relation”:“eq”,“hits”:[]}}

what am I not understanding? Is the should clause not right for this purpose?

Here’re two examples in SQL:


mysql> drop table if exists t; create table t(f text, prio bool); insert into t values(1, 'abc', 1),(2, 'abc', 0); select *, weight() + prio c, prio from t where match('abc') order by c desc;
--------------
drop table if exists t
--------------

Query OK, 0 rows affected (0.005 sec)

--------------
create table t(f text, prio bool)
--------------

Query OK, 0 rows affected (0.004 sec)

--------------
insert into t values(1, 'abc', 1),(2, 'abc', 0)
--------------

Query OK, 2 rows affected (0.001 sec)

--------------
select *, weight() + prio c, prio from t where match('abc') order by c desc
--------------

+------+------+------+------+
| id   | f    | c    | prio |
+------+------+------+------+
|    1 | abc  | 1357 |    1 |
|    2 | abc  | 1356 |    0 |
+------+------+------+------+
2 rows in set (0.001 sec)
--- 2 out of 2 results in 0ms ---

Using a custom ranker:

select *, weight() from t where match('abc') option ranker=expr('sum(bm25) + prio')
--------------

+------+------+------+----------+
| id   | f    | prio | weight() |
+------+------+------+----------+
|    1 | abc  |    1 |      357 |
|    2 | abc  |    0 |      356 |
+------+------+------+----------+
2 rows in set (0.000 sec)
--- 2 out of 2 results in 0ms ---

You should be able to do the same via the JSON interface using: