2011-10-28 55 views
3

我需要在Java中獲取Apache Solr中所有文檔的索引大小。此代碼獲得的numFound表示Apache Solr中文檔的總數。通過使用ReplicationHandler,我認爲我可以通過此鏈接獲取某人在此處所告訴的索引大小.. http://lucene.472066.n3.nabble.com/cheking-the-size-of-the-index-using-solrj-API-s-td692686.html。但我沒有得到索引大小。如何獲得索引中的索引大小

BufferedWriter out1 = null; 
     FileWriter fstream1 = new FileWriter("src/test/resources/solr-document-id-desc.txt"); 
     out1 = new BufferedWriter(fstream1); 
     ApplicationContext context = null; 
     context = new ClassPathXmlApplicationContext("application-context.xml"); 
     CommonsHttpSolrServer solrServer = (CommonsHttpSolrServer) context.getBean("solrServer"); 
     SolrQuery solrQuery = new SolrQuery().setQuery("*:*"); 

     QueryResponse rsp = solrServer.query(solrQuery); 

     //I am trying to use replicationhandler but I am not able to get the index size using statistics. Is there any way to get the index size..?       
     ReplicationHandler handler2 = new ReplicationHandler(); 
     System.out.println(handler2.getDescription()); 

     NamedList statistics = handler2.getStatistics(); 
     System.out.println("Statistics "+ statistics); 
     System.out.println(rsp.getResults().getNumFound()); 

     Iterator<SolrDocument> iter = rsp.getResults().iterator(); 

     while (iter.hasNext()) { 
         SolrDocument resultDoc = iter.next();   
         System.out.println(resultDoc.getFieldNames()); 
         String id = (String) resultDoc.getFieldValue("numFound"); 
         String description = (String) resultDoc.getFieldValue("description"); 
         System.out.println(id+"~~"+description); 
         out1.write(id+"~~"+description); 
         out1.newLine(); 
     } 
     out1.close(); 

    Any suggestions will be appreciated.. 

更新代碼: -

ReplicationHandler handler2 = new ReplicationHandler(); 
System.out.println(handler2.getDescription()); 
NamedList statistics = handler2.getStatistics(); 
System.out.println("Statistics "+ statistics.get("indexSize")); 
+0

我的建議是讓你的問題更清晰:)你已經提交了代碼,但沒有說你正在嘗試*做什麼或者實際做了什麼,或者你遇到了什麼問題。請閱讀http://tinyurl.com/so-hints –

+0

@JonSkeet,我已經更新了這些問題。讓我知道現在是否有意義.. – ferhan

+0

不是真的 - 你說你*有*得到索引大小,但是你「沒有得到」它......所以會發生什麼? –

回答

10

的indexsize是可用的統計ReplicationHandler

org.apache.solr.handler.ReplicationHandler 

代碼

public NamedList getStatistics() { 
    NamedList list = super.getStatistics(); 
    if (core != null) { 
     list.add("indexSize", NumberUtils.readableSize(getIndexSize())); 
    } 
    } 

您可以使用URL http://localhost:8983/solr/replication?command=details,它返回索引大小。

<lst name="details"> 
    <str name="indexSize">26.13 KB</str> 
    ..... 
</lst> 

不確定它是否適用於ReplicationHandler的實例化,因爲它需要核心和索引的引用。

+0

我該如何在我的代碼中使用它。我想我已經嘗試在我的上述代碼中執行此操作。 – ferhan

+0

嘗試statistics.get(「indexSize」); – Jayendra

+0

我已經更新了代碼,我沒有得到任何東西..! – ferhan

0

你可以在數據目錄

- du -kx 
+1

OP正在Java中尋求一種方法來實現這一點。 –

-1

this post說,你可以爲了看內存的消耗使用MAT tool使用的命令。我認爲你可以在你的代碼中使用。享受solr!