Search code examples
lucenesolrj

solrj getCountDistinct() return null?


I am trying to get unique value of a field with the code:

query.set("q","*:*" );
query.setGetFieldStatistics(true);
query.setGetFieldStatistics("popu_s");

QueryResponse rsp = solr.query(query);

FieldStatsInfo stats = rsp.getFieldStatsInfo().get("popu_s"); 
System.out.println(stats.getCount());
System.out.println(stats.getCountDistinct());

stats.getCount() gives the correct count. However, stats.getCountDistinct() always returns null. Any idea?


Solution

  • Found the solution! To make getCountDistinct() return a value, one needs to add this

       query.addStatsFieldCalcDistinct("popu_s", true);