gpt4 book ai didi

java - 从命中/命中迁移到 TopDocs/TopDocCollector

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:55 25 4
gpt4 key购买 nike

我有这样的现有代码:

final Term t = /* ... */;
final Iterator i = searcher.search( new TermQuery( t ) ).iterator();
while ( i.hasNext() ) {
Hit hit = (Hit)i.next();
// "FILE" is the field that recorded the original file indexed
File f = new File( hit.get( "FILE" ) );
// ...
}

我不清楚如何使用 TopDocs/TopDocCollector 重写代码以及如何迭代所有结果。

最佳答案

基本上,您必须决定对预期结果数量的限制。然后在生成的 TopDocs 中遍历所有 ScoreDoc

final MAX_RESULTS = 10000;
final Term t = /* ... */;
final TopDocs topDocs = searcher.search( new TermQuery( t ), MAX_RESULTS );
for ( ScoreDoc scoreDoc : topDocs.scoreDocs ) {
Document doc = searcher.doc( scoreDoc.doc )
// "FILE" is the field that recorded the original file indexed
File f = new File( doc.get( "FILE" ) );
// ...
}

这基本上就是 Hits 类所做的,只是它将限制设置为 50 个结果,如果您迭代超过该限制,则会重复搜索,即通常很浪费。这就是它被弃用的原因。

添加:如果对结果的数量没有限制,则应使用 HitCollector:

final Term t = /* ... */;
final ArrayList<Integer> docs = new ArrayList<Integer>();
searcher.search( new TermQuery( t ), new HitCollector() {
public void collect(int doc, float score) {
docs.add(doc);
}
});

for(Integer docid : docs) {
Document doc = searcher.doc(docid);
// "FILE" is the field that recorded the original file indexed
File f = new File( doc.get( "FILE" ) );
// ...
}

关于java - 从命中/命中迁移到 TopDocs/TopDocCollector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/973354/

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