mysql 和 php FULLTEXT 搜索做得更多,但做得不是更少?

发布于 2024-11-17 02:22:41 字数 1293 浏览 0 评论 0原文

关于这个问题列出: MySQL 问题 - 服务器完全溢出!请指教 是我的所有服务器详细信息,因此我不必在这里重新编写它们。

所以我有一个与 mysql 数据库交互的 PHP 脚本。 2天前,由于用户的许多请求,我已从经典的“select * from table where column like 'blabla' and columns like 'lablab'”切换到全文搜索。 已编入索引、经过测试,一切顺利。 上班2天,已经收到一包投诉。

搜索确实更好,也就是说,如果您使用完整的单词...:(

尝试搜索“apple”,您会发现几乎相同的结果。尝试搜索

“applecomputer at lowprices”,全文搜索远远高于任何类似的搜索搜索

尝试搜索“ple”,您可以使用经典方法获得完整的结果列表...但是通过全文搜索,您面前会出现一个大而厚的“未找到”文本...

抱歉让它听起来如此诗意到目前为止...:|

有什么解决方法吗为此,让全文使用一些奇怪的通配符?比如 %foo% 应该会给我带来 ffoork、flfooor 等...

对于那些问的人,是的,除了语法、简单表达式之外,我真的不知道如何使用 mysql。和基本功能。

请帮助我提供代码片段或链接到解决此问题的某种方法... 如果可能的话,我宁愿不使用一些第 3 方 mysql 搜索引擎来使我的存在变得复杂,但作为最后的手段......

提前非常感谢! 干杯!

抱歉,我忘了把我的代码贴出来:

$query="SELECT *, 
    CASE when title like '%".$s."%' then 1 else 0 END as titlematch, 
    CASE when tags like '%".$s."%' then 1 else 0 END as tagsmatch,
    MATCH (title, tags) AGAINST ('".$s."') AS relevance 
    FROM videos  
    WHERE MATCH(title, tags) AGAINST ('".$s."' IN BOOLEAN MODE) 
    HAVING relevance > 0  
    ORDER by titlematch desc, tagsmatch desc, relevance desc, `datetime` desc, `views` desc
    ";

Listed on this question:
MySQL problem - COMPLETE server overrun! Please advise
are all my server details, so I don't have to re-write them here.

So I'm having a PHP script which interacts with a mysql DB.
2 days ago, due to many requests from my users, I have switched from clasic "select * from table where column like 'blabla' and column like 'lablab'" to the fulltext search.
Indexed, tested, good to go.
2 days on the job, already received a bag of complains.

Searches are indeed better, that is if you use full words... :(

try searching for "apple" and you find almost the same results.

try searching for "apple computer at low prices" and the fulltext search is WAY above any LIKE search

try searching for "ple" and you get a full list of results with the classic method... but a big fat "Nothing found" text laying in front of you with the fulltext search...

Sorry for making it sound so poetic up to this point... :|

Is there any workaround for this? making the fulltext use some freakin' wildcards? like %foo% should bring me ffoork, flfooor etc...

And for those of you asking, yeah I don't really know my way around mysql further than syntax, simple expressions and basic functions.

Please help me with a code snippet or link to some method of working this out...
If possible, i would rather not complicate my existence with some 3rd party mysql search engine but as a last resort...

Thank you very much in advance!
Cheers!

Sorry, I forgot to put my code up:

$query="SELECT *, 
    CASE when title like '%".$s."%' then 1 else 0 END as titlematch, 
    CASE when tags like '%".$s."%' then 1 else 0 END as tagsmatch,
    MATCH (title, tags) AGAINST ('".$s."') AS relevance 
    FROM videos  
    WHERE MATCH(title, tags) AGAINST ('".$s."' IN BOOLEAN MODE) 
    HAVING relevance > 0  
    ORDER by titlematch desc, tagsmatch desc, relevance desc, `datetime` desc, `views` desc
    ";

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

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

发布评论

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

评论(3

看轻我的陪伴 2024-11-24 02:22:41

为什么不在 PHP 搜索功能中使用条件,如果搜索了单个单词或几个字符,则使用良好的 ol' LIKE 搜索?

Why don't you just use a condition in your PHP search function, and if a single word or few characters were searched, use the good ol' LIKE search?

云柯 2024-11-24 02:22:41

如果您已经在使用布尔模式,则可以更新 PHP 代码以自动在 AGAINST(...) 内的搜索字符串周围添加通配符。这应该可以解决您的单字搜索问题。

您还可以在多词搜索的情况下在每个单词周围添加通配符,但我建议使用真实数据(例如,常用搜索的关键字,如果您记录它们)进行一些测试,以确保当前结果相关性不受到影响。


更新:尝试以下操作:

SELECT
    *, 
    CASE when title like '%".$s."%' then 1 else 0 END as titlematch, 
    CASE when tags like '%".$s."%' then 1 else 0 END as tagsmatch,
    MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE) AS relevance

FROM videos

WHERE
    title like '%".$s."%' OR
    tags like '%".$s."%' OR
    MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE)

ORDER by
    titlematch desc, tagsmatch desc, relevance desc

If you are already using boolean mode, you can update your PHP code to automatically add wildcards around the search string inside of AGAINST(...). This should solve your problem for single-word searches.

You could also add wildcards around each single word in case of multi-word searches, but I'd recommend doing some testing with real data (e.g. commonly searched keywords, if you log them) in order to make sure that current result relevancy is not impacted.


Update: try the following:

SELECT
    *, 
    CASE when title like '%".$s."%' then 1 else 0 END as titlematch, 
    CASE when tags like '%".$s."%' then 1 else 0 END as tagsmatch,
    MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE) AS relevance

FROM videos

WHERE
    title like '%".$s."%' OR
    tags like '%".$s."%' OR
    MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE)

ORDER by
    titlematch desc, tagsmatch desc, relevance desc
半葬歌 2024-11-24 02:22:41

查看布尔全文搜索。它们允许使用通配符和其他运算符来执行复杂的全文搜索。

MySQL 布尔全文搜索

如果这不起作用如果全文搜索返回 0 个结果,替代解决方案可能是执行类似搜索。

Have a look into BOOLEAN fulltext searches. They allow the use of wildcards and other operators to perform complex fulltext searches.

MySQL Boolean Fulltext Searches

If this doesn't work for you, an alternate solution could be to perform a like search if the fulltext search returns 0 results.

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