I am trying to update a document in my index using writer.update(Term t, Document doc) method. Update is successful if I specify a TextField in Term, but fails when I supply a LongPoint in Term
A sample of my code:
package com.luceneserver.core;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.RAMDirectory;
public class SampleDocUpdates {
public static void main(String[] args) throws IOException {
IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig());
IndexReader reader;
IndexSearcher searcher;
// first document in the index
Document initialDoc = new Document();
// adding a text field
initialDoc.add(new TextField("foo_text", "abc", Store.YES));
// adding a numeric field
initialDoc.add(new LongPoint("foo_number", 1000));
// adding stored field to display hits
initialDoc.add(new StoredField("foo_number", 1000));
writer.addDocument(initialDoc);
// second document in index which should update the first one..
Document newDoc = new Document();
newDoc.add(new TextField("foo_text", "def", Store.YES));
newDoc.add(new LongPoint("foo_number", 2000));
newDoc.add(new StoredField("foo_number", 2000));
// update doc with foo_text:abc with the newDoc instance.
writer.updateDocument(new Term("foo_text", "abc"), newDoc);
reader = DirectoryReader.open(writer);
searcher = new IndexSearcher(reader);
ScoreDoc[] scoreDocs = searcher.search(new MatchAllDocsQuery(), 1000).scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
System.out.println(searcher.doc(scoreDoc.doc).get("foo_text")+"\t"+searcher.doc(scoreDoc.doc).get("foo_number"));
//'def 2000'
}
}
}
This code successfully updates the initial document with second document...
but if I use
writer.updateDocument(new Term("foo_number", 1000), newDoc);
It fails... My requirement is to update documents by uniquely identifying the documents based on the LongPoint field..
The documentation (lucene 6.3.0) has only updateDocuments() with Terms as selection criteria and not a LongPoint field.
Is there any alternative way to achieve this?
All that updateDocument
does is delete documents that contain the term, and then add the new document. So, you can just use deleteDocuments
, which you can pass a Query to instead, followed by addDocument
to accomplish the same thing.
writer.deleteDocuments(LongPoint.newExactQuery("foo_number", 1000));
writer.addDocument(newDoc);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.