我尝试使用writer.update(Term t,Documentdoc)方法更新索引中的文档。如果在Term中指定TextField,则更新成功,但如果在Term中提供LongPoint,则更新失败
我的代码示例:
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'
}
}
}
此代码成功地用第二个文档更新了初始文档...
但如果我用
writer.updateDocument(new Term(“foo_number”,1000),newDoc);
失败...我的要求是通过基于LongPoint字段唯一标识文档来更新文档。
文档(lucene 6.3.0)只有updateDocuments(),并将Terms作为选择标准,而不是LongPoint字段。
有没有其他方法可以做到这一点?
2条答案
按热度按时间juzqafwq1#
updateDocument
所做的就是删除包含该术语的文档,然后添加新文档。所以,你可以只使用deleteDocuments
,你可以向它传递一个Query,后面跟着addDocument
来完成同样的事情。bkhjykvo2#
这个问题应该在最近的Lucene发行版(我猜是9.7.0)中解决。有一个pull请求向IndexWriter添加了一个updateDocuments(Query,Iterable)方法。