我现有的代码是这样的:

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重写代码以及如何遍历所有结果。

最佳答案

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

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/

10-11 02:41