如何使用Java获取Solr中的索引大小

时间:2011-10-28 19:37:18

标签: java solr

我需要使用Java获取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")); 

3 个答案:

答案 0 :(得分:12)

indexsize可用于ReplicationHandler

中的统计信息
org.apache.solr.handler.ReplicationHandler

代码

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

您可以使用返回索引大小的网址http://localhost:8983/solr/replication?command=details

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

不确定它是否适用于ReplicationHandler的实例化,因为它需要核心和索引的引用。

答案 1 :(得分:1)

您可以在数据目录中使用该命令

  - du -kx

答案 2 :(得分:0)

this post中所述,您可以使用MAT tool来查看内存消耗情况。我认为你可以在你的代码中使用。享受solr!