简体   繁体   English

喜欢使用MS SQL Server的“类似帖子”吗?

[英]“Similar Posts” like functionality using MS SQL Server?

I have lots of article store in MS SQL server 2005 database in a table called Articles- 我在MS SQL Server 2005数据库中有很多文章存储在名为Articles-的表中

"Articles (ArticleID, ArticleTitle, ArticleContent)"

Now I want some SP or SQL query which could return me similar Article against any user's input (very much like "Similar Posts" in blogs OR "Related Questions" in stackoverflow). 现在,我需要一些SP或SQL查询,这些查询可以针对任何用户的输入向我返回类似的文章(非常类似于博客中的“类似帖子”或stackoverflow中的“相关问题”)。 The matching should work on both ArticleTitle and ArticleContent. 匹配应同时适用于ArticleTitle和ArticleContent。 The query should be intelligent enough to sort the result on the basis on their relevancy. 查询应该足够智能,可以根据它们的相关性对结果进行排序。

Is it possible to do this in MS SQL Server 2005? 在MS SQL Server 2005中可以做到这一点吗?

Something like this might work, a kind of ranking system. 这样的事情可能会起作用,这是一种排名系统。 You would probably have to split the string in your application to build a SQL string, but I have used similar to build an effective site search. 您可能必须在应用程序中拆分字符串才能构建SQL字符串,但是我使用了类似的方法来构建有效的站点搜索。

Select
Top 10
ArticleID,
ArticleTitle,
ArticleContent
From
Articles
Order By
(Case When ArticleTitle = 'Article Title' Then 1 Else 0 End) Desc,
(Case When ArticleTitle = 'Article' Then 1 Else 0 End) Desc,
(Case When ArticleTitle = 'Title' Then 1 Else 0 End) Desc,
(Case When Soundex('Article Title') = Soundex(ArticleTitle) Then 1 Else 0 End) Desc,
(Case When Soundex('Article') = Soundex(ArticleTitle) Then 1 Else 0 End) Desc,
(Case When Soundex('Title') = Soundex(ArticleTitle) Then 1 Else 0 End) Desc,
(Case When PatIndex('%Article%Title%', ArticleTitle) > 0 Then 1 Else 0 End) Desc,
(Case When PatIndex('%Article%', ArticleTitle) > 0 Then 1 Else 0 End) Desc,
(Case When PatIndex('%Title%', ArticleTitle) > 0 Then 1 Else 0 End) Desc,
(Case When PatIndex('%Article%Title%', ArticleContent) > 0 Then 1 Else 0 End) Desc,
(Case When PatIndex('%Article%', ArticleContent) > 0 Then 1 Else 0 End) Desc,
(Case When PatIndex('%Title%', ArticleContent) > 0 Then 1 Else 0 End) Desc

You can then add/remove case statements from the order by clause to improve the list based on your data. 然后,您可以从order by子句中添加/删除案例语句,以根据数据改进列表。

First of all you need to define what article similarity means. 首先,您需要定义文章相似性的含义。
For example you can associate some meta information with articles, like tags. 例如,您可以将一些元信息与文章(如标签)相关联。
To be able to find similar articles you need to extract some features from them, for example you can build full text index. 为了能够找到相似的文章,您需要从中提取一些功能,例如,您可以建立全文索引。

You can take advantage of full text search capability of MSSQL 2005 您可以利用MSSQL 2005的全文本搜索功能

-- Assuming @Title contains title of current articles you can find related articles runnig this query  
SELECT * FROM Acticles WHERE CONTAINS(ArticleTitle, @Title)

I think the question is what 'similar' means to you. 我认为问题是“相似”对您意味着什么。 If you create a field for user to input some kind of tags, it becomes much more easier to query. 如果创建一个供用户输入某种标签的字段,则查询变得更加容易。

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

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