简体   繁体   English

在长词中查找多个词并按相关性对结果进行排序

[英]Find multiple words in a long word and sort the results by relevance

I am doing queries to a database that stores one, very long, word in each of the records. 我正在查询在每个记录中存储一个很长单词的数据库。 The are no spaces, no special characters, only ualpha. 没有空格,没有特殊字符,只有ualpha。 Right now I am using the LIKE keyword to look for records that contain all of the words from my input. 现在,我正在使用LIKE关键字来查找包含输入中所有单词的记录。 I would like the script to show me results with the highest relevance first. 我希望脚本首先显示相关性最高的结果。

So for example, I type in "be me if". 因此,例如,我输入“如果我愿意”。 This script should first return records that contain all of those words, like this: 此脚本应首先返回包含所有这些单词的记录,如下所示:

"XSF BE NSU ME POPKL IF " “ XSF BE NSU ME POPKL IF

Then it would show those that contain only two of those words and then finally those that contain only one of those words. 然后它将显示仅包含这些单词中的两个单词的单词,然后显示仅包含那些单词中的一个单词的单词。

I tried using "ORDER BY CASE" and giving points to the best matches, like this: 我尝试使用“ ORDER BY CASE”并给出最佳匹配点,如下所示:

 - ORDER BY CASE
 - WHEN `code` LIKE '%word1%' AND `code` LIKE '%word2%' THEN 1
 - WHEN `code` LIKE '%word1%' THEN 2
 - ELSE 3 END;

It worked but when given more than three words the server crashed because it had to browse through almost 4 million records and simply couldn't handle it. 它可以正常工作,但是当给出三个以上的单词时,服务器崩溃了,因为它必须浏览近400万条记录并且根本无法处理。

I tried fulltext search after that but it doesn't allow me to search for partial words, only prefixes. 之后,我尝试了全文搜索,但不允许我搜索部分单词,只能搜索前缀。

Is there any way to achieve what I am looking for? 有什么方法可以实现我想要的?

I would suggest adding the values. 我建议添加值。 MySQL treats booleans as integers, so you can do: MySQL将布尔值视为整数,因此您可以执行以下操作:

order by ((code like '%word1%') +
          (code like '%word2%') +
          (code like '%word3%')
         ) desc

EDIT: 编辑:

Of course, this should be after a where clause: 当然,这应该在where子句之后:

where code like '%word1%' or code like '%word2%' or code like '%word3%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM