“Similar Posts” like functionality using MS SQL Server?

前端 未结 3 1500
长情又很酷
长情又很酷 2020-12-16 08:25

I have lots of article store in MS SQL server 2005 database in a table called Articles-

\"Articles (ArticleID, ArticleTitle, ArticleContent)\"
相关标签:
3条回答
  • 2020-12-16 08:49

    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

    -- Assuming @Title contains title of current articles you can find related articles runnig this query  
    SELECT * FROM Acticles WHERE CONTAINS(ArticleTitle, @Title)
    
    0 讨论(0)
  • 2020-12-16 08:51

    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.

    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.

    0 讨论(0)
  • 2020-12-16 08:54

    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.

    0 讨论(0)
提交回复
热议问题