lucenenet-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Quinton Bernhardt <>
Subject RAMDirectory - i cannot delete document
Date Wed, 22 Aug 2012 06:04:50 GMT

I'm using the RAMDirectory [for a unit test] in and i
cannot seem to delete a document from the index.  In the test, updating the
same document results in 2 documents in the index.  I've tried the
deletedocument and the updatedocument method.  Perhaps you can spot
something i'm going wrong here?

-- creating the indexwriter instance with the ramdirectory
private IndexWriter makeMemoryIndexWriter() {
            Directory ram = new RAMDirectory();

            IndexWriter result = new IndexWriter(ram, new
StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true,
            return result;

-- updating the doc (here i've tried the updatedocument and the
deletedocument and then adddocument

 public void addOrUpdate(int objectiveId, DateTime? createdDate, int
objectiveDefinitionId, Entities.ObjectiveRunningStatus
objectiveRunninStatus, string freeTextContent) {

            Term idTerm = new Term("ObjectiveId", objectiveId.ToString());
            TermQuery query = new TermQuery(idTerm);

            Document document = new Document();
            document.Add(new Field("ObjectiveId", objectiveId.ToString()
,Field.Store.YES, Field.Index.NOT_ANALYZED));
            if (createdDate != null) {
                document.Add(new Field("CreatedDate",
DateTools.DateToString(createdDate.Value, DateTools.Resolution.DAY),
Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("ObjectiveDefinitionId",
objectiveDefinitionId.ToString(), Field.Store.YES,
            document.Add(new Field("RunningStatus",
objectiveRunninStatus.ToString(), Field.Store.YES,
            document.Add(new Field("Content", freeTextContent,
Field.Store.COMPRESS, Field.Index.ANALYZED));


-- and finally it may be my test logic that's faulty
        public void AddObjectiveToIndex() {

            // arrange
            IndexWriter indexWriter = makeMemoryIndexWriter();
            ObjectiveIndexWriter target = new
            int objectiveId = 99;
            Nullable<DateTime> createdDate = DateTime.Today;
            int objtiveDefinitionId = 2;
            ObjectiveRunningStatus objectiveRunningStatus =

            // act

            // add control instance
            target.addOrUpdate(objectiveId, createdDate,
objtiveDefinitionId, objectiveRunningStatus, "old data");
            Assert.AreEqual(1, indexWriter.MaxDoc(), "This test is invalid
as the first control value did not get inserted into the RAM index");

            // add changed instance
            target.addOrUpdate(objectiveId, createdDate,
objtiveDefinitionId, objectiveRunningStatus, "updated");

            // assert
            Assert.AreEqual(1, indexWriter.MaxDoc(), "When updating an
existing item, the old document must be deleted"); // FAILS HERE with 2
items in the index!!

            Assert.Fail("must still add assert to fetch and test new
document values");


Thanks for the help.


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message