使用 PHP 和 Mysql 进行相关性搜索

发布于 2024-11-28 16:25:27 字数 150 浏览 1 评论 0 原文

这样做的最佳方法是什么?

我有列:track_name、artist_name、album_name

我希望所有列都与搜索查询匹配。以及匹配时的一些灵活性。

mysql之类的太严格了,即使是%XXX%。它匹配整个字符串,而不是各个部分。

Whats the best way to go around doing this?

I have columns: track_name, artist_name, album_name

I want all columns to be matched against the search query. and some flexibility while matching.

mysql like is too strict, even with %XXX%. It matches the string as a whole, not the parts.

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

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

发布评论

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

评论(2

腻橙味 2024-12-05 16:25:27

您的 MySQL 查询可能有多个 OR 子句,搜索用户输入的每个空格分隔的单词。例如,用户搜索“Queens of the Stoneage”可能会在 SQL 中表示为 SELECT * FROMongs WHERE Artist_name LIKE "%Queens%" OR Artist_name LIKE "Stoneage"。

然而,这可能是不可取的,因为以 % 开头的 LIKE 搜索效率低下,并且在大型数据库上可能会非常慢。

虽然我无法谈论性能影响,但您应该看看 自然语言全文搜索。这可能是您会发现的最有效的解决方案:

SELECT * FROMongs WHERE MATCH(track_name, Artist_name, album_name) AGAINST('Queens of the Stoneage' IN NATURAL LANGUAGE MODE);

一些 PHP 函数确实存在用于确定文本字符串的相似性,但将这项工作保留在数据库中可能是最有效的(并且不那么令人沮丧):

Your MySQL query could have several OR clauses, searching for each space-delimited word entered by the user. For example, a user search for "Queens of the Stoneage" may be represented in SQL as SELECT * FROM songs WHERE artist_name LIKE "%Queens%" OR artist_name LIKE "Stoneage".

However, that could be undesirable because LIKE searches which start with an % are inefficient and could be terribly slow on a large database.

Though I can't speak to the performance implications, you should have a look at natural language full-text searches. It's probably the most effective solution you'll find:

SELECT * FROM songs WHERE MATCH(track_name, artist_name, album_name) AGAINST('Queens of the Stoneage' IN NATURAL LANGUAGE MODE);

Some PHP functions do exist for determining the similarity of strings of text, but keeping this work in the database will probably be most efficient (and less frustrating):

∝单色的世界 2024-12-05 16:25:27

我认为你需要重新考虑你的申请。我从您的评论中了解到,您需要在程序中实现一些逻辑运算符,例如“and”、“or”和“not”。它不仅仅涉及像全文索引这样的奇特算法或像这个 mysql 匹配查询这样的最长公共子字符串。但我可能是错的。

I think you need to rethink your application. What I understood from your comment is that you need to implement some logic operator like "and", "or" and "not" in your program. It's not only about fancy algorithm like fulltext index or longest common substring like this mysql match query. But I can be wrong.

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