From 8aecefd623b0744485b5daa60b636dd88ac8d04a Mon Sep 17 00:00:00 2001 From: Erick Erickson Date: Fri, 24 Feb 2012 14:36:40 +0000 Subject: [PATCH] Fix for SOLR-3160. Thanks Luca Cavanna! git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1293270 13f79535-47bb-0310-9956-ffa450edef68 --- .../handler/component/StatsValuesFactory.java | 4 +- .../handler/component/StatsComponentTest.java | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) 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 882df8fd5e2..dcd46821f93 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 @@ -354,7 +354,9 @@ class DateStatsValues extends AbstractStatsValues { */ protected void addTypeSpecificStats(NamedList res) { res.add("sum", new Date(sum)); - res.add("mean", new Date(sum / count)); + if (count > 0) { + res.add("mean", new Date(sum / count)); + } } } diff --git a/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java index 08fdd8dd9e7..9805dd8309e 100644 --- a/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java +++ b/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java @@ -281,4 +281,62 @@ public class StatsComponentTest extends AbstractSolrTestCase { , "//lst[@name='false']/double[@name='stddev'][.='0.0']" ); } + + public void testFieldStatisticsResultsNumericFieldAlwaysMissing() throws Exception { + SolrCore core = h.getCore(); + assertU(adoc("id", "1")); + assertU(adoc("id", "2")); + assertU(adoc("id", "3")); + assertU(adoc("id", "4")); + assertU(commit()); + + Map args = new HashMap(); + args.put(CommonParams.Q, "*:*"); + args.put(StatsParams.STATS, "true"); + args.put(StatsParams.STATS_FIELD, "active_i"); + args.put("indent", "true"); + SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args)); + + assertQ("test string statistics values", req, + "//null[@name='active_i'][.='']"); + } + + public void testFieldStatisticsResultsStringFieldAlwaysMissing() throws Exception { + SolrCore core = h.getCore(); + assertU(adoc("id", "1")); + assertU(adoc("id", "2")); + assertU(adoc("id", "3")); + assertU(adoc("id", "4")); + assertU(commit()); + + Map args = new HashMap(); + args.put(CommonParams.Q, "*:*"); + args.put(StatsParams.STATS, "true"); + args.put(StatsParams.STATS_FIELD, "active_s"); + args.put("indent", "true"); + SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args)); + + assertQ("test string statistics values", req, + "//null[@name='active_s'][.='']"); + } + + //SOLR-3160 + public void testFieldStatisticsResultsDateFieldAlwaysMissing() throws Exception { + SolrCore core = h.getCore(); + + assertU(adoc("id", "1")); + assertU(adoc("id", "2")); + assertU(adoc("id", "3")); + assertU(commit()); + + Map args = new HashMap(); + args.put(CommonParams.Q, "*:*"); + args.put(StatsParams.STATS, "true"); + args.put(StatsParams.STATS_FIELD, "active_dt"); + args.put("indent", "true"); + SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args)); + + assertQ("test string statistics values", req, + "//null[@name='active_dt'][.='']"); + } }