3
votes

I need to get total size of an index in Apache Solr using Java. The following code gets the total number of documents but I am looking for the size. And with the use of ReplicationHandler I was thinking that I can get the index size as told by someone here on this link.. http://lucene.472066.n3.nabble.com/cheking-the-size-of-the-index-using-solrj-API-s-td692686.html but I am not getting the index size.

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..

Update Code:-

ReplicationHandler handler2 = new ReplicationHandler();
System.out.println( handler2.getDescription()); 
 NamedList statistics = handler2.getStatistics();
 System.out.println("Statistics   "+ statistics.get("indexSize")); 
3
My suggestion is that you make your question clearer :) You've presented code, but not said what you're trying to do or what it actually does, or any problems you've been having. Please read tinyurl.com/so-hintsJon Skeet
@JonSkeet, I have updated the questions. let me know if it makes sense now..arsenal
Not really - you say you have got the index size, but that you're "not getting" it... so what happens?Jon Skeet
I have got the number of documents in the Solr.. I need the index size..!!arsenal
"And with the use of ReplicationHandler I was thinking that I can get the index size as told by someone here on this link" - and what was the result?Jon Skeet

3 Answers

12
votes

The indexsize is available with the statistics in ReplicationHandler

org.apache.solr.handler.ReplicationHandler

code

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

You can use the URL http://localhost:8983/solr/replication?command=details , which returns the index size.

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

Not sure if it works with the instantiation of ReplicationHandler, as it would need the reference of the core and the index.

0
votes

You can use the command in the data directory

  - du -kx
0
votes

as said in this post you can use MAT tool in order to see the memory consumption. I think that you could use in your code. Enjoy solr!