gpt4 book ai didi

java - Lucene - 在 writer.updateDocuments() 中使用 LongPoint Field 作为术语更新文档

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:32 25 4
gpt4 key购买 nike

我正在尝试使用 writer.update(Term t, Document doc) 方法更新索引中的文档。如果我在 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() 以术语作为选择标准,而不是 LongPoint 字段。
有没有其他方法可以实现这一目标?

最佳答案

updateDocument 所做的就是删除包含该术语的文档,然后添加新文档。因此,您可以只使用 deleteDocuments,您可以将查询传递给它,然后使用 addDocument 来完成同样的事情。

writer.deleteDocuments(LongPoint.newExactQuery("foo_number", 1000));
writer.addDocument(newDoc);

关于java - Lucene - 在 writer.updateDocuments() 中使用 LongPoint Field 作为术语更新文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44853827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com