Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract stats component with solrj 4.9.0

I have a solr query like this

q=categories:cat1 OR categories:cat2&stats=true&stats.field=count&stats.facet=block_num

Basically, I want to get the sum(count) group by block num.

This query works on a browser. But with solrj, I could not access the stats fields from Response obj. I can do a response.getFieldStatsInfo(). But it is not what I want. Here is how I construct the query

    SolrQuery query = new SolrQuery(q);       
    query.add("stats", "true");
    query.add("stats.field", "count");
    query.add("stats.facet", "block_num");

With a debugger, I could see that the response has a private statsInfo object and it has the information I am looking for. But there is no api to access the object.

I would like to know if there is

  1. a better way to query a solr server other than solrj (curl? how do you parse the response?
  2. a better way to construct my query. maybe using group instead of stats?
  3. a way to access the hidden statsInfo object in the query response()? [it is so frustrated. I can see all the info I need in the private obj on my debugger!]

Thanks!

like image 532
E.A. Avatar asked Oct 01 '22 05:10

E.A.


1 Answers

It is possible to do something like this:

// Field names
private static final String FIELD_ONE = "field_one";
private static final String FIELD_TWO = "field_two";

// Initialized somewhere in constructor
private final HttpSolrServer solrClient;

public void printStats() throws SolrServerException {
    final SolrQuery query = new SolrQuery("*:*");
    query.setGetFieldStatistics(true);
    query.setGetFieldStatistics(FIELD_ONE);
    query.setGetFieldStatistics(FIELD_TWO);
    final QueryResponse response = solrClient.query(query);

    final FieldStatsInfo fieldOneStats = response.getFieldStatsInfo().get(FIELD_ONE); 
    System.out.println(fieldOneStats.getSum());
    System.out.println(fieldOneStats.getMin());
    System.out.println(fieldOneStats.getMax());

    final FieldStatsInfo fieldTwoStats = response.getFieldStatsInfo().get(FIELD_TWO);
    System.out.println(fieldTwoStats.getSum());
    System.out.println(fieldTwoStats.getMin());
    System.out.println(fieldTwoStats.getMax());
}
like image 141
Dmitry Y. Avatar answered Oct 06 '22 01:10

Dmitry Y.