Sqlserver2008實現全文檢索


一、開啟 SQL Full-text 服務

保證 SQL Full-text Filter Daemon Launcher服務處於開啟狀態,不同版本SQLServer全文檢索服務名稱可能稍有不同,如果服務列表中沒有這個服務,請使用SQLServer安裝光盤安裝“全文檢索”組件。

二、啟用全文檢索

執行SQL語句啟用全文檢索:EXEC sp_fulltext_database 'disable'   啟用全文檢索

                     EXEC sp_fulltext_database 'disable'   禁用全文檢索

三、設置全文語言為中文

在服務器->屬性->高級中,設置默認全文語言為2052(中文)。

四、建立數據表

在需要全文檢索的數據表中,必須有一列字符型的字段存放文件類型,例如建表語句中的FileType。必須有一列Varbinary(Max)類型的字段存放文件內容,例如建表語句中的FileContent。

建表SQL語句示例:

1 CREATE TABLE SampleBlobTable
2 (
3 [PKID] int identity(1,1) primary key,
4 [FileName] Nvarchar(255) null,
5 [FileType] Nvarchar(32) null,
6 [FileContent] VARBINARY(MAX) NULL,
7 [AddTime] datetime default(getdate())
8 )

五、建立全文索引

  步驟1:建立全文索引

  在需要全文檢索的數據表上點擊右鍵->全文索引->定義全文索引。

  步驟2:選擇唯一索引

  步驟3:選擇列表

選擇表列,本例中以FileType列標明文件格式,將文件存入數據庫時須正確填寫此字段,此字段中的數據內容包括“doc”、“txt”、“xls”等。

后續步驟無需更改默認值,點擊下一步繼續直至完成。

六、支持PDF文件

  1.安裝 Adobe iFilter
  Adobe iFilter6.0:

  http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=2611&fileID=2457

  Adobe iFilter9.0for 64bit:

  http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=4025&fileID=3941

  2.執行SQL語句
  execsp_fulltext_service 'load_os_resources', 1;

  exec sp_fulltext_service'verify_signature', 0;

  3.重新啟動SQLSERVER
  4.檢查支持文件
  執行下列語句:

  select document_type,path from sys.fulltext_document_types wheredocument_type ='.pdf',如查詢結果為下圖則表示成功,可以進行PDF的全文檢索了。(原博本步驟沒有圖示)

七、查詢語法及示例

  5. 語法

 1 CONTAINS
 2 ( {column| * } , '<contains_search_condition >' 
 3 )
 4 
 5 < contains_search_condition >::=
 6 {< simple_term >
 7 | < prefix_term >
 8 | < generation_term >
 9 | < proximity_term >
10 | < weighted_term >
11 } 
12 | { ( <contains_search_condition > )
13 {AND | AND NOT | OR } < contains_search_condition > [ ...n ] 
14 }
15 
16 < simple_term > ::=
17 word |" phrase "
18 
19 < prefix term> ::=
20 { "word * " | "phrase *"}
21 
22 < generation_term > ::=
23 FORMSOF ( INFLECTIONAL , < simple_term > [ ,...n ] )
24 
25 < proximity_term > ::=
26 {< simple_term > | < prefix_term > }
27 { { NEAR | ~ } { < simple_term > | < prefix_term >} } [ ...n ]
28 
29 < weighted_term > ::=
30 ISABOUT
31 ( {{
32 <simple_term>
33 | < prefix_term >
34 | < generation_term >
35 | < proximity_term >
36 } 
37 [ WEIGHT ( weight_value ) ]
38 } [ ,...n ] 
39 )

  6.示例

  1.查找文件內容含“合同”的數據。

  select * from SampleBlobTable where contains(filecontent,'合同')

  注意:如果查詢條件中包含空格,查詢條件需用雙引號括起來,如'”合同”',否則視為語法錯誤。

  2.查找文件內容含“歸檔”或“標題”的數據。

  select * from SampleBlobTable where contains(filecontent,'歸檔 OR 標題')

  注意:多個詞之間用邏輯操作符連接 (包括 AND ,AND NOT,OR)。如果詞中包含空格,那么這個詞要用雙引號括起來。

  3.查找文件內容含“北京?站”的數據。

  select * from SampleBlobTable where contains(filecontent,'北京Near 站')

  注意:上述SQL語句將返回包含“北京站”、“北京西站”、“北京東站”等“北京”與“站”無間隔或間隔一個漢字(如果是英文則為一個單詞)的數據,不會包含“北京東南站”的數據。

  4.查找所有開頭字母為”hu”的數據。

    select * from SampleBlobTable wherecontains(filecontent,'hu*')

  注意:上述SQL語句將返回包含”human”、”hungry”等單詞的數據,此語法只針對英文有效,針對中文“*”符號無論有無,效果均相同。

  5.加權查詢

   select * from SampleBlobTable where contains(filecontent,'ISABOUT (city weight (.8),county weight (.4))')

  注意:上述SQL語將將針對city和county兩個詞進行不同權重的查詢,權重不同將影響返回數據集的顯示順序(如果限定返回數量,則間接影響是否返回數據)。

  6.多態查詢

  select * from SampleBlobTable where contains(filecontent,'FORMSOF (INFLECTIONAL,dry)')

  注意:查詢將返回包含”dry”,”dried”,”drying”等數據,針對英語有效。

附:文檔修改歷史

內容

修改人

時間

備注

創建

卞吉東

2012-03-29

 

增補

卞吉東

2012-04-01

增加支持PDF文件方法

本文轉載自:https://blog.csdn.net/xiaogechengxuyuan/article/details/10827229 

創建索引及使用詳情參考:https://www.cnblogs.com/qianzf/p/7131741.html

            https://jingyan.baidu.com/article/e5c39bf5fcd7dc39d76033a3.html

            https://blog.csdn.net/qq_24188927/article/details/82841329

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM