2012-12-24 83 views
3

我想要得到所有結果solrj,我添加了10個文件到Solr,我沒有得到任何異常,但如果我添加超過10個文件到Solr我得到例外。我搜索一下,我得到這個例外,在http://localhost:8983/solr/browse 10個文件在第一頁,第11個文件轉到第二頁。我如何獲得所有結果?Solrj獲得所有結果

String qry="*:*"; 
       CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr"); 
     QueryResponse rsp=server.query(new SolrQuery(qry)); 
     SolrDocumentList docs=rsp.getResults(); 
         for(int i=0;i<docs.getNumFound();i++){ 

          System.out.println(docs.get(i));      
    } 

異常在線程 「AWT-EventQueue的 - 0」 java.lang.IndexOutOfBoundsException:指數:10,大小:10

回答

7
Integer start = 0; 

    query.setStart(start); 
    QueryResponse response = server.query(query); 
    SolrDocumentList rs = response.getResults(); 
    long numFound = rs.getNumFound(); 
    int current = 0; 
    while (current < numFound) { 

     ListIterator<SolrDocument> iter = rs.listIterator(); 
     while (iter.hasNext()) { 
      current++; 

      System.out.println("************************************************************** " + current + " " + numFound); 
      SolrDocument doc = iter.next(); 
      Map<String, Collection<Object>> values = doc.getFieldValuesMap(); 

      Iterator<String> names = doc.getFieldNames().iterator(); 
      while (names.hasNext()) { 
       String name = names.next(); 
       System.out.print(name); 
       System.out.print(" = "); 

       Collection<Object> vals = values.get(name); 
       Iterator<Object> valsIter = vals.iterator(); 
       while (valsIter.hasNext()) { 
        Object obj = valsIter.next(); 
        System.out.println(obj.toString()); 
       } 
      } 
     } 
     query.setStart(current); 
     response = server.query(query); 
     rs = response.getResults(); 
     numFound = rs.getNumFound(); 


    } 
} 
+2

你好 - 歡迎來到StackOverflow!有一些評論和答案會讓你更容易理解。 – Nilesh

5

numFound給你匹配查詢結果的總數。

但是,默認情況下,Solr將只返回由參數rows控制的前10個結果。
您正在嘗試遍歷numFound,但由於返回的結果僅爲10,因此失敗。
您應該使用rows參數進行迭代。

要獲得下一組結果,您需要使用不同的start參數重新查詢Solr。這是爲了支持分頁,以便您不必一次性提取所有結果,這是一項非常繁重的操作。

7

一個簡單的方法:

CloudSolrServer server = new CloudSolrServer(solrZKServerUrl); 
SolrQuery query = new SolrQuery(); 
query.setQuery("*:*"); 
query.setRows(Integer.MAX_VALUE); 
QueryResponse rsp; 
rsp = server.query(query, METHOD.POST); 
SolrDocumentList docs = rsp.getResults(); 
for (SolrDocument doc : docs) { 
    Collection<String> fieldNames = doc.getFieldNames(); 
    for (String s: fieldNames) { 
     System.out.println(doc.getFieldValue(s)); 
    } 
} 
+1

陳,試着添加更多的細節 – ANjaNA

+0

希望很明顯。 –

0

如果你重構你這樣的代碼,將工作

String qry="*:*"; 
SolrQuery query = new SolrQuery(); 
query.setQuery("*:*"); 
query.setRows(Integer.MAX_VALUE); //Add me to avoid IndexOutOfBoundExc 
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr"); 
QueryResponse rsp=server.query(query); 
SolrDocumentList docs=rsp.getResults(); 
for(int i=0;i<docs.getNumFound();i++){ 
    System.out.println(docs.get(i)); 
      } 

這個問題的答案,爲什麼它很簡單。

的反應是告訴你,有getNumFound()匹配的文檔, 但如果你沒有在查詢中指定的響應必須有多少人攜帶,這個限制會自動設置好的10個,

結束了 只取了排名前10位的文件出來getNumFound()的文檔中找到

因爲這個原因,文件清單將只有10個元素,並試圖與我> 9做第i個elementh的get (例如10)將帶你到

java.lang.IndexOutOfBoundsException

就像你正在試驗。

P.S我建議你使用了迭代器就像@Chen勝倫一樣。

P.P.S起初這讓我抓狂了。

相關問題