public virtual void TestTryDeleteDocument() { Directory directory = CreateIndex(); IndexWriter writer = GetWriter(directory); ReferenceManager <IndexSearcher> mgr = new SearcherManager(writer, true, new SearcherFactory()); TrackingIndexWriter mgrWriter = new TrackingIndexWriter(writer); IndexSearcher searcher = mgr.Acquire(); TopDocs topDocs = searcher.Search(new TermQuery(new Term("foo", "0")), 100); Assert.AreEqual(1, topDocs.TotalHits); long result; if (Random.NextBoolean()) { IndexReader r = DirectoryReader.Open(writer, true); result = mgrWriter.TryDeleteDocument(r, 0); r.Dispose(); } else { result = mgrWriter.TryDeleteDocument(searcher.IndexReader, 0); } // The tryDeleteDocument should have succeeded: Assert.IsTrue(result != -1); Assert.IsTrue(writer.HasDeletions()); if (Random.NextBoolean()) { writer.Commit(); } Assert.IsTrue(writer.HasDeletions()); mgr.MaybeRefresh(); searcher = mgr.Acquire(); topDocs = searcher.Search(new TermQuery(new Term("foo", "0")), 100); Assert.AreEqual(0, topDocs.TotalHits); }
private static RAMDirectory MakeEmptyIndex(int numDeletedDocs) { RAMDirectory d = new RAMDirectory(); IndexWriter w = new IndexWriter(d, new WhitespaceAnalyzer(), true, MaxFieldLength.LIMITED, null); for (int i = 0; i < numDeletedDocs; i++) { w.AddDocument(new Document(), null); } w.Commit(null); w.DeleteDocuments(null, new MatchAllDocsQuery()); w.Commit(null); if (0 < numDeletedDocs) { Assert.IsTrue(w.HasDeletions(null), "writer has no deletions"); } Assert.AreEqual(numDeletedDocs, w.MaxDoc(), "writer is missing some deleted docs"); Assert.AreEqual(0, w.NumDocs(null), "writer has non-deleted docs"); w.Close(); IndexReader r = IndexReader.Open((Directory)d, true, null); Assert.AreEqual(numDeletedDocs, r.NumDeletedDocs, "reader has wrong number of deleted docs"); r.Close(); return(d); }
public virtual void TestDeleteDocuments() { Directory directory = CreateIndex(); IndexWriter writer = GetWriter(directory); ReferenceManager <IndexSearcher> mgr = new SearcherManager(writer, true, new SearcherFactory()); IndexSearcher searcher = mgr.Acquire(); TopDocs topDocs = searcher.Search(new TermQuery(new Term("foo", "0")), 100); Assert.AreEqual(1, topDocs.TotalHits); TrackingIndexWriter mgrWriter = new TrackingIndexWriter(writer); long result = mgrWriter.DeleteDocuments(new TermQuery(new Term("foo", "0"))); Assert.AreEqual(1, result); // writer.Commit(); Assert.IsTrue(writer.HasDeletions()); mgr.MaybeRefresh(); searcher = mgr.Acquire(); topDocs = searcher.Search(new TermQuery(new Term("foo", "0")), 100); Assert.AreEqual(0, topDocs.TotalHits); }
/// <summary> /// 删除全部索引数据 /// </summary> /// <returns></returns> public bool DeleteAll() { bool IsSuccess = true; try { IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED); writer.DeleteAll(); writer.Commit(); IsSuccess = writer.HasDeletions(); writer.Dispose(); } catch { IsSuccess = false; } return IsSuccess; }
/// <summary> /// 删除索引数据(根据id) /// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(string id) { bool IsSuccess = false; Term term = new Term("id", id); IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED); writer.DeleteDocuments(term); // writer.DeleteDocuments(term)或者writer.DeleteDocuments(query); writer.Commit(); IsSuccess = writer.HasDeletions(); writer.Dispose(); return IsSuccess; }