2009-12-14 53 views
1

我創建了一個網站,但我有一個問題。 我想建立一旦索引和使用它。Lucene.net:單獨的建築索引搜索索引

目前我有兩個功能,「創建一個文檔的儲存入目錄」和「搜索」

當用戶提交:

sub submit() 
    create_doc() 
    search(text) 
end sub 

這個工作,但是當我嘗試這:

create_doc() 
sub submit() 
    search(text) 
end sub 

這就像目錄已被刪除。

global: 
Dim analyzer As StandardAnalyzer = New StandardAnalyzer()Dim directory As Directory = FSDirectory.GetDirectory("C:\[...]luceneindex", True) 
Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, True) 

Sub create_doc() 
    Dim meindoc As New Document() 
    im feldbodytext As Field = New Field("bodytext", textstring[...] 
    meindoc.Add(feldbodytext) 
    indexwriter.AddDocument(meindoc) 
    indexwriter.Close() 
end sub 

Sub lucene_search(ByVal strSuchbegriff As String) 
    Dim parser As QueryParser = New QueryParser("bodytext", analyzer) 
    Dim query As Query = parser.Parse(strSuchbegriff) 
    Dim hits As Hits = searcher.Search(query) 
    [...] 
end sub 

是否有可能永久存儲索引? 可能會有init問題。指數作家gloabel,但關閉它當地?

回答

4

我覺得你的問題是,每次你宣佈你IndexWriter,正在重新創建索引和刪除索引的內容 - 這是因爲第三個參數傳遞到構造函數(True)的:

Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, True) 

您應該改用False,以表明該指數的現有內容應保持不變:

Dim indexwriter As IndexWriter = New IndexWriter(directory, analyzer, False) 
+0

您好,非常感謝您的回答侑, 利用昏暗的IndexWriter作爲的IndexWriter =新的IndexWriter(目錄,分析儀,FALSE) 我遇到一個錯誤「源無法找到」 ,它甚至當我第一次編入索引時沒有找到任何東西, 當我把兩個函數放在另一個函數中時,他找到了一個索引。 它可能是目錄? : Dim directory As Directory = FSDirectory.GetDirectory(「C:\ Dok [...] \ luceneindex」,True) – Tyzak 2009-12-15 08:00:30

0

啊,我想我已經得到了它;-)

我第一次創建一個索引我必須使用

Dim directory As Directory = FSDirectory.GetDirectory("C:\[...]\luceneindex", True) 
Dim indexwriter As IndexWriter = New IndexWriter("C:\[...]luceneindex", analyzer, True) 

和索引後,我不得不都與「假」使用。

真的每次都創建一個索引? 感謝=)

+0

是的 - 您需要在第一次創建索引時使用該目錄,然後從該點使用假表示您正在添加到現有的索引。 – Justin 2009-12-15 09:21:07