SQL包含字符串中的表和关键字位置

时间:2011-05-10 09:27:05

标签: sql relevance full-text-search

对全文搜索有疑问。

我有一个有效的查询,但我想改进它。例如,假设我正在搜索“最佳解决方案”。在我的结果中(使用FTS,CONTAINSTABLE,NEAR,ISABOUT,*)我有列 rank,id,string

rank| id | string
----+----+-------
430 | 33 | my own best solution
430 | 32 | the way for best solution
430 | 30 | the best solution sample
430 | 31 | best solution 
430 | 34 | best solution creation how
300 | 40 | usefull best software solution
300 | 41 | best software solution
200 | 50 | wolrds bests solutions
200 | 51 | bests solutions of the world

因此,此查询100%适合我,所有RANK都是正确的,但我想让此查​​询更具相关性。

如果关键字在字符串中的位置在左侧更多,则它应该出现在结果的前面。例如:

rank| id | string
----+----+-------
430 | 31 | best solution 
430 | 34 | best solution creation how
430 | 30 | the best solution sample
430 | 33 | my own best solution
430 | 32 | the way for best solution
300 | 41 | best software solution
300 | 40 | usefull best software solution
200 | 51 | bests solutions of the world
200 | 50 | wolrds bests solutions

这可能吗?如果是这样,我怎么能得到这个结果?

2 个答案:

答案 0 :(得分:2)

您需要indexOf功能。在sql手册中搜索类似的功能。

然后添加一个类似ORDER BY rank, CASE indexOf(string, 'best') WHEN -1 THEN 100000 ELSE indexOf(string, 'best') END的排序条件。

如果找不到执行某种indexOf的函数,请使用数据库管理器的CREATE FUNCTION功能自行编写。

答案 1 :(得分:1)

你想要的第三方解决方案是Sphinx(还有其他人)。在这里阅读更多信息:

http://www.ioncannon.net/programming/685/full-text-search-with-sphinx/