2013-12-09 51 views
0

我需要你的幫助。我想在全文太陽黑子搜索中獲得匹配的字符串。 那是我的代碼如何在目前太陽黑子軌 - 在全文搜索中獲得匹配的子字符串

search2 = WebsiteEntry.search do 
    fulltext params[:q] 
    end 

    search2.each_hit_with_result do |hit, res| 
    @match = <Here I need your Help, i need the substring on which sunspot made a hit> 
    @results.push SearchResult.new(res, hit.score, @match) 
    end 
end 

看起來像現在,如果i'm搜索單詞「測試」黑子看上去一切,其中「測試」是一個子不區分大小寫。 例如:太陽黑子返回匹配的單詞「FirstTest」或「TEST2」或「testit」。

是否有可能獲得匹配的字符串?我需要太陽黑子擊中的字符串

從「FirstTest」需要點擊「測試」。因爲我想顯示太陽黑子創建比賽的地方。所以,從「FirstTest」我需要子字符串「測試」。從「TEST2」我需要「測試」等。

謝謝。

回答

0

我現在已經解決了我的問題。 「命中」 - OBJEKT在「each_hit_with_result」函數返回與所匹配的子字符串列表,稱爲「亮點」

search2 = WebsiteEntry.search do 
    fulltext params[:q] do 
     highlight :domain 
    end 
    end 

    search2.each_hit_with_result do |hit, res| 
    hit.highlight(:domain).format { |word| puts "highlight: "+word} 

沒有I'm讓每一個匹配字符串形成完整的文本搜索。問題是,我沒有把我的屬性標記爲重點。 Thx