2012-01-30 11 views
0

サイトをクロールしてコンテンツをluceneインデックスファイルとして物理ディレクトリに書き込むアプリケーションがあります。Luceneインデックスファイルへのスレッドセーフ書き込み

この目的のためにスレッドを使用すると、ロックによって書き込みエラーまたはエラーが発生します。

私は、複数のスレッドを使用して、スレッドのタスクを失うことなくインデックスファイルに書き込みたいと思っています。上記のクラスに

public class WriteDocument 
{ 
    private static Analyzer _analyzer; 

    private static IndexWriter indexWriter; 

    private static string Host; 

    public WriteDocument(string _Host) 
    { 
     Host = _Host; 
     Lucene.Net.Store.Directory _directory = FSDirectory.GetDirectory(Host, false); 
     _analyzer = new StandardAnalyzer(); 
     bool indexExists = IndexReader.IndexExists(_directory); 
     bool createIndex = !indexExists; 

     indexWriter = new IndexWriter(_directory, _analyzer, true); 
    } 
    public void AddDocument(object obj) 
    { 
      DocumentSettings doc = (DocumentSettings)obj;    
      Field urlField = new Field("Url", doc.downloadedDocument.Uri.ToString(), Field.Store.YES, Field.Index.TOKENIZED); 
      document.Add(urlField); 
      indexWriter.AddDocument(document); 

      document = null; 
      doc.downloadedDocument = null; 

      indexWriter.Optimize(); 
      indexWriter.Close(); 
     } 
} 

、私はこのような値を渡しています:

DocumentSettings writedoc = new DocumentSettings() 
{ 
     Host = Host, 
     downloadedDocument = downloadDocument 
}; 
Thread t = new Thread(() => 
{ 
doc.AddDocument(writedoc); 
}); 
t.Start(); 

私はt.Start();t.Join();を追加した場合、コードがエラーなし私の作品を。しかし、これは私のプロセスを遅くし、事実上、これはスレッドを使用せずに得られる出力と同じです。

私のようなエラーを取得しています:誰もがこのコードで私を助けることができる

Cannot rename /indexes/Segments.new to /indexes/Segments 
the file is used by some other process. 

+0

どのようなエラーが表示されますか?あなたが使用しているコードの小さな例を見ることができますか? – unholysampler

+0

明快にするためにコードを追加しました –

答えて

0

IndexWriterはスレッドセーフではないため、これは不可能です。

ダウンロード用に複数のスレッドを使用する場合は、ダウンロードして作成しているドキュメントをフィードしてキューに入れることができる、ある種の「メッセージポンプ」をビルドする必要があります。

例:AddDocumentメソッドでは、インデックスを直接利用するのではなく、最終的に索引付けするサービスにそれらを送信します。

このサービスは常にキューのすべてのインデックスを作成する必要があります。キューがない場合はしばらくスリープしてください。

+0

うわー、私は混乱しています - Lucene.net 3.0.3 docsはIndexWriterが[thread-safe]です(https://lucenenet.apache.org/docs/3.0.3/d2/d1d/)。 class_lucene_1_1_net_1_1_index_1_1_index_writer.html#details) – chester89

0

スレッドごとに別々のインデックスを作成し、すべてのスレッドを末尾にマージする方法があります。例えばindex1、index2 ... indexn(スレッド1..nに対応)し、それらをマージします。

関連する問題