2011-11-02 49 views
0

我正在Lucene中運行一個程序。我得到每個單詞的總點擊次數。這意味着它會獲取包含我搜索的單詞的所有文件。點擊lucene的總數

實施例:

Searching for 'Amazon' 
Number of hits: 2 
Hit: Files\peru.txt 
Hit: Files\correspondent.txt 
Searching for 'business' 
Number of hits: 5 
Hit: Files\innovation.txt 
Hit: Files\xmas.txt 
Hit: Files\bp.txt 
Hit: Files\symbian.txt 
Hit: Files\peru.txt 
Searching for 'environment' 
Number of hits: 3 
Hit: Files\food.txt 
Hit: Files\sarkozy.txt 
Hit: Files\symbian.txt 

第一個問題是如何增加的總命中數爲整個查詢(2 + 5 + 3),並顯示它們它們。

我的第二個問題是如何顯示結果的順序?從2然後3然後5

任何建議將感激!

代碼搜索的索引和上述輸出:

public static void searchIndex(String searchString) throws IOException, ParseException { 
     int counter = 0 ; 



     System.out.println("Searching for '" + searchString + "'"); 
     Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY); 
     IndexReader indexReader = IndexReader.open(directory); 
     IndexSearcher indexSearcher = new IndexSearcher(indexReader); 

     Analyzer analyzer = new StandardAnalyzer(); 
     QueryParser queryParser = new QueryParser(FIELD_CONTENTS, analyzer); 
     Query query = queryParser.parse(searchString); 
     Hits hits = indexSearcher.search(query); 
     System.out.println("Number of hits: " + hits.length()); 



     Iterator<Hit> it = hits.iterator(); 
     while (it.hasNext()) { 
      Hit hit = it.next(); 
      Document document = hit.getDocument(); 
      String path = document.get(path1); 
      System.out.println("Hit: " + path); 
     } 

    } 
} 

問候。

回答

1

使用Searcher.search由會員TopDocs.totalHits得到TopDocs爲每個關鍵字,然後總和/排序。

search的第二個參數應該不重要,如果你只是想統計。如果您想查找所有匹配,請將其設置爲您索引中文檔的數量,因爲這對於匹配數量是一個平凡的上限。

+0

我編輯了我的文章並添加了代碼。你能告訴我怎樣才能說明我的要求? – HShbib

+0

@HumamShbib:你似乎在使用Lucene 2.x API。 'TopDocs.totalHits'在該API中是'Hits.length()'。我假設你知道如何對列表進行求和或排序? –

+0

我沒有任何想法,我嘗試使用沒有做任何事情的計數器。您能否告訴我如何總結每個文檔的Hits.length命中?並以Acsending方式對總數進行排序。 – HShbib