开发者

Lucene.net: Separate building Index from Searching the Index

I created a website but i have a problem. i want to build once an index und use it.

at the moment i have two functions "create a document an store it into the directory" and "searching"

when the user submit:

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

this works, but when i try this:

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

it's like the directory has been deleted.

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 m开发者_运维问答eindoc 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

Is there a possibility to store the index permanently? could there be a problem init. the index writer gloabel, but close it local?


I think your problem is that each time you declare your IndexWriter, the index is being re-created and the contents of the index erased - this is because of the 3rd parameter being passed into the constructor (True):

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

You should instead use False, to indicate that the existing contents of the index should remain unchanged:

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


ahh, i think i've got it ;-)

the first time i create a index i have to use

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

and after indexing i have to use both with "False".

True everytimes creates an index? thanks =)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜