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

发布于 2024-11-06 07:22:16 字数 1025 浏览 0 评论 0原文

有一个关于全文搜索的问题。

我有一个有效的查询,但我想改进它。例如,假设我正在寻找“最佳解决方案”。在我的结果中(使用 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 code>s 是正确的,但我想让这个查询更相关。

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

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

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

Have a question about full text search.

I have a query that works, but I want to improve it. For example, suppose I'm searching for "best solution". In my result (using FTS,CONTAINSTABLE,NEAR,ISABOUT,*) I have the columns 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

So this query is 100% right for me, and all RANKs are correct, but I want to make this query more relevant.

If a keyword's position in a string is more to the left, it should appear earlier in the result. For example:

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

Is this possible? If so, how can I get this result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-11-13 07:22:16

您需要一个 indexOf 函数。在 sql 手册中搜索类似的函数。

然后添加一个排序条件,如下所示:ORDER BYrank, CASE indexOf(string, 'best') WHEN -1 THEN 100000 ELSE indexOf(string, 'best') END

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

You need an indexOf function. Search your sql manual for a similar function.

Then add a sorting criteria looking like ORDER BY rank, CASE indexOf(string, 'best') WHEN -1 THEN 100000 ELSE indexOf(string, 'best') END.

If you can't find a function doing a kind of indexOf, write it yourself using the CREATE FUNCTION feature of your database manager.

云归处 2024-11-13 07:22:16

您想要的第三方解决方案是 Sphinx(还有其他解决方案)。阅读更多相关信息:

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

The third party solution you want is Sphinx (there are still others). Read more about here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文