如何减小 indexsearcher.search() 函数返回的“命中”对象的大小?
目前我在做类似的事情:
Hits hits = indexSearch.search(query,filter,...);
Iterator hitsIt = hits.iterator();
int newSize=0;
while (hitsIt.hasNext()){
Hit currHit = (Hit)hitsIt.next();
if (hasPermission(currHit)){
newSize++;
}
}
但是,当命中数很大(例如 500 或更多)时,这会产生巨大的性能问题。
我听说过一种叫做“HitsCollector”或“Collector”的东西,应该有助于提高性能,但我不知道如何使用它。
如果有人能指出正确的方向,我将不胜感激。
我们使用 Apache Lucene 在 Atlassian Confluence 网络应用程序中建立索引。
最佳答案
Collector 只是一个简单的回调机制,它会在每次文档命中时被调用,您可以像这样使用一个收集器:-
public class MyCollector extends HitCollector {
// this is called back for every document that
// matches, with the docid and the score
public void collect(int doc, float score){
// do whatever you have to in here
}
}
..
HitCollector collector = new MyCollector();
indexSearch(query,filter,collector);
关于performance - 如何过滤掉indexsearcher.search()函数返回的 "Hits"结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9673349/