[英]Relevancy search with PHP and Mysql
Whats the best way to go around doing this? go 这样做的最佳方法是什么?
I have columns: track_name, artist_name, album_name我有列: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%. mysql 之类的太严格了,即使是 %XXX%。 It matches the string as a whole, not the parts.
它匹配整个字符串,而不是部分。
Your MySQL query could have several OR
clauses, searching for each space-delimited word entered by the user.您的 MySQL 查询可能有多个
OR
子句,用于搜索用户输入的每个以空格分隔的单词。 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"
.例如,用户搜索“Queens of the Stoneage”可以在 SQL 中表示为
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.但是,这可能是不可取的,因为以
%
开头的LIKE
搜索效率低下,并且在大型数据库上可能非常慢。
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):一些 PHP 函数确实存在用于确定文本字符串的相似性,但将这项工作保存在数据库中可能是最有效的(并且不那么令人沮丧):
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.
我从您的评论中了解到,您需要在程序中实现一些逻辑运算符,例如“and”、“or”和“not”。 It's not only about fancy algorithm like fulltext index or longest common substring like this mysql match query.
这不仅仅是关于像全文索引这样的花哨算法或最长常见的 substring 这样的 mysql 匹配查询。 But I can be wrong.
但我可能是错的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.