Hello.
The documentation KNN vector search specifies: “Documents are always sorted by their distance to the search vector. Any additional sorting criteria you specify will be applied after this primary sort condition.”
Why is there such a limitation? Why can’t I control the sorting as usual?
For example, I want the priority of records to always be taken into account, regardless of the relevance of the search. Let’s say we are talking about displaying products in an online store: I want the products that are in stock to always be displayed at the top of the list and the products that are out of stock to be displayed only after them.
For each record, I have a sort_order field that indicates the priority. In this case:
sort_order = 100 - products in stock;
sort_order = 300 - products that are not in the store and cannot be purchased.
As a result, the buyer sees the product they are looking for, but some of the products that cannot be ordered are displayed on the first screen, which is unnecessary information for them.
SELECT product_id, name, sort_order, WEIGHT(), knn_dist() FROM product_index_rt_64453318 WHERE knn ( index_vector, 20, (...) ) ORDER BY sort_order ASC, WEIGHT() DESC LIMIT 0, 20;
+------------+--------------------------------------+------------+----------+------------+
| product_id | name | sort_order | weight() | knn_dist() |
+------------+--------------------------------------+------------+----------+------------+
| 1860046 | Television LG 75QNED86A6A | 300 | 1 | 0.360425 |
| 1860052 | Television LG 75QNED80A6A | 100 | 1 | 0.36158258 |
| 1878233 | Television OzoneHD 19HN82T2 | 300 | 1 | 0.37646562 |
| 1882064 | Television 75" LG NanoCell 4K 60Hz | 100 | 1 | 0.37682074 |
| 1847746 | Television Samsung QE75QN80FAUXUA | 100 | 1 | 0.38049912 |
| 1879042 | Television 50" LG QNED 4K 120Hz Smart| 100 | 1 | 0.38467413 |
I also cannot use knn_dist() in ORDER BY
This causes to an error. Perhaps this would solve the sorting problem in my case, if it were possible.
ORDER BY sort_order ASC, knn_dist() ASC, WEIGHT() DESC LIMIT 0, 20;
ERROR 1064 (42000): P01: syntax error, unexpected '(', expecting $end near '() ASC, WEIGHT() DESC LIMIT 0, 20'