SOLR-3209: FieldStatsInfo returns Object rather then Double

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1298527 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2012-03-08 19:25:30 +00:00
parent f164629353
commit 455ba2465f
3 changed files with 47 additions and 24 deletions

View File

@ -304,9 +304,8 @@ class NumericStatsValues extends AbstractStatsValues<Number> {
*/
class DateStatsValues extends AbstractStatsValues<Date> {
private static final DateField DATE_FIELD = new DateField();
private long sum;
private long sum = -1;
double sumOfSquares = 0;
public DateStatsValues(SchemaField sf) {
super(sf);
@ -317,22 +316,27 @@ class DateStatsValues extends AbstractStatsValues<Date> {
*/
protected void updateTypeSpecificStats(NamedList stv) {
sum += ((Date) stv.get("sum")).getTime();
sumOfSquares += ((Number)stv.get("sumOfSquares")).doubleValue();
}
/**
* {@inheritDoc}
*/
@Override
public void updateTypeSpecificStats(Date value) {
sum += value.getTime();
public void updateTypeSpecificStats(Date v) {
long value = v.getTime();
sumOfSquares += (value * value); // for std deviation
sum += value;
}
/**
* {@inheritDoc}
*/
@Override
public void updateTypeSpecificStats(Date value, int count) {
sum += value.getTime() * count;
public void updateTypeSpecificStats(Date v, int count) {
long value = v.getTime();
sumOfSquares += (value * value * count); // for std deviation
sum += value * count;
}
/**
@ -353,10 +357,29 @@ class DateStatsValues extends AbstractStatsValues<Date> {
* @param res NamedList to add the type specific statistics too
*/
protected void addTypeSpecificStats(NamedList<Object> res) {
if(sum<=0) {
return; // date==0 is meaningless
}
res.add("sum", new Date(sum));
if (count > 0) {
res.add("mean", new Date(sum / count));
}
res.add("sumOfSquares", sumOfSquares);
res.add("stddev", getStandardDeviation());
}
/**
* Calculates the standard deviation. For dates, this is really the MS deviation
*
* @return Standard deviation statistic
*/
private double getStandardDeviation() {
if (count <= 1) {
return 0.0D;
}
return Math.sqrt(((count * sumOfSquares) - (sum * sum)) / (count * (count - 1.0D)));
}
}

View File

@ -33,12 +33,12 @@ import java.util.Map;
public class FieldStatsInfo implements Serializable {
final String name;
Double min;
Double max;
Double sum;
Object min;
Object max;
Object sum;
Long count;
Long missing;
Double mean = null;
Object mean = null;
Double sumOfSquares = null;
Double stddev = null;
@ -50,13 +50,13 @@ public class FieldStatsInfo implements Serializable {
for( Map.Entry<String, Object> entry : nl ) {
if( "min".equals( entry.getKey() ) ) {
min = (Double)entry.getValue();
min = entry.getValue();
}
else if( "max".equals( entry.getKey() ) ) {
max = (Double)entry.getValue();
max = entry.getValue();
}
else if( "sum".equals( entry.getKey() ) ) {
sum = (Double)entry.getValue();
sum = entry.getValue();
}
else if( "count".equals( entry.getKey() ) ) {
count = (Long)entry.getValue();
@ -65,7 +65,7 @@ public class FieldStatsInfo implements Serializable {
missing = (Long)entry.getValue();
}
else if( "mean".equals( entry.getKey() ) ) {
mean = (Double)entry.getValue();
mean = entry.getValue();
}
else if( "sumOfSquares".equals( entry.getKey() ) ) {
sumOfSquares = (Double)entry.getValue();
@ -129,15 +129,15 @@ public class FieldStatsInfo implements Serializable {
return name;
}
public Double getMin() {
public Object getMin() {
return min;
}
public Double getMax() {
public Object getMax() {
return max;
}
public Double getSum() {
public Object getSum() {
return sum;
}
@ -149,7 +149,7 @@ public class FieldStatsInfo implements Serializable {
return missing;
}
public Double getMean() {
public Object getMean() {
return mean;
}

View File

@ -711,8 +711,8 @@ abstract public class SolrExampleTests extends SolrJettyTestBase
FieldStatsInfo stats = rsp.getFieldStatsInfo().get( f );
assertNotNull( stats );
assertEquals( 23.0, stats.getMin().doubleValue(), 0 );
assertEquals( 94.0, stats.getMax().doubleValue(), 0 );
assertEquals( 23.0, ((Double)stats.getMin()).doubleValue(), 0 );
assertEquals( 94.0, ((Double)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) );
@ -737,8 +737,8 @@ abstract public class SolrExampleTests extends SolrJettyTestBase
stats = rsp.getFieldStatsInfo().get( f );
assertNotNull( stats );
assertEquals( 5.0, stats.getMin().doubleValue(), 0 );
assertEquals( 20.0, stats.getMax().doubleValue(), 0 );
assertEquals( 5.0, ((Double)stats.getMin()).doubleValue(), 0 );
assertEquals( 20.0, ((Double)stats.getMin()).doubleValue(), 0 );
assertEquals( new Long(nums.length), stats.getCount() );
assertEquals( new Long(0), stats.getMissing() );
@ -782,7 +782,7 @@ abstract public class SolrExampleTests extends SolrJettyTestBase
assertEquals( inStockF.getCount(), inStockT.getCount() );
assertEquals( stats.getCount().longValue(), inStockF.getCount()+inStockT.getCount() );
assertTrue( "check that min max faceted ok", inStockF.getMin() > inStockT.getMax() );
assertTrue( "check that min max faceted ok", ((Double)inStockF.getMin()).doubleValue() > ((Double)inStockF.getMax()).doubleValue() );
assertEquals( "they have the same distribution", inStockF.getStddev(), inStockT.getStddev() );
}