lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Taylor <paul_t...@fastmail.fm>
Subject Why is the old value still in the index
Date Fri, 16 Dec 2011 16:54:05 GMT
I'm adding documents to an index, at a later date I modify a document 
and update the index, close the writer and open a new IndexReader. My 
indexreader iterates over terms for that field and docFreq() returns one 
as I would expect, however the iterator  returns both the old value of 
the document and the new value, I don't expect (or want) the old value 
to still be in the index, so why is this.


This full test program generates:

TermDocsFreq1
test
TermDocsFreq1
test
test2

Dont expect to see 'test' listed the second time


package com.jthink.jaikoz;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.*;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;


public class LuceneTest
{
     public  static void main(String []args)
     {
         try
         {
             String FIELD1="field1";
             RAMDirectory dir = new RAMDirectory();
             IndexWriterConfig iwc = new 
IndexWriterConfig(Version.LUCENE_35, new 
StandardAnalyzer(Version.LUCENE_35));
             IndexWriter       iw  = new IndexWriter(dir, iwc);
             Document document = new Document();
             document.add(new Field(FIELD1,"test", Field.Store.YES, 
Field.Index.ANALYZED));
             iw.addDocument(document);
             iw.close();

             IndexReader ir = IndexReader.open(dir,true);
             TermEnum terms = ir.terms(new Term(FIELD1));
             System.out.println("TermDocsFreq"+terms.docFreq());
             do
             {
                 if (terms.term() != null)
                 {
                     System.out.println(terms.term().text());
                 }
             }
             while (terms.next() && terms.term().field().equals(FIELD1));

             IndexWriterConfig iwc2 = new 
IndexWriterConfig(Version.LUCENE_35, new 
StandardAnalyzer(Version.LUCENE_35));
             iw  = new IndexWriter(dir, iwc2);
             document = new Document();
             document.add(new Field(FIELD1,"test2", Field.Store.YES, 
Field.Index.ANALYZED));
             iw.updateDocument(new Term(FIELD1,"term1"),document);
             iw.close();

             ir = IndexReader.open(dir,true);
             terms = ir.terms(new Term(FIELD1));
             System.out.println("TermDocsFreq"+terms.docFreq());
             do
             {
                 if (terms.term() != null)
                 {
                     System.out.println(terms.term().text());
                 }
             }
             while (terms.next() && terms.term().field().equals(FIELD1));
         }
         catch(Exception ex)
         {
             ex.printStackTrace();
         }
     }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message