Best way to do a weighted search over multiple fields in mysql?(对mysql中的多个字段进行加权搜索的最佳方法?)
问题描述
这是我想要做的:
- 将搜索主题与表格的多个字段进行匹配
- 按字段的重要性和匹配的相关性(按该顺序)对结果进行排序
例如:假设我有一个博客.然后有人搜索php".结果会这样显示:
Ex: let's assume I have a blog. Then someone searches for "php". The results would appear that way:
- 首先,匹配字段title",按相关性排序
- 然后,字段body"的匹配项也按相关性排序
- 等等与指定的字段...
我实际上是用 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中的多个字段进行加权搜索的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对mysql中的多个字段进行加权搜索的最佳方法?


- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01