【问题标题】:Search algorithms or tool for searching from database用于从数据库中搜索的搜索算法或工具
【发布时间】:2019-11-16 01:05:11
【问题描述】:

我有这个数据库表:

Column       Type

source       text            
news_id      int(12)             
heading      text            
body         text        
source_url   tinytext
time         timestamp
news_pic     char(100) 
location     char(128)
tags         text
time_created timestamp
hits         int(10)

现在我正在寻找一种算法或工具来在这个包含新闻数据的表中搜索关键字。关键字应在标题、正文、标签和新闻点击次数中进行搜索,以获得最佳结果。

【问题讨论】:

  • 全网都有php脚本来建立与数据库的连接,请求它并获取结果......如果是mysql数据库,像phpmyadmin这样的工具就可以完成这项工作......

标签: php mysql search search-engine


【解决方案1】:

MySQL 已经内置了您需要的工具:全文搜索。我将假设您知道如何使用 PHP 与 MySQL 交互。如果没有,请先查看。反正...

1) 为要搜索的字段添加全文索引:

alter table TABLE_NAME add fulltext(heading);
alter table TABLE_NAME add fulltext(body);
alter table TABLE_NAME add fulltext(tags);

2) 使用match ... against 语句执行全文搜索:

select * from TABLE_NAME where match(heading, body, tags, hits) against ('SEARCH_STRING');

显然,在这些示例中,将您的表名称替换为 TABLE_NAME,并将搜索字符串替换为 SEARCH_STRING。

我不明白您为什么要搜索命中数,因为它只是一个整数。但是,您可以通过在查询中添加order 子句来按点击次数排序

select * from TABLE_NAME where match(heading, body, tags, hits) against ('SEARCH_STRING') order by hits desc;

【讨论】:

  • 我需要一个像“Slope One”这样的算法,用于向用户推荐一些东西。在我的搜索结果中,我想优先考虑一些字段,如标题、标签和点击次数那个消息有,这不能使用简单的 MySQL 查询来完成
猜你喜欢
  • 2020-07-09
  • 1970-01-01
  • 2013-07-21
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
相关资源
最近更新 更多