From 455ba2465f7396f16bfa6515ad7b846a11209b09 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 8 Mar 2012 19:25:30 +0000 Subject: [PATCH] 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 --- .../handler/component/StatsValuesFactory.java | 37 +++++++++++++++---- .../client/solrj/response/FieldStatsInfo.java | 24 ++++++------ .../solr/client/solrj/SolrExampleTests.java | 10 ++--- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java b/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java index dcd46821f93..7fb303d2a0d 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java +++ b/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java @@ -304,9 +304,8 @@ class NumericStatsValues extends AbstractStatsValues { */ class DateStatsValues extends AbstractStatsValues { - 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 { */ 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 { * @param res NamedList to add the type specific statistics too */ protected void addTypeSpecificStats(NamedList 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))); } } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/FieldStatsInfo.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/FieldStatsInfo.java index c32cd1cdd37..01ccbe9759f 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/response/FieldStatsInfo.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/FieldStatsInfo.java @@ -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 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; } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java index ab0383efa3f..49d92342d09 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java @@ -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() ); }