views:

95

answers:

2

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.ToString, 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?

+1  A: 

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

Mikos
I suspect you are overwriting (recreating) the index in your write loop. Make sure your Index creation code is outside of the write loop
Mikos
+2  A: 

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

AlexCuse