You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lucenenet.apache.org by Quinton Bernhardt <fr...@gmail.com> on 2012/08/22 08:04:50 UTC

RAMDirectory - i cannot delete document

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

Re: RAMDirectory - i cannot delete document

Posted by Quinton Bernhardt <fr...@gmail.com>.
Hi

I found the problem, i was supposed to use method .numDocs() not .MaxDocs().

Thanks

On Wed, Aug 22, 2012 at 8:04 AM, Quinton Bernhardt <fr...@gmail.com>wrote:

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


-- 
/quinton