2

关于如何控制 Bloodhound 和 Twitter Typeahead 对显示在 typeahead 中的结果进行排序的方式,我遇到了一些问题。

我正在使用预输入来显示建议的地址和城市,这意味着有人可以搜索“纽约”或“曼哈顿纽约”或“纽约曼哈顿第 55 大道”。

当用户搜索“纽约”时,我希望 Typeahead 将“纽约”作为最接近匹配的第一个建议。目前如果用户搜索“纽约”,第一个建议是“纽约曼哈顿55h大道”、“纽约曼哈顿22街”等。

任何人都可以指出我如何实现这种排序的正确方向?

4

1 回答 1

0

I probably missunderstood what Bloodhound was for. I thought Bloodhound was used to sort the results in a clever way, and that there were some kind of way you could select sorting algoritm through Bloodhound. I guess I was wrong.

The solution is to sort the whole thing before it fetches it. In this case during the SQL Query of the database. I ordered in the following way to get what I wanted:

SELECT address, city FROM house WHERE city LIKE :query OR address LIKE :query GROUP BY city ORDER BY CASE 
                                    WHEN city LIKE :query1 THEN 0
                                    WHEN city LIKE :query2 THEN 1
                                    WHEN city LIKE :query3 THEN 2
                                    ELSE 3
                                    END, city

Where query1, query2 and query3 were variations of the parameter. For example "cityName%", "% %cityName% %", "% cityName".

于 2014-03-03T17:18:28.907 回答