[英]Sphinx - Sort results in a specific order
In Sphinx
, I want to search for rows containing some keywords and sort them in a specific order. 在
Sphinx
,我想搜索包含一些关键字的行并按特定顺序对其进行排序。
For example, search for words like 'Dog', 'Cat', 'Lion' and sort in an order such that rows with 'Dog' will be listed first, followed by rows with 'Cat', and then with 'Lion'. 例如,搜索“ Dog”,“ Cat”,“ Lion”之类的单词并按顺序排序,这样将首先列出带有“ Dog”的行,然后是带有“ Cat”的行,然后是“ Lion”。
In Sphinx
, searching will be easy with below query: 在
Sphinx
,使用以下查询将很容易进行搜索:
SELECT * FROM test_index WHERE MATCH('test_string "DOG" | "CAT" | "Lion"')
How can we sort the rows in a predefined order in Sphinx
? 如何在
Sphinx
中以预定义的顺序对行进行排序?
You can order with predefined list in SQL
. 您可以使用
SQL
预定义列表进行排序。
Complete SQL: 完整的SQL:
SELECT *
FROM test_index
WHERE MATCH('test_string "DOG" | "CAT" | "Lion"')
ORDER BY CASE
WHEN field = 'DOG' THEN 0
WHEN field = 'CAT' THEN 1
WHEN field = 'Lion' THEN 2
end
Yes Sphinx doesn't support ORDER BY CASE
. 是的,Sphinx不支持
ORDER BY CASE
。 If you want to short in predefined order you can use SPH_SORT_EXTENDED
. 如果
SPH_SORT_EXTENDED
预定义的顺序做空,可以使用SPH_SORT_EXTENDED
。 Example in PHP
is following: PHP
示例如下:
$sph->SetSortMode(SPH_SORT_EXTENDED, "DOG, CAT, Lion");
Example: 例:
Article.search "term", :sort_mode => :extended,
:order => "Project ASC, Company ASC"
YOu can do something like 你可以做类似的事情
WHERE MATCH('test_string DOG | DOG | DOG | CAT | CAT | Lion')
Which means certain words will match multiple times. 这意味着某些单词会多次匹配。 MIgh need to combine with a different ranking moder eg
OPTION ranker=wordcount
可能需要与其他排名主持人结合使用,例如
OPTION ranker=wordcount
Boost operator might also do something similar... Boost运算符也可能会执行类似的操作...
WHERE MATCH('test_string DOG^3 | CAT^2 | Lion')
(removed the quotes for brevity) (为简洁起见,删除了报价)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.