2011-07-06 67 views
12

如果我有以下的全文搜索查詢:SQL Server全文搜索 - 加權某些列在其他

SELECT * 
FROM dbo.Product 
    INNER JOIN CONTAINSTABLE(Product, (Name, Description, ProductType), 'model') ct 
     ON ct.[Key] = Product.ProductID 

是否可以權衡正在搜索的列?

舉例來說,我更關心出現在名稱列比我做的 描述或ProductType列字模型。

當然,如果這個詞是在所有3列,然後我希望它比排名,如果它只是在名稱列更高。如果它只出現在名稱中而只是在描述/產品類型中出現,是否有任何方法可以提高排名?

+0

[SQL Server的加權全文搜索(可能重複http://stackoverflow.com/questions/310425/sql-server-weighted-full-text-搜索) – user7116

回答

9

你可以做類似下面的查詢。在這裏,WeightedRank由個人比賽的等級乘以計算。注意:不幸的是我沒有安裝羅斯文所以我不能對此進行測試,所以看它更像是僞代碼,讓我知道,如果它不能正常工作。

declare @searchTerm varchar(50) = 'model'; 

SELECT 100 * coalesce(ct1.RANK, 0) + 
     10 * coalesce(ct2.RANK, 0) + 
     1 * coalesce(ct3.RANK, 0) as WeightedRank, 
     * 
    FROM dbo.Product 
      LEFT JOIN 
     CONTAINSTABLE(Product, Name, @searchTerm) ct1 ON ct.[Key] = Product.ProductID 
      LEFT JOIN 
     CONTAINSTABLE(Product, Description, @searchTerm) ct2 ON ct.[Key] = Product.ProductID 
      LEFT JOIN 
     CONTAINSTABLE(Product, ProductType, @searchTerm) ct3 ON ct.[Key] = Product.ProductID 
5

Pro Full-Text Search in SQL Server 2008Listing 3-25. Sample Column Rank-Multiplier Search

SELECT * 
FROM (
    SELECT Commentary_ID 
     ,SUM([Rank]) AS Rank 
    FROM (
     SELECT bc.Commentary_ID 
      ,c.[RANK] * 10 AS [Rank] 
     FROM FREETEXTTABLE(dbo.Contributor_Birth_Place, *, N'England') c 
     INNER JOIN dbo.Contributor_Book cb ON c.[KEY] = cb.Contributor_ID 
     INNER JOIN dbo.Book_Commentary bc ON cb.Book_ID = bc.Book_ID 

     UNION ALL 

     SELECT c.[KEY] 
      ,c.[RANK] * 5 
     FROM FREETEXTTABLE(dbo.Commentary, Commentary, N'England') c 

     UNION ALL 

     SELECT ac.[KEY] 
      ,ac.[RANK] 
     FROM FREETEXTTABLE(dbo.Commentary, Article_Content, N'England') ac 
     ) s 
    GROUP BY Commentary_ID 
    ) s1 
INNER JOIN dbo.Commentary c1 ON c1.Commentary_ID = s1.Commentary_ID 
ORDER BY [Rank] DESC;