sql MS SQL全文搜索查询示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL全文搜索查询示例相关的知识,希望对你有一定的参考价值。

-- CONTAINS
SELECT Comments 
    FROM Production.ProductReview 
    WHERE CONTAINS(Comments, 'adventure') ;

-- CONTAINSTABLE  (Returns a table)
SELECT * FROM CONTAINSTABLE
     (Production.ProductReview,Comments, 'adventure');
-- [Output]
-- KEY    RANK
-- 4      8

-- Joining with full-text search
SELECT PR.Comments,
       FTS.Rank
FROM Production.ProductReview  PR
JOIN 
CONTAINSTABLE(Production.ProductReview,
               Comments,
               'adventure') FTS
ON PR.ProductReviewID = FTS.[KEY]
ORDER  BY FTS.Rank DESC;
 
 -- Use double quotes (" "),
 -- to look for a complete phrase
 SELECT ProductID, Comments 
    FROM Production.ProductReview 
    WHERE CONTAINS(Comments, '"the praises"');
    
-- OR, AND
SELECT Comments 
FROM Production.ProductReview 
WHERE CONTAINS(Comments, 'little OR mountain') 

-- Multiple phrases
SELECT Comments
FROM Production.ProductReview 
WHERE CONTAINS(Comments, 'getting') 
AND CONTAINS(Comments,'new');

-- DOS Wildcards *
SELECT Comments 
FROM Production.ProductReview 
WHERE CONTAINS(Comments, '"*Adventure*"') 

-- Near: Relatively near each other
SELECT Comments 
FROM Production.ProductReview 
WHERE CONTAINS(Comments, 'mountain NEAR biking') 

-- Weighting
SELECT PR.ProductID, PR.Comments, FTS.Rank
FROM Production.ProductReview  PR
JOIN 
CONTAINSTABLE(Production.ProductReview,
    Comments, 'ISABOUT (little weight (.5),
                adventure weight (.5))') FTS
ON PR.ProductReviewID = FTS.[KEY]
ORDER  BY FTS.Rank DESC

-- Inflection
-- Find for inflectional change of the word
-- Here: praises is an inflectional change of praise
SELECT Comments 
FROM Production.ProductReview 
WHERE CONTAINS(Comments,
     'FORMSOF(INFLECTIONAL,praise)');
     
-- Output
-- I can't believe I'm singing the praises of  ...

以上是关于sql MS SQL全文搜索查询示例的主要内容,如果未能解决你的问题,请参考以下文章

SQL 2008 全文搜索词邻近度

带有可为空参数的sql全文搜索

如何提高sql2000全文检索搜索效率

SQL 2008:关闭全文搜索查询的停用词

SQL Server中的全文搜索

将用户输入的搜索查询转换为用于 SQL Server 全文搜索的 where 子句