8

我們有一個使用SQL Server 2008作爲數據庫的Web應用程序。我們的用戶可以在數據庫中的特定列上進行全文搜索。 SQL Server的全文功能似乎不支持命中突出顯示。我們是否需要自己來構建它,或者是否有一些圖書館或知識來解決如何做到這一點?如何從SQL Server全文查詢中突出顯示結果

順便說一句,應用程序是用C#編寫的,所以.Net解決方案將是理想的,但不是必要的,因爲我們可以翻譯。

+0

http://www.sqlperformance.com/2012/09/t-sql-queries/hit-highlighting-in-full-text-search – 2012-09-25 13:56:25

回答

3

伊斯梅爾的想法不斷擴展,這不是最終的解決方案,但我認爲這是一個很好的開始。

首先,我們需要獲取已經檢索與全文引擎的單詞列表:

declare @SearchPattern nvarchar(1000) = 'FORMSOF (INFLECTIONAL, " ' + @SearchString + ' ")' 
declare @SearchWords table (Word varchar(100), Expansion_type int) 
insert into @SearchWords 
select distinct display_term, expansion_type 
from sys.dm_fts_parser(@SearchPattern, 1033, 0, 0) 
where special_term = 'Exact Match' 

已經有相當多的人可以擴大,例如搜索模式是基本相當;也有可能有更好的方法來過濾掉你不需要的單詞,但它最少會給你一個詞幹單詞列表等,這些單詞將通過全文搜索進行匹配。

當你得到你需要的結果後,你可以使用RegEx來解析結果集(或者最好只有一個子集來加速它,儘管我還沒有想出一個好辦法)。爲此,我只需用兩個while循環和一堆臨時表和變量:

declare @FinalResults table 
while (select COUNT(*) from @PrelimResults) > 0 
begin 
    select top 1 @CurrID = [UID], @Text = Text from @PrelimResults 
    declare @TextLength int = LEN(@Text) 
    declare @IndexOfDot int = CHARINDEX('.', REVERSE(@Text), @TextLength - dbo.RegExIndexOf(@Text, '\b' + @FirstSearchWord + '\b') + 1) 
    set @Text = SUBSTRING(@Text, case @IndexOfDot when 0 then 0 else @TextLength - @IndexOfDot + 3 end, 300) 

    while (select COUNT(*) from @TempSearchWords) > 0 
    begin 
     select top 1 @CurrWord = Word from @TempSearchWords 
     set @Text = dbo.RegExReplace(@Text, '\b' + @CurrWord + '\b', '<b>' + SUBSTRING(@Text, dbo.RegExIndexOf(@Text, '\b' + @CurrWord + '\b'), LEN(@CurrWord) + 1) + '</b>') 
     delete from @TempSearchWords where Word = @CurrWord 
    end 

    insert into @FinalResults 
    select * from @PrelimResults where [UID] = @CurrID 
    delete from @PrelimResults where [UID] = @CurrID 
end 

幾個注意事項:
1.嵌套while循環可能是沒有這樣做的最有效的方法,但是什麼還有其他的想法。如果我要使用遊標,它基本上是一樣的東西?
2. @FirstSearchWord這裏指的是原始搜索詞之一的文本中的第一個實例,所以基本上,您正在替換的文本只會在摘要中。再次,這是一個非常基本的方法,某種文本羣集發現算法可能會很方便。
3.要首先獲得RegEx,您需要CLR用戶定義函數。

1

在這種情況下,您可能會錯過數據庫的要點。它的工作是將數據返回給您,以滿足您提供的條件。我想你會想在你的web控件中使用正則表達式來實現突出顯示。

這是一個快速搜索將顯示的內容。

http://www.dotnetjunkies.com/PrintContent.aspx?type=article&id=195E323C-78F3-4884-A5AA-3A1081AC3B35

+3

感謝您的回覆。雖然我意識到突出顯示不在數據庫的範圍之內,但也許數據庫應該提供命中位置等,而不必依賴於正則表達式等,當你考慮詞幹,停止詞等的影響時,這可能是困難/不準確的。 。 – 2008-09-16 23:32:37

1

一些細節:

  search_kiemeles=replace(lcase(search),"""","") 
      do while not rs.eof 'The search result loop 
       hirdetes=rs("hirdetes") 
       data=RegExpValueA("([A-Za-zöüóőúéáűíÖÜÓŐÚÉÁŰÍ0-9]+)",search_kiemeles) 'Give back all the search words in an array, I need non-english characters also 
       For i=0 to Ubound(data,1) 
        hirdetes = RegExpReplace(hirdetes,"("&NoAccentRE(data(i))&")","<em>$1</em>") 
       Next 
       response.write hirdetes 
       rs.movenext 
      Loop 
      ... 

功能

'All Match to Array 
Function RegExpValueA(patrn, strng) 
    Dim regEx 
    Set regEx = New RegExp ' Create a regular expression. 
    regEx.IgnoreCase = True ' Set case insensitivity. 
    regEx.Global = True 
    Dim Match, Matches, RetStr 
    Dim data() 
    Dim count 
    count = 0 
    Redim data(-1) 'VBSCript Ubound array bug workaround 
    if isnull(strng) or strng="" then 
     RegExpValueA = data 
     exit function 
    end if 
    regEx.Pattern = patrn ' Set pattern. 
    Set Matches = regEx.Execute(strng) ' Execute search. 
    For Each Match in Matches ' Iterate Matches collection. 
     count = count + 1 
     Redim Preserve data(count-1) 
     data(count-1) = Match.Value 
    Next 
    set regEx = nothing 
    RegExpValueA = data 
End Function 

'Replace non-english chars 
Function NoAccentRE(accent_string) 
    NoAccentRE=accent_string 
    NoAccentRE=Replace(NoAccentRE,"a","§") 
    NoAccentRE=Replace(NoAccentRE,"á","§") 
    NoAccentRE=Replace(NoAccentRE,"§","[aá]") 
    NoAccentRE=Replace(NoAccentRE,"e","§") 
    NoAccentRE=Replace(NoAccentRE,"é","§") 
    NoAccentRE=Replace(NoAccentRE,"§","[eé]") 
    NoAccentRE=Replace(NoAccentRE,"i","§") 
    NoAccentRE=Replace(NoAccentRE,"í","§") 
    NoAccentRE=Replace(NoAccentRE,"§","[ií]") 
    NoAccentRE=Replace(NoAccentRE,"o","§") 
    NoAccentRE=Replace(NoAccentRE,"ó","§") 
    NoAccentRE=Replace(NoAccentRE,"ö","§") 
    NoAccentRE=Replace(NoAccentRE,"ő","§") 
    NoAccentRE=Replace(NoAccentRE,"§","[oóöő]") 
    NoAccentRE=Replace(NoAccentRE,"u","§") 
    NoAccentRE=Replace(NoAccentRE,"ú","§") 
    NoAccentRE=Replace(NoAccentRE,"ü","§") 
    NoAccentRE=Replace(NoAccentRE,"ű","§") 
    NoAccentRE=Replace(NoAccentRE,"§","[uúüű]") 
end function