2010-04-20 54 views
0

我很喜歡全文搜索,並被告知要對2個表格進行全文搜索並按相關性排序結果。包含2個表格的全文搜索

我會在表格的「職位」和表格「PostComments」中查找。我必須在Posts.Description,Posts.Title和PostComments.Comments上查找搜索詞(讓我們說「AnyWord」)。

我必須按相關性返回帖子順序,但因爲我正在查看帖子和帖子評論我不知道這是否有意義。我會說我需要在同一張桌子上的所有信息,以便按相關性排序。

你能幫我弄清楚這是否有意義,如果它確實如何實現它?

編輯

我會試着更好地解釋一點我需要什麼。

如果搜索到的術語出現在標題上,描述中或任何相關的PostComments上,則與搜索相關。

但在前端我會顯示帖子列表。該列表中的帖子標題是該帖子本身的鏈接。帖子評論在那裏可見,但不在搜索結果列表中,儘管它們涉及搜索過程。

所以,你可能會對這只是相匹配的搜索結果職位,因爲搜索項存在於一個或多個註釋

回答

1

只有ContainsTable收益相關的評估。你沒有提到需要返回的內容,所以我只是簡單地返回表值的名稱以及給定表的主鍵(你可以用實際的主鍵列名替換「PrimaryKey」,例如PostId或PostCommentsId) ,價值和等級。

Select Z.TableName, Z.PK, Z.Value, Z.Rank 
From (
     Select 'Posts' As TableName, Posts.PrimaryKey As PK, Posts.Description As Value, CT.Rank 
     From Posts 
      Join ContainsTable(Posts, Description, 'Anyword') As CT 
       On CT.Key = Posts.PrimaryKey 
     Union All 
     Select 'PostComments', PostComments.PrimaryKey, PostComments.Comments, CT.Rank 
     From PostComments 
     Join ContainsTable(PostComments, Comments, 'Anyword') As CT 
       On CT.Key = PostComments.PrimaryKey 
     ) As Z 
Order By Z.Rank Desc 

編輯鑑於更多的信息,這是很清晰的。首先,看起來搜索排名與結果沒有關係。因此,所有必要的是在搜索Post信息和搜索PostComments之間使用OR:

Select ... 
From Posts 
Where Contains(Posts.Description, Posts.Title, 'searchterm') 
    Or Exists (
       Select 1 
       From PostComments 
       Where PostComments.PostId = Posts.Id 
        And Contains(PostComments.Comments, 'searchterm') 
       ) 
+0

@Thomas,感謝您的幫助!也許我不夠清楚,但搜索必須返回POSTS。結果將通過相關性顯示在POSTS順序列表中。 – StackOverflower 2010-04-20 15:21:48

+0

@Timmy O'工具 - 那麼PostComments如何適合等式?帖子和帖子評論如何相關? – Thomas 2010-04-20 15:50:39

+0

@Timmy O'工具 - PostComments中找到的值的排名如何適合問題?如果您能夠展示您期望返回的內容,這將有所幫助。 – Thomas 2010-04-20 15:53:55