1

この記事http://leoncullens.nl/post/2012/11/18/Full-Text-Search-on-Azure-with-LuceneNET.aspxに従って、 Lucene インデックス作成をセットアップしました。

ほとんどの場合スムーズに実行されますが、ID に関連付けられた既存のドキュメントが存在する場合、それを削除してから再挿入するように編集するにはどうすればよいですか?

次のメソッドを編集します。

public void CreateIndex() {


IndexWriter indexWriter = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED); // Create    the IndexWriter

    foreach (Book book in _bookService.List()) // Use whatever data source you like
    {
        // NEED TO INSERT A CHECK HERE TO SEE IF A DOC EXISTS
        Document document = CreateBookDocument(book); // Create a 'Document' for each row of your data

        indexWriter.AddDocument(document);
    }

    try
    {
        indexWriter.Optimize(); // Call optimize once to improve performance
        indexWriter.Dispose(); // Commit and dispose the object

        Thread.Sleep(60 * 10 * 1000); // Sleep 10 minutes when the index is created successfully, otherwise immediately restart the process
    }
    catch (Exception)
    {
        indexWriter.Rollback();
        indexWriter.Dispose();
    }
}

private Document CreateBookDocument(Book book)
{
    Document document = new Document();
    document.Add(new Field("Id", book.Id.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO)); // We want to store the ID so we can retrieve data for the ID, but we don't want to index it because it is useless searching through the IDs
    document.Add(new Field("Title", book.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
    document.Add(new Field("Publisher", book.Publisher, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
    document.Add(new Field("Isbn10", book.Isbn10, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));

    return document;
}
4

2 に答える 2

1

私の理解では、実際に Lucene 内の Document インスタンスを更新するようなものはありません。常に削除/挿入として行われます。

于 2013-02-25T20:24:45.477 に答える
1

we use the Lucene.Net 2.0.0.4, and for 3.0 I think they are the same.

first You need to add the ID as Field.Index.TOKENIZED, then you can modify(delete first then insert)the Index.

document.Add(new Field("Id", book.Id.ToString(), Field.Store.YES, Field.Index.TOKENIZED));

then open the index instead of create the new

IndexWriter indexWriter = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), false, IndexWriter.MaxFieldLength.UNLIMITED);

there is the delete sample code:

IndexReader reader = IndexReader.Open(_directory);
reader.DeleteDocuments(new Term("Id", book_id_delete));
reader.Close();
于 2013-04-17T17:49:12.310 に答える