这是我想要做的:
例如:假设我有一个博客.然后有人搜索php".结果会这样显示:
Ex: let's assume I have a blog. Then someone searches for "php". The results would appear that way:
我实际上是用 PHP 中的一个类完成的,但它使用了很多联合(很多!)并且随着搜索主题的大小而增长.所以我担心性能和 DOS 问题.有人知道这个吗?
I actually did this with a class in PHP but it uses a lot of UNIONS (a lot!) and grows with the size of the search subject. So I'm worried about performance and DOS issues. Does anybody has a clue on this?
可能这种加权搜索/结果的方法适合您:
Probably this approach of doing a weighted search / results is suitable for you:
SELECT *,
IF(
`name` LIKE "searchterm%", 20,
IF(`name` LIKE "%searchterm%", 10, 0)
)
+ IF(`description` LIKE "%searchterm%", 5, 0)
+ IF(`url` LIKE "%searchterm%", 1, 0)
AS `weight`
FROM `myTable`
WHERE (
`name` LIKE "%searchterm%"
OR `description` LIKE "%searchterm%"
OR `url` LIKE "%searchterm%"
)
ORDER BY `weight` DESC
LIMIT 20
它使用一个选择子查询来提供排序结果的权重.在这种情况下搜索了三个字段,您可以指定每个字段的权重.它可能比联合更便宜,并且可能是纯 MySQL 中更快的方法之一.
It uses a select subquery to provide the weight for ordering the results. In this case three fields searched over, you can specify a weight per field. It's probably less expensive than unions and probably one of the faster ways in plain MySQL only.
如果您有更多的数据并需要更快地获得结果,您可以考虑使用 Sphinx 或 Lucene 之类的东西.
If you've got more data and need results faster, you can consider using something like Sphinx or Lucene.
这篇关于对mysql中的多个字段进行加权搜索的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 SELECT(MYSQL/PHP) 中加入 2 个表Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 个表)
如何使<option selected=“selected">由How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 设置?)
使用 PHP 中的数组自动填充选择框Auto populate a select box using an array in PHP(使用 PHP 中的数组自动填充选择框)
PHP SQL SELECT where like search item with multiple wordsPHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(),名称 ASCMySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名称 ASC)