lucenenet-user mailing list archives

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

I'm using the RAMDirectory [for a unit test] in lucene.net 2.9.4.1 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,
IndexWriter.MaxFieldLength.UNLIMITED);
            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) {
            ModelAsserts.assertLargerThanZeroValue(objectiveId,
"objectiveId");
            ModelAsserts.assertLargerThanZeroValue(objectiveDefinitionId,
"objectiveDefinitionId");
            ModelAsserts.assertNonNullNonBlankParameter(freeTextContent,
"freeTextContent");

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

            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,
Field.Index.NOT_ANALYZED));
            document.Add(new Field("RunningStatus",
objectiveRunninStatus.ToString(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
            document.Add(new Field("Content", freeTextContent,
Field.Store.COMPRESS, Field.Index.ANALYZED));


            _indexWriter.AddDocument(document);
            _indexWriter.Commit();
        }

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

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

            // 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.

-- 
/quinton

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