开发者

My Lucene queries only ever find one hit

I'm getting started with Lucene.Net (stuck on version 2.3.1). I add sample documents with this:

    Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)
    Dim doc = Document()
    doc.Add(New Field("Title", "foo", Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.NO))
    doc.Add(New Field("Date", DateTime.UtcNow.ToS开发者_如何学运维tring, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.NO))
    indexWriter.AddDocument(doc)
    indexWriter.Close()

I search for documents matching "foo" with this:

Dim searcher = New IndexSearcher(indexDir)
Dim parser = New QueryParser("Title", New StandardAnalyzer())
Dim Query = parser.Parse("foo")
Dim hits = searcher.Search(Query)
Console.WriteLine("Number of hits = " + hits.Length.ToString)

No matter how many times I run this, I only ever get one result. Any ideas?


Mikos is right about recreating the index, your problem is here:

Dim indexWriter = New IndexWriter(indexDir, New Standard.StandardAnalyzer(), True)

Because you are passing true, you're recreating the index each time - need to check for existence and create IF NEEDED.

I ran into this a while ago, here's how I got around it:

   If _writer Is Nothing Then
       Dim create As Boolean = Not System.IO.Directory.Exists(path) OrElse System.IO.Directory.GetFiles(path).Length = 0

       _directory = FSDirectory.GetDirectory(path, _lockFactory)
       _writer = New IndexWriter(_directory, _analyzer, create)
   End If

Where path is the path to your index. Not sure if this is the best approach but it is working for me (using lucene.net 2.3 also).

Also, you should avoid creating the writer every time if you can - lucene isn't going to like if you get in a situation where you have > 1 writer open on a particular index


Check how many documents are in your index using Luke. Could very well be something in your document add routine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜