/// <summary> /// 更新索引 /// </summary> /// <param name="changeset">实体</param> public void Update(LuceneIndexChangeset changeset) { var config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, _analyzer); using var writer = new IndexWriter(_directory, config); foreach (var change in changeset.Entries) { switch (change.State) { case LuceneIndexState.Added: writer.AddDocument(change.Entity.ToDocument()); break; case LuceneIndexState.Removed: writer.DeleteDocuments(new Term("IndexId", change.Entity.IndexId)); break; case LuceneIndexState.Updated: writer.UpdateDocument(new Term("IndexId", change.Entity.IndexId), change.Entity.ToDocument()); break; } } writer.Flush(true, changeset.HasDeletes); }
/// <summary> /// 删除索引 /// </summary> /// <param name="entries">实体集</param> public void Delete <T>(List <T> entries) where T : ILuceneIndexable { var set = new LuceneIndexChangeset { Entries = entries.Select(e => new LuceneIndexChange(e, LuceneIndexState.Removed)).ToList() }; Update(set); }
/// <summary> /// 更新索引 /// </summary> /// <param name="change">实体</param> public void Update(LuceneIndexChange change) { var changeset = new LuceneIndexChangeset(change); Update(changeset); }