lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tim Eck" <>
Subject update/re-add an existing document with numeric fields
Date Wed, 09 May 2012 17:21:30 GMT
Note: I'm bound to lucene 3.0.3 for the context of this question, but

I would be interested to know if newer versions would help me here.  


I have an existing document in my directory that has one regular

String field and one numeric field. I naively thought I could update

that document to change the String field with code like this: 


  FSDirectory dir =;

  IndexWriter writer = new IndexWriter(dir, new

      StandardAnalyzer(Version.LUCENE_30), MaxFieldLength.UNLIMITED);


  // doc has 2 fields, one String and the other numeric

  Document doc = new Document();

  doc.add(new Field("string", "value", Store.YES,


  NumericField nf = new NumericField("numeric", Field.Store.YES,







  // make sure we can query on the numeric field

  IndexSearcher searcher = new IndexSearcher(dir);

  TopDocs docs = TermQuery(new Term("numeric",

      NumericUtils.intToPrefixCoded(42))), 1); 

  if (docs.totalHits != 1) {

      throw new AssertionError();


  doc = searcher.doc(docs.scoreDocs[0].doc);



  // update document with new value for string field


  doc.add(new Field("string", "value2", Store.YES,


  writer.updateDocument(new Term("string", "value"), doc);



  // search again

  searcher = new IndexSearcher(dir);

  docs = TermQuery(new Term("numeric",

      NumericUtils.intToPrefixCoded(42))), 1); 

  if (docs.totalHits != 1) {

      throw new AssertionError(docs.totalHits);




That doesn't seem to work however. It seems I need to get the 

NumericField rematerialized in the document passed to

updateDocument(). I was hoping to avoid that if possible so

I'm looking for any suggestions someone might offer. 






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