SolrQuery query = new SolrQuery( "*:*" );
query.setRows( 0 );
query.setGetFieldStatistics( f );
QueryResponse rsp = server.query( query );
FieldStatsInfo stats = rsp.getFieldStatsInfo().get( f );
assertNotNull( stats );
assertEquals( 23.0, stats.getMin().doubleValue(), 0 );
assertEquals( 94.0, stats.getMax().doubleValue(), 0 );
assertEquals( new Long(nums.length), stats.getCount() );
assertEquals( new Long(0), stats.getMissing() );
assertEquals( "26.4", stats.getStddev().toString().substring(0,4) );
// now lets try again with a new set... (odd median)
//----------------------------------------------------
server.deleteByQuery( "*:*" );// delete everything!
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got in
nums = new int[] { 5, 7, 10, 19, 20 };
for( int num : nums ) {
SolrInputDocument doc = new SolrInputDocument();
doc.setField( "id", "doc"+i++ );
doc.setField( "name", "doc: "+num );
doc.setField( f, num );
server.add( doc );
}
server.commit();
assertNumFound( "*:*", nums.length ); // make sure they all got in
rsp = server.query( query );
stats = rsp.getFieldStatsInfo().get( f );
assertNotNull( stats );
assertEquals( 5.0, stats.getMin().doubleValue(), 0 );
assertEquals( 20.0, stats.getMax().doubleValue(), 0 );
assertEquals( new Long(nums.length), stats.getCount() );
assertEquals( new Long(0), stats.getMissing() );
// Now try again with faceting
//---------------------------------
server.deleteByQuery( "*:*" );// delete everything!
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got in
nums = new int[] { 1, 2, 3, 4, 5, 10, 11, 12, 13, 14 };
for( i=0; i<nums.length; i++ ) {
int num = nums[i];
SolrInputDocument doc = new SolrInputDocument();
doc.setField( "id", "doc"+i );
doc.setField( "name", "doc: "+num );
doc.setField( f, num );
doc.setField( "inStock", i < 5 );
server.add( doc );
}
server.commit();
assertNumFound( "inStock:true", 5 ); // make sure they all got in
assertNumFound( "inStock:false", 5 ); // make sure they all got in
// facet on 'inStock'
query.addStatsFieldFacets( f, "inStock" );
rsp = server.query( query );
stats = rsp.getFieldStatsInfo().get( f );
assertNotNull( stats );
List<FieldStatsInfo> facets = stats.getFacets().get( "inStock" );
assertNotNull( facets );
assertEquals( 2, facets.size() );
FieldStatsInfo inStockF = facets.get(0);
FieldStatsInfo inStockT = facets.get(1);
if( "true".equals( inStockF.getName() ) ) {
FieldStatsInfo tmp = inStockF;
inStockF = inStockT;
inStockT = tmp;
}
// make sure half went to each