简体   繁体   English

如何使用PHP和MySQL获取相关文章

[英]How to get related posts using PHP and MySQL

What is the best way to get related posts using PHP and MySQL? 使用PHP和MySQL获取相关文章的最佳方法是什么? The second question is how would I get the top 5 related posts from by comparing tags and categories from each post. 第二个问题是,我如何通过比较每个帖子的标签和类别来获得前5个相关的帖子。 My MySql tables are listed below. 下面列出了MySql表。

CREATE TABLE categories ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
category VARCHAR(255) NOT NULL, 
url VARCHAR(255) NOT NULL,
PRIMARY KEY (id), 
INDEX parent (parent_id),
UNIQUE KEY(parent_id, url)
);

CREATE TABLE posts_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL,
users_posts_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE users_posts (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title TEXT NOT NULL,
posts_content LONGTEXT NOT NULL,
PRIMARY KEY (id)
);

Post relevance is a big area of research with no nice & smart solution. 职位相关性是一个很大的研究领域,没有一个好的解决方案。 You may assign each post +0.1 point for tag match, +0.4 for category match. 您可以为每个帖子分配+0.1点用于标记匹配,为+0.4分配类别匹配。 Later you may consider post content too. 以后您也可以考虑发布内容。 Then you may sort by this value. 然后,您可以按此值排序。

This is not something you can easily do in 1 sql query. 这不是您可以在1 sql查询中轻松完成的操作。

SQL is for data retrieval, and is useful for retrieving data based on objective criteria, where there is a right or wrong answer. SQL用于数据检索,对于根据客观标准(答案正确或错误)检索数据很有用。 There is no objective measure of what makes a post a "related post", so it's not something that you can effectively do with SQL alone. 没有什么措施可以使帖子成为“相关帖子”,因此,单独使用SQL并不能有效地做到这一点。

Document clustering , which means grouping related documents, is a large and active research area, so that's a good place to start, but implementing something yourself will be very difficult. 文档聚类 ,即对相关文档进行分组,是一个庞大而活跃的研究领域,因此这是一个不错的起点,但是您自己实现某些功能将非常困难。 Depending on the language you're using, you might look at clustering libraries. 根据您使用的语言,您可能会查看集群库。 For example, if you're using Java (or anything that runs on the JVM, or you can set up a web service to do the clustering), you could look at using Weka . 例如,如果您正在使用Java(或在JVM上运行的任何东西,或者您可以设置Web服务来进行集群),那么可以考虑使用Weka

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM