Domanda

This seems like it should be obvious, but I'm having no luck finding any reliable information on the right way to handle readers and writers in Lucene.net 3.0.3.. My understanding from the docs was that I could have a static IndexWriter that's inherently thread-safe, and I could call GetReader to get readers from that writer as-needed.

I tried this by initializing a static writer in my constructor (code below) but when I do a search against this, I get _0.cfs FileNotFound exceptions (the index is empty, i haven't saved any docs, but i do have segments_1 and segments.gen in my folder).. Is there more to initialization that I'm missing?

_luceneDir = Path.Combine(indexFolder, string.Format("{0}.index", accountId));
Console.WriteLine("Starting up search with index at {0}", _luceneDir);
if(luceneIndexDirectory == null)
    luceneIndexDirectory = FSDirectory.Open(_luceneDir);
if(IndexWriter.IsLocked(luceneIndexDirectory)) 
    IndexWriter.Unlock(luceneIndexDirectory);
if (indexWriter == null)
    indexWriter = new IndexWriter(luceneIndexDirectory, standardAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);
È stato utile?

Soluzione

I'd delete the index folder and simply let lucene create it. If you manually created those segment files, don't do that... also, if those are the only two files, the write.lock is missing actually.

Just to give you a running example which should work. If you create an empty console project and paste this into the main:

using (Directory directory = FSDirectory.Open("LuceneIndex"))
using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
using (IndexWriter writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
using (IndexReader reader = writer.GetReader())
{
    writer.DeleteAll();

    var doc = new Lucene.Net.Documents.Document();
    doc.Add(new Lucene.Net.Documents.Field("ID", "1", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NOT_ANALYZED, Lucene.Net.Documents.Field.TermVector.NO));
    doc.Add(new Lucene.Net.Documents.Field("txt", "text", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NOT_ANALYZED, Lucene.Net.Documents.Field.TermVector.NO));

    writer.AddDocument(doc);
    writer.Optimize();
    writer.Flush(true, true, true);

    Query query = new TermQuery(new Term("txt", "text"));
    //Setup searcher
    IndexSearcher searcher = new IndexSearcher(directory);
    //Do the search
    TopDocs hits = searcher.Search(query, 10);
}

It should create a folder "LuceneIndex" under bin/Debug when running in debug mode, adds one document and should find it at the end.

Maybe that helps you get started...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top