对mysql中的多个字段进行加权搜索的最佳方法?

Best way to do a weighted search over multiple fields in mysql?(对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中的多个字段进行加权搜索的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)