2012-03-12 32 views
3

如何縮小由indexsearcher.search()函數返回的「Hits」對象的大小?如何濾除indexsearcher.search()函數返回的結果「Hits」?

目前,我做這樣的事情:

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」或可能是「收藏家」,這應該有助於提高性能,但我不知道如何使用它。

如果有人能指出我正確的方向,我將不勝感激。

我們在Atlassian Confluence web應用程序中使用Apache Lucene進行索引。

回答

1

爲了獲得良好的性能,您必須將安全信息與每個文檔一起編入索引。這當然取決於你的安全模型。例如,如果您可以將每個文檔分配給對其有權限的安全角色,則可以使用它。也 檢查出this question。你的幾乎是一個重複的。

2

的收集器僅僅是被調用爲每個文檔打一個簡單的回調機制,你會使用一個收集器是這樣的: -

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);