You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2015/03/10 16:34:07 UTC

svn commit: r1665579 [1/2] - in /lucene/dev/trunk: lucene/ solr/ solr/core/ solr/core/src/java/org/apache/solr/handler/component/ solr/core/src/java/org/apache/solr/request/ solr/core/src/java/org/apache/solr/response/ solr/core/src/test-files/solr/col...

Author: hossman
Date: Tue Mar 10 15:34:06 2015
New Revision: 1665579

URL: http://svn.apache.org/r1665579
Log:
SOLR-6349 + SOLR-6682: Added support for stats.field localparams to enable/disable individual stats; Fix response when using EnumField with StatsComponent

Added:
    lucene/dev/trunk/solr/licenses/commons-math3-3.4.1.jar.sha1   (with props)
    lucene/dev/trunk/solr/licenses/commons-math3-LICENSE-ASL.txt   (with props)
    lucene/dev/trunk/solr/licenses/commons-math3-NOTICE.txt   (with props)
Modified:
    lucene/dev/trunk/lucene/ivy-versions.properties
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/core/ivy.xml
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetProcessor.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetValue.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsComponent.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsField.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/DocValuesStats.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/UnInvertedField.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java
    lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema.xml
    lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema11.xml
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/TestDistributedSearch.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallAdvancedTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/FacetPivotSmallTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/StatsComponentTest.java
    lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java

Modified: lucene/dev/trunk/lucene/ivy-versions.properties
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/ivy-versions.properties?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/ivy-versions.properties (original)
+++ lucene/dev/trunk/lucene/ivy-versions.properties Tue Mar 10 15:34:06 2015
@@ -87,6 +87,7 @@ com.sun.jersey.version = 1.9
 /org.apache.ant/ant = 1.8.2
 /org.apache.avro/avro = 1.7.5
 /org.apache.commons/commons-compress = 1.8.1
+/org.apache.commons/commons-math3 = 3.4.1
 /org.apache.derby/derby = 10.9.1.0
 /org.apache.directory.server/apacheds-all = 2.0.0-M15
 

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Tue Mar 10 15:34:06 2015
@@ -158,6 +158,12 @@ New Features
 
 * SOLR-7126: Secure loading of runtime external jars (Noble Paul)
 
+* SOLR-6349: Added support for stats.field localparams to enable/disable individual stats to 
+  limit the amount of computation done and the amount of data returned. 
+  eg: stats.field={!min=true max=true}field_name
+  (Tomas Fernandez-Lobbe, Xu Zhang, hossman)
+
+
 Bug Fixes
 ----------------------
 
@@ -221,6 +227,9 @@ Bug Fixes
   looking for start.jar.
   (Xu Zhang via Shawn Heisey)
 
+* SOLR-6682: Fix response when using EnumField with StatsComponent
+  (Xu Zhang via hossman)
+
 Optimizations
 ----------------------
 

Modified: lucene/dev/trunk/solr/core/ivy.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/ivy.xml?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/ivy.xml (original)
+++ lucene/dev/trunk/solr/core/ivy.xml Tue Mar 10 15:34:06 2015
@@ -37,6 +37,7 @@
     <dependency org="com.google.guava" name="guava" rev="${/com.google.guava/guava}" conf="compile"/>
     <dependency org="com.spatial4j" name="spatial4j" rev="${/com.spatial4j/spatial4j}" conf="compile"/>
     <dependency org="org.antlr" name="antlr-runtime" rev="${/org.antlr/antlr-runtime}" conf="compile"/>
+    <dependency org="org.apache.commons" name="commons-math3" rev="${/org.apache.commons/commons-math3}" conf="test"/>
     <dependency org="org.ow2.asm" name="asm" rev="${/org.ow2.asm/asm}" conf="compile"/>
     <dependency org="org.ow2.asm" name="asm-commons" rev="${/org.ow2.asm/asm-commons}" conf="compile"/>
     <dependency org="org.restlet.jee" name="org.restlet" rev="${/org.restlet.jee/org.restlet}" conf="compile"/>

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetProcessor.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetProcessor.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetProcessor.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetProcessor.java Tue Mar 10 15:34:06 2015
@@ -278,8 +278,7 @@ public class PivotFacetProcessor extends
           for (StatsField statsField : statsFields) {
             stv.put(statsField.getOutputKey(), statsField.computeLocalStatsValues(subset));
           }
-          // for pivots, we *always* include requested stats - even if 'empty'
-          pivot.add("stats", StatsComponent.convertToResponse(true, stv));
+          pivot.add("stats", StatsComponent.convertToResponse(stv));
         }
         values.add( pivot );
       }

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetValue.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetValue.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetValue.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/PivotFacetValue.java Tue Mar 10 15:34:06 2015
@@ -183,8 +183,7 @@ public class PivotFacetValue {
     }
     if (null != statsValues) {
       newList.add(PivotListEntry.STATS.getName(), 
-                  // for pivots, we *always* include requested stats - even if 'empty'
-                  StatsComponent.convertToResponse(true, statsValues));
+                  StatsComponent.convertToResponse(statsValues));
     }
     return newList;
   }      

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsComponent.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsComponent.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsComponent.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsComponent.java Tue Mar 10 15:34:06 2015
@@ -52,7 +52,7 @@ public class StatsComponent extends Sear
   @Override
   public void process(ResponseBuilder rb) throws IOException {
     if (!rb.doStats) return;
-
+    
     boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);
     Map<String, StatsValues> statsValues = new LinkedHashMap<>();
 
@@ -61,7 +61,7 @@ public class StatsComponent extends Sear
       statsValues.put(statsField.getOutputKey(), statsField.computeLocalStatsValues(docs));
     }
     
-    rb.rsp.add( "stats", convertToResponse(isShard, statsValues) );
+    rb.rsp.add( "stats", convertToResponse(statsValues) );
   }
 
   @Override
@@ -122,7 +122,7 @@ public class StatsComponent extends Sear
     // so that "result" is already stored in the response (for aesthetics)
 
     Map<String, StatsValues> allStatsValues = rb._statsInfo.getAggregateStatsValues();
-    rb.rsp.add("stats", convertToResponse(false, allStatsValues));
+    rb.rsp.add("stats", convertToResponse(allStatsValues));
 
     rb._statsInfo = null; // free some objects 
   }
@@ -142,7 +142,7 @@ public class StatsComponent extends Sear
    * including the esoteric "stats_fields" wrapper.
    */
   public static NamedList<NamedList<NamedList<?>>> convertToResponse
-    (boolean force, Map<String,StatsValues> statsValues) {
+    (Map<String,StatsValues> statsValues) {
 
     NamedList<NamedList<NamedList<?>>> stats = new SimpleOrderedMap<>();
     NamedList<NamedList<?>> stats_fields = new SimpleOrderedMap<>();
@@ -151,11 +151,7 @@ public class StatsComponent extends Sear
     for (Map.Entry<String,StatsValues> entry : statsValues.entrySet()) {
       String key = entry.getKey();
       NamedList stv = entry.getValue().getStatsValues();
-      if (force || ((Long) stv.get("count") != 0)) {
-        stats_fields.add(key, stv);
-      } else {
-        stats_fields.add(key, null);
-      }
+      stats_fields.add(key, stv);
     }
     return stats;
   }

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsField.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsField.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsField.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsField.java Tue Mar 10 15:34:06 2015
@@ -19,30 +19,32 @@ package org.apache.solr.handler.componen
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.lucene.index.LeafReaderContext;
-import org.apache.lucene.search.*;
 import org.apache.lucene.queries.function.FunctionQuery;
 import org.apache.lucene.queries.function.ValueSource;
-import org.apache.lucene.queries.function.valuesource.QueryValueSource;
 import org.apache.lucene.queries.function.valuesource.FieldCacheSource;
-
+import org.apache.lucene.queries.function.valuesource.QueryValueSource;
+import org.apache.lucene.search.Query;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrException.ErrorCode;
 import org.apache.solr.common.params.CommonParams;
-import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.params.StatsParams;
 import org.apache.solr.common.util.StrUtils;
-import org.apache.solr.request.SolrQueryRequest; // jdocs
 import org.apache.solr.request.DocValuesStats;
+import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.schema.IndexSchema;
 import org.apache.solr.schema.SchemaField;
 import org.apache.solr.search.DocIterator;
@@ -60,6 +62,74 @@ import org.apache.solr.search.SyntaxErro
  * @see StatsComponent
  */
 public class StatsField {
+  
+  /**
+   * An enumeration representing the sumer set of all possible stat values that can be computed.
+   * Each of these enum values can be specified as a local param in a <code>stats.field</code> 
+   * (eg: <code>stats.field={!min=true mean=true}my_field_name</code>) but not all enum values 
+   * are valid for all field types (eg: <code>mean</code> is meaningless for String fields)
+   *
+   * @lucene.internal
+   * @lucene.experimental
+   */
+  public static enum Stat {
+    min(true),
+    max(true),
+    missing(true),
+    sum(true),
+    count(true),
+    mean(false, sum, count),
+    sumOfSquares(true),
+    stddev(false, sum, count, sumOfSquares),
+    calcdistinct(true);
+    
+    private final List<Stat> distribDeps;
+    
+    /**
+     * Sole constructor for Stat enum values
+     * @param deps the set of stat values, other then this one, which are a distributed 
+     *        dependency and must be computed and returned by each individual shards in 
+     *        order to compute <i>this</i> stat over the entire distributed result set.
+     * @param selfDep indicates that when computing this stat across a distributed result 
+     *        set, each shard must compute this stat <i>in addition to</i> any other 
+     *        distributed dependences.
+     * @see #getDistribDeps
+     */
+    Stat(boolean selfDep, Stat... deps) {
+      distribDeps = new ArrayList<Stat>(deps.length+1);
+      distribDeps.addAll(Arrays.asList(deps));
+      if (selfDep) { 
+        distribDeps.add(this);
+      }
+    }
+    
+    /**
+     * Given a String, returns the corrisponding Stat enum value if any, otherwise returns null.
+     */
+    public static Stat forName(String paramKey) {
+      try {
+        return Stat.valueOf(paramKey);
+      } catch (IllegalArgumentException e) {
+        return null;
+      }
+    }
+    
+    /**
+     * The stats that must be computed and returned by each shard involved in a distributed 
+     * request in order to compute the overall value for this stat across the entire distributed 
+     * result set.  A Stat instance may include itself in the <code>getDistribDeps()</code> result,
+     * but that is not always the case.
+     */
+    public EnumSet<Stat> getDistribDeps() {
+      return EnumSet.copyOf(this.distribDeps);
+    }
+  }
+
+  /**
+   * The set of stats computed by default when no localparams are used to specify explicit stats 
+   */
+  public final static Set<Stat> DEFAULT_STATS = Collections.<Stat>unmodifiableSet
+    (EnumSet.of(Stat.min, Stat.max, Stat.missing, Stat.sum, Stat.count, Stat.mean, Stat.sumOfSquares, Stat.stddev));
 
   private final SolrIndexSearcher searcher;
   private final ResponseBuilder rb;
@@ -68,10 +138,13 @@ public class StatsField {
   private final ValueSource valueSource; // may be null if simple field stats
   private final SchemaField schemaField; // may be null if function/query stats
   private final String key;
-  private final boolean calcDistinct; // TODO: put this inside localParams ? SOLR-6349 ?
+  private final boolean  topLevelCalcDistinct;
   private final String[] facets;
   private final List<String> tagList;
   private final List<String> excludeTagList;
+  private final EnumSet<Stat> statsToCalculate = EnumSet.noneOf(Stat.class);
+  private final EnumSet<Stat> statsInResponse = EnumSet.noneOf(Stat.class);
+  private final boolean isShard;
 
   /**
    * @param rb the current request/response
@@ -84,6 +157,7 @@ public class StatsField {
 
     SolrParams params = rb.req.getParams();
     try {
+      isShard = params.getBool("isShard", false);
       SolrParams localParams = QueryParsing.getLocalParams(originalParam, params);
       if (null == localParams) {
         // simplest possible input: bare string (field name)
@@ -91,7 +165,9 @@ public class StatsField {
         customParams.add(QueryParsing.V, originalParam);
         localParams = customParams;
       }
+
       this.localParams = localParams;
+      
 
       String parserName = localParams.get(QueryParsing.TYPE);
       SchemaField sf = null;
@@ -141,11 +217,12 @@ public class StatsField {
                                                // default to entire original param str.
                                                originalParam));
 
-    
-    this.calcDistinct = null == schemaField
-      ? params.getBool(StatsParams.STATS_CALC_DISTINCT, false) 
-      : params.getFieldBool(schemaField.getName(), StatsParams.STATS_CALC_DISTINCT, false);
-
+    this.topLevelCalcDistinct = null == schemaField
+        ? params.getBool(StatsParams.STATS_CALC_DISTINCT, false) 
+        : params.getFieldBool(schemaField.getName(), StatsParams.STATS_CALC_DISTINCT, false);
+        
+    populateStatsSets();
+        
     String[] facets = params.getFieldParams(key, StatsParams.STATS_FACET);
     this.facets = (null == facets) ? new String[0] : facets;
     String tagStr = localParams.get(CommonParams.TAG);
@@ -360,15 +437,6 @@ public class StatsField {
     return valueSource;
   }
 
-  /**
-   * Whether or not the effective value of the {@link StatsParams#STATS_CALC_DISTINCT} param
-   * is true or false for this StatsField
-   */
-  public boolean getCalcDistinct() {
-    return calcDistinct;
-  }
-
-
   public List<String> getTagList() {
     return tagList;
   }
@@ -377,4 +445,59 @@ public class StatsField {
     return "StatsField<" + originalParam + ">";
   }
 
+
+  /**
+   * A helper method which inspects the {@link #localParams} associated with this StatsField, 
+   * and uses them to populate the {@link #statsInResponse} and {@link #statsToCalculate} data 
+   * structures
+   */
+  private void populateStatsSets() {
+    
+    boolean statSpecifiedByLocalParam = false;
+    // local individual stat
+    Iterator<String> itParams = localParams.getParameterNamesIterator();
+    while (itParams.hasNext()) {
+      String paramKey = itParams.next();
+        Stat stat = Stat.forName(paramKey);
+        if (stat != null) {
+          statSpecifiedByLocalParam = true;
+          // TODO: this isn't going to work for planned "non-boolean' stats - eg: SOLR-6350, SOLR-6968
+          if (localParams.getBool(paramKey, false)) {
+            statsInResponse.add(stat);
+            statsToCalculate.addAll(stat.getDistribDeps());
+          }
+        }
+    }
+    
+    // if no individual stat setting. 
+    if ( ! statSpecifiedByLocalParam ) {
+      statsInResponse.addAll(DEFAULT_STATS);
+      for (Stat stat : statsInResponse) {
+        statsToCalculate.addAll(stat.getDistribDeps());
+      }
+    }
+
+    // calcDistinct has special "default" behavior using top level CalcDistinct param
+    if (topLevelCalcDistinct && localParams.getBool(Stat.calcdistinct.toString(), true)) {
+      statsInResponse.add(Stat.calcdistinct);
+      statsToCalculate.addAll(Stat.calcdistinct.getDistribDeps());
+    }
+  }
+
+  public boolean calculateStats(Stat stat) {
+    return statsToCalculate.contains(stat);
+  }
+  
+  public boolean includeInResponse(Stat stat) {
+    if (isShard) {
+      return statsToCalculate.contains(stat);
+    }
+   
+    if (statsInResponse.contains(stat)) {
+      return true;
+    }
+    return false;
+  }
+
+
 }

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/StatsValuesFactory.java Tue Mar 10 15:34:06 2015
@@ -28,6 +28,7 @@ import org.apache.solr.common.EnumFieldV
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.handler.component.StatsField.Stat;
 import org.apache.solr.schema.*;
 
 /**
@@ -79,17 +80,19 @@ abstract class AbstractStatsValues<T> im
   /** Tracks all data about tthe stats we need to collect */
   final protected StatsField statsField;
 
-  /** 
-   * local copy to save method dispatch in tight loops 
-   * @see StatsField#getCalcDistinct
-   */
-  final protected boolean calcDistinct;
-
   /** may be null if we are collecting stats directly from a function ValueSource */
   final protected SchemaField sf;
   /** may be null if we are collecting stats directly from a function ValueSource */
   final protected FieldType ft;
 
+  // final booleans from StatsField to allow better inlining & JIT optimizing
+  final protected boolean computeCount;
+  final protected boolean computeMissing;
+  final protected boolean computeCalcDistinct;
+  final protected boolean computeMin;
+  final protected boolean computeMax;
+  final protected boolean computeMinOrMax;
+
   /** 
    * Either a function value source to collect from, or the ValueSource associated 
    * with a single valued field we are collecting from.  Will be null until/unless 
@@ -112,15 +115,21 @@ abstract class AbstractStatsValues<T> im
   protected long missing;
   protected long count;
   protected long countDistinct;
-  protected Set<T> distinctValues;
+  protected final Set<T> distinctValues;
   
   // facetField   facetValue
   protected Map<String, Map<String, StatsValues>> facets = new HashMap<>();
 
   protected AbstractStatsValues(StatsField statsField) {
     this.statsField = statsField;
-    this.calcDistinct = statsField.getCalcDistinct();
-    this.distinctValues = new TreeSet<>();
+    this.computeCount = statsField.calculateStats(Stat.count);
+    this.computeMissing = statsField.calculateStats(Stat.missing);
+    this.computeCalcDistinct = statsField.calculateStats(Stat.calcdistinct);
+    this.computeMin = statsField.calculateStats(Stat.min);
+    this.computeMax = statsField.calculateStats(Stat.max);
+    this.computeMinOrMax = computeMin || computeMax;
+      
+    this.distinctValues = computeCalcDistinct ? new TreeSet<>() : null;
 
     // alternatively, we could refactor a common base class that doesn't know/care
     // about either SchemaField or ValueSource - but then there would be a lot of
@@ -149,14 +158,20 @@ abstract class AbstractStatsValues<T> im
    */
   @Override
   public void accumulate(NamedList stv) {
-    count += (Long) stv.get("count");
-    missing += (Long) stv.get("missing");
-    if (calcDistinct) {
+    if (computeCount) {
+      count += (Long) stv.get("count");
+    }
+    if (computeMissing) {
+      missing += (Long) stv.get("missing");
+    }
+    if (computeCalcDistinct) {
       distinctValues.addAll((Collection<T>) stv.get("distinctValues"));
       countDistinct = distinctValues.size();
     }
-
-    updateMinMax((T) stv.get("min"), (T) stv.get("max"));
+    
+    if (computeMinOrMax) {
+      updateMinMax((T) stv.get("min"), (T) stv.get("max"));
+    }
     updateTypeSpecificStats(stv);
 
     NamedList f = (NamedList) stv.get(FACETS);
@@ -197,12 +212,16 @@ abstract class AbstractStatsValues<T> im
   }
 
   public void accumulate(T value, int count) { 
-    this.count += count;
-    if (calcDistinct) {
+    if (computeCount) {
+      this.count += count;
+    }
+    if (computeCalcDistinct) {
       distinctValues.add(value);
       countDistinct = distinctValues.size();
     }
-    updateMinMax(value, value);
+    if (computeMinOrMax) {
+      updateMinMax(value, value);
+    }
     updateTypeSpecificStats(value, count);
   }
 
@@ -211,7 +230,9 @@ abstract class AbstractStatsValues<T> im
    */
   @Override
   public void missing() {
-    missing++;
+    if (computeMissing) {
+      missing++;
+    }
   }
    
   /**
@@ -237,27 +258,39 @@ abstract class AbstractStatsValues<T> im
   public NamedList<?> getStatsValues() {
     NamedList<Object> res = new SimpleOrderedMap<>();
 
-    res.add("min", min);
-    res.add("max", max);
-    res.add("count", count);
-    res.add("missing", missing);
-    if (calcDistinct) {
+    if (statsField.includeInResponse(Stat.min)) {
+      res.add("min", min);
+    }
+    if (statsField.includeInResponse(Stat.max)) {
+      res.add("max", max);
+    }
+    if (statsField.includeInResponse(Stat.count)) {
+      res.add("count", count);
+    }
+    if (statsField.includeInResponse(Stat.missing)) {
+      res.add("missing", missing);
+    }
+    if (statsField.includeInResponse(Stat.calcdistinct)) {
       res.add("distinctValues", distinctValues);
       res.add("countDistinct", countDistinct);
     }
 
     addTypeSpecificStats(res);
+    
+    if (!facets.isEmpty()) {
 
-     // add the facet stats
-    NamedList<NamedList<?>> nl = new SimpleOrderedMap<>();
-    for (Map.Entry<String, Map<String, StatsValues>> entry : facets.entrySet()) {
-      NamedList<NamedList<?>> nl2 = new SimpleOrderedMap<>();
-      nl.add(entry.getKey(), nl2);
-      for (Map.Entry<String, StatsValues> e2 : entry.getValue().entrySet()) {
-        nl2.add(e2.getKey(), e2.getValue().getStatsValues());
-      }
+      // add the facet stats
+     NamedList<NamedList<?>> nl = new SimpleOrderedMap<>();
+     for (Map.Entry<String, Map<String, StatsValues>> entry : facets.entrySet()) {
+       NamedList<NamedList<?>> nl2 = new SimpleOrderedMap<>();
+       nl.add(entry.getKey(), nl2);
+       for (Map.Entry<String, StatsValues> e2 : entry.getValue().entrySet()) {
+         nl2.add(e2.getKey(), e2.getValue().getStatsValues());
+       }
+     }
+     res.add(FACETS, nl);
     }
-    res.add(FACETS, nl);
+
     return res;
   }
 
@@ -314,8 +347,13 @@ class NumericStatsValues extends Abstrac
   double sum;
   double sumOfSquares;
 
+  final protected boolean computeSum;
+  final protected boolean computeSumOfSquares;
+
   public NumericStatsValues(StatsField statsField) {
     super(statsField);
+    this.computeSum = statsField.calculateStats(Stat.sum);
+    this.computeSumOfSquares = statsField.calculateStats(Stat.sumOfSquares);
   }
 
   @Override
@@ -332,8 +370,12 @@ class NumericStatsValues extends Abstrac
    */
   @Override
   public void updateTypeSpecificStats(NamedList stv) {
-    sum += ((Number)stv.get("sum")).doubleValue();
-    sumOfSquares += ((Number)stv.get("sumOfSquares")).doubleValue();
+    if (computeSum) {
+      sum += ((Number)stv.get("sum")).doubleValue();
+    }
+    if (computeSumOfSquares) {
+      sumOfSquares += ((Number)stv.get("sumOfSquares")).doubleValue();
+    }
   }
 
   /**
@@ -342,8 +384,12 @@ class NumericStatsValues extends Abstrac
   @Override
   public void updateTypeSpecificStats(Number v, int count) {
     double value = v.doubleValue();
-    sumOfSquares += (value * value * count); // for std deviation
-    sum += value * count;
+    if (computeSumOfSquares) {
+      sumOfSquares += (value * value * count); // for std deviation
+    }
+    if (computeSum) {
+      sum += value * count;
+    }
   }
 
    /**
@@ -351,22 +397,23 @@ class NumericStatsValues extends Abstrac
    */
   @Override
   protected void updateMinMax(Number min, Number max) {
-    if (null == min) {
-      assert null == max : "min is null but max isn't ? ==> " + max;
-      return; // No-Op
-    }
-
-    assert null != max : "max is null but min isn't ? ==> " + min;
-
-    // we always use the double value, because that way the response Object class is 
+    // we always use the double values, because that way the response Object class is 
     // consistent regardless of whether we only have 1 value or many that we min/max
     //
     // TODO: would be nice to have subclasses for each type of Number ... breaks backcompat
-    double minD = min.doubleValue();
-    double maxD = max.doubleValue();
 
-    this.min = (null == this.min) ? minD : Math.min(this.min.doubleValue(), minD);
-    this.max = (null == this.max) ? maxD : Math.max(this.max.doubleValue(), maxD);
+    if (computeMin) { // nested if to encourage JIT to optimize aware final var?
+      if (null != min) {
+        double minD = min.doubleValue();
+        this.min = (null == this.min) ? minD : Math.min(this.min.doubleValue(), minD);
+      }
+    }
+    if (computeMax) { // nested if to encourage JIT to optimize aware final var?
+      if (null != max) {
+        double maxD = max.doubleValue();
+        this.max = (null == this.max) ? maxD : Math.max(this.max.doubleValue(), maxD);
+      }
+    }
   }
 
   /**
@@ -376,10 +423,18 @@ class NumericStatsValues extends Abstrac
    */
   @Override
   protected void addTypeSpecificStats(NamedList<Object> res) {
-    res.add("sum", sum);
-    res.add("sumOfSquares", sumOfSquares);
-    res.add("mean", sum / count);
-    res.add("stddev", getStandardDeviation());
+    if (statsField.includeInResponse(Stat.sum)) {
+      res.add("sum", sum);
+    }
+    if (statsField.includeInResponse(Stat.sumOfSquares)) {
+      res.add("sumOfSquares", sumOfSquares);
+    }
+    if (statsField.includeInResponse(Stat.mean)) {
+      res.add("mean", sum / count);
+    }
+    if (statsField.includeInResponse(Stat.stddev)) {
+      res.add("stddev", getStandardDeviation());
+    }
   }
 
   /**
@@ -424,14 +479,20 @@ class EnumStatsValues extends AbstractSt
    * {@inheritDoc}
    */
   protected void updateMinMax(EnumFieldValue min, EnumFieldValue max) {
-    if (max != null) {
-      if (max.compareTo(this.max) > 0)
-        this.max = max;
+    if (computeMin) { // nested if to encourage JIT to optimize aware final var?
+      if (null != min) {
+        if (null == this.min || (min.compareTo(this.min) < 0)) {
+          this.min = min;
+        }
+      }
+    }
+    if (computeMax) { // nested if to encourage JIT to optimize aware final var?
+      if (null != max) {
+        if (null == this.max || (max.compareTo(this.max) > 0)) {
+          this.max = max;
+        }
+      }
     }
-    if (this.min == null)
-      this.min = min;
-    else if (this.min.compareTo(min) > 0)
-      this.min = min;
   }
 
   /**
@@ -470,8 +531,13 @@ class DateStatsValues extends AbstractSt
   private long sum = 0;
   double sumOfSquares = 0;
 
+  final protected boolean computeSum;
+  final protected boolean computeSumOfSquares;
+
   public DateStatsValues(StatsField statsField) {
     super(statsField);
+    this.computeSum = statsField.calculateStats(Stat.sum);
+    this.computeSumOfSquares = statsField.calculateStats(Stat.sumOfSquares);
   }
 
   @Override
@@ -488,9 +554,10 @@ class DateStatsValues extends AbstractSt
    */
   @Override
   protected void updateTypeSpecificStats(NamedList stv) {
-    Date date = (Date) stv.get("sum");
-    if (date != null) {
-      sum += date.getTime();
+    if (computeSum) {
+      sum += ((Date) stv.get("sum")).getTime();
+    }
+    if (computeSumOfSquares) {
       sumOfSquares += ((Number)stv.get("sumOfSquares")).doubleValue();
     }
   }
@@ -501,8 +568,12 @@ class DateStatsValues extends AbstractSt
   @Override
   public void updateTypeSpecificStats(Date v, int count) {
     long value = v.getTime();
-    sumOfSquares += (value * value * count); // for std deviation
-    sum += value * count;
+    if (computeSumOfSquares) {
+      sumOfSquares += (value * value * count); // for std deviation
+    }
+    if (computeSum) {
+      sum += value * count;
+    }
   }
 
    /**
@@ -510,11 +581,15 @@ class DateStatsValues extends AbstractSt
    */
   @Override
   protected void updateMinMax(Date min, Date max) {
-    if(null != min && (this.min==null || this.min.after(min))) {
-      this.min = min;
+    if (computeMin) { // nested if to encourage JIT to optimize aware final var?
+      if (null != min && (this.min==null || this.min.after(min))) {
+        this.min = min;
+      }
     }
-    if(null != max && (this.max==null || this.max.before(max))) {
-      this.max = max;
+    if (computeMax) { // nested if to encourage JIT to optimize aware final var?
+      if (null != max && (this.max==null || this.max.before(max))) {
+        this.max = max;
+      }
     }
   }
 
@@ -525,15 +600,18 @@ class DateStatsValues extends AbstractSt
    */
   @Override
   protected void addTypeSpecificStats(NamedList<Object> res) {
-    if(sum<=0) {
-      return; // date==0 is meaningless
+    if (statsField.includeInResponse(Stat.sum)) {
+      res.add("sum", new Date(sum));
+    }
+    if (statsField.includeInResponse(Stat.mean)) {
+      res.add("mean", (count > 0) ? new Date(sum / count) : null);
     }
-    res.add("sum", new Date(sum));
-    if (count > 0) {
-      res.add("mean", new Date(sum / count));
+    if (statsField.includeInResponse(Stat.sumOfSquares)) {
+      res.add("sumOfSquares", sumOfSquares);
+    }
+    if (statsField.includeInResponse(Stat.stddev)) {
+      res.add("stddev", getStandardDeviation());
     }
-    res.add("sumOfSquares", sumOfSquares);
-    res.add("stddev", getStandardDeviation());
   }
   
 
@@ -594,8 +672,12 @@ class StringStatsValues extends Abstract
    */
   @Override
   protected void updateMinMax(String min, String max) {
-    this.max = max(this.max, max);
-    this.min = min(this.min, min);
+    if (computeMin) { // nested if to encourage JIT to optimize aware final var?
+      this.min = min(this.min, min);
+    }
+    if (computeMax) { // nested if to encourage JIT to optimize aware final var?
+      this.max = max(this.max, max);
+    }
   }
 
   /**

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/DocValuesStats.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/DocValuesStats.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/DocValuesStats.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/DocValuesStats.java Tue Mar 10 15:34:06 2015
@@ -56,7 +56,6 @@ public class DocValuesStats {
   public static StatsValues getCounts(SolrIndexSearcher searcher, StatsField statsField, DocSet docs, String[] facet) throws IOException {
 
     final SchemaField schemaField = statsField.getSchemaField(); 
-    final boolean calcDistinct = statsField.getCalcDistinct();
 
     assert null != statsField.getSchemaField()
       : "DocValuesStats requires a StatsField using a SchemaField";

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/UnInvertedField.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/UnInvertedField.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/UnInvertedField.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/request/UnInvertedField.java Tue Mar 10 15:34:06 2015
@@ -23,35 +23,17 @@ import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.lucene.index.LeafReader;
-import org.apache.lucene.index.SortedDocValues;
-import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermsEnum;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TermRangeQuery;
 import org.apache.lucene.uninverting.DocTermOrds;
 import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.BytesRefBuilder;
 import org.apache.lucene.util.CharsRefBuilder;
-import org.apache.lucene.util.FixedBitSet;
-import org.apache.lucene.util.UnicodeUtil;
 import org.apache.solr.common.SolrException;
-import org.apache.solr.common.params.FacetParams;
-import org.apache.solr.common.util.NamedList;
 import org.apache.solr.core.SolrCore;
-import org.apache.solr.handler.component.FieldFacetStats;
-import org.apache.solr.handler.component.StatsField;
-import org.apache.solr.handler.component.StatsValues;
-import org.apache.solr.handler.component.StatsValuesFactory;
 import org.apache.solr.schema.FieldType;
-import org.apache.solr.schema.SchemaField;
 import org.apache.solr.schema.TrieField;
-import org.apache.solr.search.BitDocSet;
-import org.apache.solr.search.DocIterator;
 import org.apache.solr.search.DocSet;
 import org.apache.solr.search.SolrCache;
 import org.apache.solr.search.SolrIndexSearcher;
-import org.apache.solr.util.LongPriorityQueue;
-import org.apache.solr.util.PrimUtils;
 
 /**
  *
@@ -84,7 +66,6 @@ import org.apache.solr.util.PrimUtils;
  *
  */
 public class UnInvertedField extends DocTermOrds {
-  private static int TNUM_OFFSET=2;
 
   static class TopTerm {
     BytesRef term;
@@ -214,420 +195,6 @@ public class UnInvertedField extends Doc
     return numTermsInField;
   }
 
-  public NamedList<Integer> getCounts(SolrIndexSearcher searcher, DocSet baseDocs, int offset, int limit, Integer mincount, boolean missing, String sort, String prefix) throws IOException {
-    use.incrementAndGet();
-
-    FieldType ft = searcher.getSchema().getFieldType(field);
-
-    NamedList<Integer> res = new NamedList<>();  // order is important
-
-    DocSet docs = baseDocs;
-    int baseSize = docs.size();
-    int maxDoc = searcher.maxDoc();
-
-    //System.out.println("GET COUNTS field=" + field + " baseSize=" + baseSize + " minCount=" + mincount + " maxDoc=" + maxDoc + " numTermsInField=" + numTermsInField);
-    if (baseSize >= mincount) {
-
-      final int[] index = this.index;
-      // tricky: we add more more element than we need because we will reuse this array later
-      // for ordering term ords before converting to term labels.
-      final int[] counts = new int[numTermsInField + 1];
-
-      //
-      // If there is prefix, find its start and end term numbers
-      //
-      int startTerm = 0;
-      int endTerm = numTermsInField;  // one past the end
-
-      TermsEnum te = getOrdTermsEnum(searcher.getLeafReader());
-      if (te != null && prefix != null && prefix.length() > 0) {
-        final BytesRefBuilder prefixBr = new BytesRefBuilder();
-        prefixBr.copyChars(prefix);
-        if (te.seekCeil(prefixBr.get()) == TermsEnum.SeekStatus.END) {
-          startTerm = numTermsInField;
-        } else {
-          startTerm = (int) te.ord();
-        }
-        prefixBr.append(UnicodeUtil.BIG_TERM);
-        if (te.seekCeil(prefixBr.get()) == TermsEnum.SeekStatus.END) {
-          endTerm = numTermsInField;
-        } else {
-          endTerm = (int) te.ord();
-        }
-      }
-
-      /***********
-      // Alternative 2: get the docSet of the prefix (could take a while) and
-      // then do the intersection with the baseDocSet first.
-      if (prefix != null && prefix.length() > 0) {
-        docs = searcher.getDocSet(new ConstantScorePrefixQuery(new Term(field, ft.toInternal(prefix))), docs);
-        // The issue with this method are problems of returning 0 counts for terms w/o
-        // the prefix.  We can't just filter out those terms later because it may
-        // mean that we didn't collect enough terms in the queue (in the sorted case).
-      }
-      ***********/
-
-      boolean doNegative = baseSize > maxDoc >> 1 && termInstances > 0
-              && startTerm==0 && endTerm==numTermsInField
-              && docs instanceof BitDocSet;
-
-      if (doNegative) {
-        FixedBitSet bs = ((BitDocSet)docs).getBits().clone();
-        bs.flip(0, maxDoc);
-        // TODO: when iterator across negative elements is available, use that
-        // instead of creating a new bitset and inverting.
-        docs = new BitDocSet(bs, maxDoc - baseSize);
-        // simply negating will mean that we have deleted docs in the set.
-        // that should be OK, as their entries in our table should be empty.
-        //System.out.println("  NEG");
-      }
-
-      // For the biggest terms, do straight set intersections
-      for (TopTerm tt : bigTerms.values()) {
-        //System.out.println("  do big termNum=" + tt.termNum + " term=" + tt.term.utf8ToString());
-        // TODO: counts could be deferred if sorted==false
-        if (tt.termNum >= startTerm && tt.termNum < endTerm) {
-          counts[tt.termNum] = searcher.numDocs(new TermQuery(new Term(field, tt.term)), docs);
-          //System.out.println("    count=" + counts[tt.termNum]);
-        } else {
-          //System.out.println("SKIP term=" + tt.termNum);
-        }
-      }
-
-      // TODO: we could short-circuit counting altogether for sorted faceting
-      // where we already have enough terms from the bigTerms
-
-      // TODO: we could shrink the size of the collection array, and
-      // additionally break when the termNumber got above endTerm, but
-      // it would require two extra conditionals in the inner loop (although
-      // they would be predictable for the non-prefix case).
-      // Perhaps a different copy of the code would be warranted.
-
-      if (termInstances > 0) {
-        DocIterator iter = docs.iterator();
-        while (iter.hasNext()) {
-          int doc = iter.nextDoc();
-          //System.out.println("iter doc=" + doc);
-          int code = index[doc];
-
-          if ((code & 0xff)==1) {
-            //System.out.println("  ptr");
-            int pos = code>>>8;
-            int whichArray = (doc >>> 16) & 0xff;
-            byte[] arr = tnums[whichArray];
-            int tnum = 0;
-            for(;;) {
-              int delta = 0;
-              for(;;) {
-                byte b = arr[pos++];
-                delta = (delta << 7) | (b & 0x7f);
-                if ((b & 0x80) == 0) break;
-              }
-              if (delta == 0) break;
-              tnum += delta - TNUM_OFFSET;
-              //System.out.println("    tnum=" + tnum);
-              counts[tnum]++;
-            }
-          } else {
-            //System.out.println("  inlined");
-            int tnum = 0;
-            int delta = 0;
-            for (;;) {
-              delta = (delta << 7) | (code & 0x7f);
-              if ((code & 0x80)==0) {
-                if (delta==0) break;
-                tnum += delta - TNUM_OFFSET;
-                //System.out.println("    tnum=" + tnum);
-                counts[tnum]++;
-                delta = 0;
-              }
-              code >>>= 8;
-            }
-          }
-        }
-      }
-      final CharsRefBuilder charsRef = new CharsRefBuilder();
-
-      int off=offset;
-      int lim=limit>=0 ? limit : Integer.MAX_VALUE;
-
-      if (sort.equals(FacetParams.FACET_SORT_COUNT) || sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY)) {
-        int maxsize = limit>0 ? offset+limit : Integer.MAX_VALUE-1;
-        maxsize = Math.min(maxsize, numTermsInField);
-        LongPriorityQueue queue = new LongPriorityQueue(Math.min(maxsize,1000), maxsize, Long.MIN_VALUE);
-
-        int min=mincount-1;  // the smallest value in the top 'N' values
-        //System.out.println("START=" + startTerm + " END=" + endTerm);
-        for (int i=startTerm; i<endTerm; i++) {
-          int c = doNegative ? maxTermCounts[i] - counts[i] : counts[i];
-          if (c>min) {
-            // NOTE: we use c>min rather than c>=min as an optimization because we are going in
-            // index order, so we already know that the keys are ordered.  This can be very
-            // important if a lot of the counts are repeated (like zero counts would be).
-
-            // smaller term numbers sort higher, so subtract the term number instead
-            long pair = (((long)c)<<32) + (Integer.MAX_VALUE - i);
-            boolean displaced = queue.insert(pair);
-            if (displaced) min=(int)(queue.top() >>> 32);
-          }
-        }
-
-        // now select the right page from the results
-
-        // if we are deep paging, we don't have to order the highest "offset" counts.
-        int collectCount = Math.max(0, queue.size() - off);
-        assert collectCount <= lim;
-
-        // the start and end indexes of our list "sorted" (starting with the highest value)
-        int sortedIdxStart = queue.size() - (collectCount - 1);
-        int sortedIdxEnd = queue.size() + 1;
-        final long[] sorted = queue.sort(collectCount);
-
-        final int[] indirect = counts;  // reuse the counts array for the index into the tnums array
-        assert indirect.length >= sortedIdxEnd;
-
-        for (int i=sortedIdxStart; i<sortedIdxEnd; i++) {
-          long pair = sorted[i];
-          int c = (int)(pair >>> 32);
-          int tnum = Integer.MAX_VALUE - (int)pair;
-
-          indirect[i] = i;   // store the index for indirect sorting
-          sorted[i] = tnum;  // reuse the "sorted" array to store the term numbers for indirect sorting
-
-          // add a null label for now... we'll fill it in later.
-          res.add(null, c);
-        }
-
-        // now sort the indexes by the term numbers
-        PrimUtils.sort(sortedIdxStart, sortedIdxEnd, indirect, new PrimUtils.IntComparator() {
-          @Override
-          public int compare(int a, int b) {
-            return (int)sorted[a] - (int)sorted[b];
-          }
-
-          @Override
-          public boolean lessThan(int a, int b) {
-            return sorted[a] < sorted[b];
-          }
-
-          @Override
-          public boolean equals(int a, int b) {
-            return sorted[a] == sorted[b];
-          }
-        });
-
-        // convert the term numbers to term values and set
-        // as the label
-        //System.out.println("sortStart=" + sortedIdxStart + " end=" + sortedIdxEnd);
-        for (int i=sortedIdxStart; i<sortedIdxEnd; i++) {
-          int idx = indirect[i];
-          int tnum = (int)sorted[idx];
-          final String label = getReadableValue(getTermValue(te, tnum), ft, charsRef);
-          //System.out.println("  label=" + label);
-          res.setName(idx - sortedIdxStart, label);
-        }
-
-      } else {
-        // add results in index order
-        int i=startTerm;
-        if (mincount<=0) {
-          // if mincount<=0, then we won't discard any terms and we know exactly
-          // where to start.
-          i=startTerm+off;
-          off=0;
-        }
-
-        for (; i<endTerm; i++) {
-          int c = doNegative ? maxTermCounts[i] - counts[i] : counts[i];
-          if (c<mincount || --off>=0) continue;
-          if (--lim<0) break;
-
-          final String label = getReadableValue(getTermValue(te, i), ft, charsRef);
-          res.add(label, c);
-        }
-      }
-    }
-
-
-    if (missing) {
-      // TODO: a faster solution for this?
-      res.add(null, SimpleFacets.getFieldMissingCount(searcher, baseDocs, field));
-    }
-
-    //System.out.println("  res=" + res);
-
-    return res;
-  }
-
-  /**
-   * Collect statistics about the UninvertedField.  Code is very similar to {@link #getCounts(org.apache.solr.search.SolrIndexSearcher, org.apache.solr.search.DocSet, int, int, Integer, boolean, String, String)}
-   * It can be used to calculate stats on multivalued fields.
-   * <p>
-   * This method is mainly used by the {@link org.apache.solr.handler.component.StatsComponent}.
-   *
-   * @param searcher The Searcher to use to gather the statistics
-   * @param baseDocs The {@link org.apache.solr.search.DocSet} to gather the stats on
-   * @param statsField the {@link StatsField} param corrisponding to a real {@link SchemaField} to compute stats over
-   * @param facet One or more fields to facet on.
-   * @return The {@link org.apache.solr.handler.component.StatsValues} collected
-   * @throws IOException If there is a low-level I/O error.
-   */
-  public StatsValues getStats(SolrIndexSearcher searcher, DocSet baseDocs, StatsField statsField, String[] facet) throws IOException {
-    //this function is ripped off nearly wholesale from the getCounts function to use
-    //for multiValued fields within the StatsComponent.  may be useful to find common
-    //functionality between the two and refactor code somewhat
-    use.incrementAndGet();
-
-    assert null != statsField.getSchemaField()
-      : "DocValuesStats requires a StatsField using a SchemaField";
-
-    SchemaField sf = statsField.getSchemaField();
-    // FieldType ft = sf.getType();
-
-    StatsValues allstats = StatsValuesFactory.createStatsValues(statsField);
-
-    DocSet docs = baseDocs;
-    int baseSize = docs.size();
-    int maxDoc = searcher.maxDoc();
-
-    if (baseSize <= 0) return allstats;
-
-    DocSet missing = docs.andNot( searcher.getDocSet(new TermRangeQuery(field, null, null, false, false)) );
-
-    int i = 0;
-    final FieldFacetStats[] finfo = new FieldFacetStats[facet.length];
-    //Initialize facetstats, if facets have been passed in
-    SortedDocValues si;
-    for (String f : facet) {
-      SchemaField facet_sf = searcher.getSchema().getField(f);
-      finfo[i] = new FieldFacetStats(searcher, facet_sf, statsField);
-      i++;
-    }
-
-    final int[] index = this.index;
-    final int[] counts = new int[numTermsInField];//keep track of the number of times we see each word in the field for all the documents in the docset
-
-    TermsEnum te = getOrdTermsEnum(searcher.getLeafReader());
-
-    boolean doNegative = false;
-    if (finfo.length == 0) {
-      //if we're collecting statistics with a facet field, can't do inverted counting
-      doNegative = baseSize > maxDoc >> 1 && termInstances > 0
-              && docs instanceof BitDocSet;
-    }
-
-    if (doNegative) {
-      FixedBitSet bs = ((BitDocSet) docs).getBits().clone();
-      bs.flip(0, maxDoc);
-      // TODO: when iterator across negative elements is available, use that
-      // instead of creating a new bitset and inverting.
-      docs = new BitDocSet(bs, maxDoc - baseSize);
-      // simply negating will mean that we have deleted docs in the set.
-      // that should be OK, as their entries in our table should be empty.
-    }
-
-    // For the biggest terms, do straight set intersections
-    for (TopTerm tt : bigTerms.values()) {
-      // TODO: counts could be deferred if sorted==false
-      if (tt.termNum >= 0 && tt.termNum < numTermsInField) {
-        final Term t = new Term(field, tt.term);
-        if (finfo.length == 0) {
-          counts[tt.termNum] = searcher.numDocs(new TermQuery(t), docs);
-        } else {
-          //COULD BE VERY SLOW
-          //if we're collecting stats for facet fields, we need to iterate on all matching documents
-          DocSet bigTermDocSet = searcher.getDocSet(new TermQuery(t)).intersection(docs);
-          DocIterator iter = bigTermDocSet.iterator();
-          while (iter.hasNext()) {
-            int doc = iter.nextDoc();
-            counts[tt.termNum]++;
-            for (FieldFacetStats f : finfo) {
-              f.facetTermNum(doc, tt.termNum);
-            }
-          }
-        }
-      }
-    }
-
-
-    if (termInstances > 0) {
-      DocIterator iter = docs.iterator();
-      while (iter.hasNext()) {
-        int doc = iter.nextDoc();
-        int code = index[doc];
-
-        if ((code & 0xff) == 1) {
-          int pos = code >>> 8;
-          int whichArray = (doc >>> 16) & 0xff;
-          byte[] arr = tnums[whichArray];
-          int tnum = 0;
-          for (; ;) {
-            int delta = 0;
-            for (; ;) {
-              byte b = arr[pos++];
-              delta = (delta << 7) | (b & 0x7f);
-              if ((b & 0x80) == 0) break;
-            }
-            if (delta == 0) break;
-            tnum += delta - TNUM_OFFSET;
-            counts[tnum]++;
-            for (FieldFacetStats f : finfo) {
-              f.facetTermNum(doc, tnum);
-            }
-          }
-        } else {
-          int tnum = 0;
-          int delta = 0;
-          for (; ;) {
-            delta = (delta << 7) | (code & 0x7f);
-            if ((code & 0x80) == 0) {
-              if (delta == 0) break;
-              tnum += delta - TNUM_OFFSET;
-              counts[tnum]++;
-              for (FieldFacetStats f : finfo) {
-                f.facetTermNum(doc, tnum);
-              }
-              delta = 0;
-            }
-            code >>>= 8;
-          }
-        }
-      }
-    }
-    
-    // add results in index order
-    for (i = 0; i < numTermsInField; i++) {
-      int c = doNegative ? maxTermCounts[i] - counts[i] : counts[i];
-      if (c == 0) continue;
-      BytesRef value = getTermValue(te, i);
-
-      allstats.accumulate(value, c);
-      //as we've parsed the termnum into a value, lets also accumulate fieldfacet statistics
-      for (FieldFacetStats f : finfo) {
-        f.accumulateTermNum(i, value);
-      }
-    }
-
-    int c = missing.size();
-    allstats.addMissing(c);
-
-    if (finfo.length > 0) {
-      for (FieldFacetStats f : finfo) {
-        Map<String, StatsValues> facetStatsValues = f.facetStatsValues;
-        FieldType facetType = searcher.getSchema().getFieldType(f.name);
-        for (Map.Entry<String,StatsValues> entry : facetStatsValues.entrySet()) {
-          String termLabel = entry.getKey();
-          int missingCount = searcher.numDocs(new TermQuery(new Term(f.name, facetType.toInternal(termLabel))), missing);
-          entry.getValue().addMissing(missingCount);
-        }
-        allstats.addFacet(f.name, facetStatsValues);
-      }
-    }
-
-    return allstats;
-
-  }
-
   String getReadableValue(BytesRef termval, FieldType ft, CharsRefBuilder charsRef) {
     return ft.indexedToReadable(termval, charsRef).toString();
   }

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/response/TextResponseWriter.java Tue Mar 10 15:34:06 2015
@@ -21,13 +21,12 @@ import java.io.IOException;
 import java.io.Writer;
 import java.util.*;
 
-import org.apache.lucene.document.Document;
-import org.apache.lucene.index.IndexableField;
 import org.apache.solr.client.solrj.io.TupleStream;
 import org.apache.solr.client.solrj.io.Tuple;
 import org.apache.lucene.index.StorableField;
 import org.apache.lucene.index.StoredDocument;
 import org.apache.lucene.util.BytesRef;
+import org.apache.solr.common.EnumFieldValue;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrDocumentList;
 import org.apache.solr.common.util.Base64;
@@ -202,6 +201,8 @@ public abstract class TextResponseWriter
     } else if (val instanceof BytesRef) {
       BytesRef arr = (BytesRef)val;
       writeByteArr(name, arr.bytes, arr.offset, arr.length);
+    } else if (val instanceof EnumFieldValue) {
+      writeStr(name, val.toString(), true);
     } else {
       // default... for debugging only
       writeStr(name, val.getClass().getName() + ':' + val.toString(), true);

Modified: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema.xml?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema.xml (original)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema.xml Tue Mar 10 15:34:06 2015
@@ -410,6 +410,7 @@
       <tokenizer class="solr.WhitespaceTokenizerFactory" />
     </analyzer>
   </fieldType>
+  <fieldType name="severityType" class="solr.EnumField" enumsConfig="enumsConfig.xml" enumName="severity"/>
 
 </types>
 
@@ -548,6 +549,10 @@
 
    <field name="payloadDelimited" type="payloadDelimited" />
 
+   <!-- EnumType -->
+   <field name="severity" type="severityType" indexed="true" stored="true" multiValued="false"/>
+
+
    <!-- Dynamic field definitions.  If a field name is not found, dynamicFields
         will be used if the name matches any of the patterns.
         RESTRICTION: the glob-like pattern in the name attribute must have
@@ -620,6 +625,7 @@
    <dynamicField name="*_path"  type="path" indexed="true" stored="true" omitNorms="true" multiValued="true" />
    <dynamicField name="*_ancestor"  type="ancestor_path" indexed="true" stored="true" omitNorms="true" multiValued="true" />
 
+   <dynamicField name="*_sev_enum" type="severityType" indexed="true" stored="true" docValues="true" multiValued="true" />
  </fields>
 
  <defaultSearchField>text</defaultSearchField>

Modified: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema11.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema11.xml?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema11.xml (original)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema11.xml Tue Mar 10 15:34:06 2015
@@ -283,6 +283,9 @@ valued. -->
     </fieldType>  
 
 
+    <!-- EnumType -->
+    <fieldType name="severityType" class="solr.EnumField" enumsConfig="enumsConfig.xml" enumName="severity"/>
+
    <!-- Valid attributes for fields:
      name: mandatory - the name for the field
      type: mandatory - the name of a previously defined type from the <types> section
@@ -322,6 +325,10 @@ valued. -->
    <field name="cat_floatDocValues" type="float"  indexed="true" stored="true" docValues="true" multiValued="true" />
    <field name="cat_length" type="text_length" indexed="true" stored="true" multiValued="true"/>
 
+
+   <!-- EnumType -->
+   <field name="severity" type="severityType" indexed="true" stored="true" multiValued="false"/>
+
    <!-- Dynamic field definitions.  If a field name is not found, dynamicFields
         will be used if the name matches any of the patterns.
         RESTRICTION: the glob-like pattern in the name attribute must have

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/TestDistributedSearch.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/TestDistributedSearch.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/TestDistributedSearch.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/TestDistributedSearch.java Tue Mar 10 15:34:06 2015
@@ -19,26 +19,41 @@ package org.apache.solr;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.lucene.util.LuceneTestCase.Slow;
+
 import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrResponse;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.embedded.JettySolrRunner;
 import org.apache.solr.client.solrj.impl.HttpSolrClient;
 import org.apache.solr.client.solrj.response.FacetField;
 import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.client.solrj.response.RangeFacet;
+import org.apache.solr.client.solrj.response.FieldStatsInfo;
 import org.apache.solr.cloud.ChaosMonkey;
+import org.apache.solr.common.EnumFieldValue;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.params.ShardParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.params.StatsParams;
 import org.apache.solr.common.util.NamedList;
+import org.apache.solr.handler.component.ShardResponse;
+import org.apache.solr.handler.component.ShardRequest;
+import org.apache.solr.handler.component.StatsField.Stat;
+import org.apache.solr.handler.component.TrackingShardHandlerFactory;
+import org.apache.solr.handler.component.TrackingShardHandlerFactory.ShardRequestAndParams;
+import org.apache.solr.handler.component.TrackingShardHandlerFactory.RequestTrackingQueue;
+import org.apache.solr.handler.component.StatsComponentTest.StatSetCombinations;
 import org.junit.Test;
 
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.EnumSet;
 
 /**
  * TODO? perhaps use:
@@ -63,18 +78,25 @@ public class TestDistributedSearch exten
   String missingField="ignore_exception__missing_but_valid_field_t";
   String invalidField="ignore_exception__invalid_field_not_in_schema";
 
+  @Override
+  protected String getSolrXml() {
+    return "solr-trackingshardhandler.xml";
+  }
+
   @Test
   public void test() throws Exception {
     QueryResponse rsp = null;
     int backupStress = stress; // make a copy so we can restore
 
-
     del("*:*");
     indexr(id,1, i1, 100, tlong, 100,t1,"now is the time for all good men",
+           "foo_sev_enum", "Medium",
            tdate_a, "2010-04-20T11:00:00Z",
            tdate_b, "2009-08-20T11:00:00Z",
            "foo_f", 1.414f, "foo_b", "true", "foo_d", 1.414d);
     indexr(id,2, i1, 50 , tlong, 50,t1,"to come to the aid of their country.",
+           "foo_sev_enum", "Medium",
+           "foo_sev_enum", "High",
            tdate_a, "2010-05-02T11:00:00Z",
            tdate_b, "2009-11-02T11:00:00Z");
     indexr(id,3, i1, 2, tlong, 2,t1,"how now brown cow",
@@ -90,6 +112,7 @@ public class TestDistributedSearch exten
     indexr(id,7, i1, 123, tlong, 123 ,t1,"humpty dumpy had a great fall");
     indexr(id,8, i1, 876, tlong, 876,
            tdate_b, "2010-01-05T11:00:00Z",
+           "foo_sev_enum", "High",
            t1,"all the kings horses and all the kings men");
     indexr(id,9, i1, 7, tlong, 7,t1,"couldn't put humpty together again");
 
@@ -97,6 +120,7 @@ public class TestDistributedSearch exten
 
     indexr(id,10, i1, 4321, tlong, 4321,t1,"this too shall pass");
     indexr(id,11, i1, -987, tlong, 987,
+           "foo_sev_enum", "Medium",
            t1,"An eye for eye only ends up making the whole world blind.");
     indexr(id,12, i1, 379, tlong, 379,
            t1,"Great works are performed, not by strength, but by perseverance.");
@@ -363,7 +387,7 @@ public class TestDistributedSearch exten
     query("q","*:*", "rows",100, "facet","true", "facet.field",missingField, "facet.mincount",2);
     // test field that is valid in schema and missing in some shards
     query("q","*:*", "rows",100, "facet","true", "facet.field",oddField, "facet.mincount",2);
-
+    
     query("q","*:*", "sort",i1+" desc", "stats", "true", "stats.field", "stats_dt");
     query("q","*:*", "sort",i1+" desc", "stats", "true", "stats.field", i1);
     query("q","*:*", "sort",i1+" desc", "stats", "true", "stats.field", tdate_a);
@@ -392,6 +416,283 @@ public class TestDistributedSearch exten
           "stats.field", i1,
           "stats.field", tdate_a,
           "stats.field", tdate_b);
+    
+    // only ask for "min" and "mean", explicitly exclude deps of mean, whitebox check shard responses
+    try {
+      RequestTrackingQueue trackingQueue = new RequestTrackingQueue();
+      TrackingShardHandlerFactory.setTrackingQueue(jettys, trackingQueue);
+
+      rsp = query("q","*:*", "sort",i1+" desc", "stats", "true",
+                  "stats.field", "{!min=true sum=false mean=true count=false}" + i1);
+      FieldStatsInfo s = rsp.getFieldStatsInfo().get(i1);
+      assertNotNull("no stats for " + i1, s);
+      //
+      assertEquals("wrong min", -987.0D, (Double)s.getMin(), 0.0001D );
+      assertEquals("wrong mean", 377.153846D, (Double)s.getMean(), 0.0001D );
+      //
+      assertNull("expected null for count", s.getCount());
+      assertNull("expected null for calcDistinct", s.getCountDistinct());
+      assertNull("expected null for distinct vals", s.getDistinctValues());
+      assertNull("expected null for max", s.getMax());
+      assertNull("expected null for missing", s.getMissing());
+      assertNull("expected null for stddev", s.getStddev());
+      assertNull("expected null for sum", s.getSum());
+
+      // sanity check deps relationship
+      for (Stat dep : EnumSet.of(Stat.sum, Stat.count)) {
+        assertTrue("Purpose of this test is to ensure that asking for some stats works even when the deps " +
+                   "of those stats are explicitly excluded -- but the expected dep relationshp is no longer valid. " +
+                   "ie: who changed the code and didn't change this test?, expected: " + dep,
+                   Stat.mean.getDistribDeps().contains(dep));
+      }
+
+      // check our shard requests & responses - ensure we didn't get unneccessary stats from every shard
+      int numStatsShardRequests = 0;
+      EnumSet<Stat> shardStatsExpected = EnumSet.of(Stat.min, Stat.sum, Stat.count);
+      for (List<ShardRequestAndParams> shard : trackingQueue.getAllRequests().values()) {
+        for (ShardRequestAndParams shardReq : shard) {
+          if (shardReq.params.getBool(StatsParams.STATS, false)) {
+            numStatsShardRequests++;
+            for (ShardResponse shardRsp : shardReq.sreq.responses) {
+              NamedList<Object> shardStats = 
+                ((NamedList<NamedList<NamedList<Object>>>)
+                 shardRsp.getSolrResponse().getResponse().get("stats")).get("stats_fields").get(i1);
+
+              assertNotNull("no stard stats for " + i1, shardStats);
+              //
+              for (Map.Entry<String,Object> entry : shardStats) {
+                Stat found = Stat.forName(entry.getKey());
+                assertNotNull("found shardRsp stat key we were not expecting: " + entry, found);
+                assertTrue("found stat we were not expecting: " + entry, shardStatsExpected.contains(found));
+                
+              }
+            }
+          }
+        }
+      }
+      assertTrue("did't see any stats=true shard requests", 0 < numStatsShardRequests);
+    } finally {
+      TrackingShardHandlerFactory.setTrackingQueue(jettys, null);
+    }
+    
+    // only ask for "min", "mean" and "stddev",
+    rsp = query("q","*:*", "sort",i1+" desc", "stats", "true",
+                "stats.field", "{!min=true mean=true stddev=true}" + i1);
+    { // don't leak variables 
+      FieldStatsInfo s = rsp.getFieldStatsInfo().get(i1);
+      assertNotNull("no stats for " + i1, s);
+      //
+      assertEquals("wrong min", -987.0D, (Double)s.getMin(), 0.0001D );
+      assertEquals("wrong mean", 377.153846D, (Double)s.getMean(), 0.0001D );
+      assertEquals("wrong stddev", 1271.76215D, (Double)s.getStddev(), 0.0001D );
+      //
+      assertNull("expected null for count", s.getCount());
+      assertNull("expected null for calcDistinct", s.getCountDistinct());
+      assertNull("expected null for distinct vals", s.getDistinctValues());
+      assertNull("expected null for max", s.getMax());
+      assertNull("expected null for missing", s.getMissing());
+      assertNull("expected null for sum", s.getSum());
+    }
+
+    final String[] stats = new String[] {
+      "min", "max", "sum", "sumOfSquares", "stddev", "mean", "missing", "count"
+    };
+    
+    // ask for arbitrary pairs of stats
+    for (String stat1 : stats) {
+      for (String stat2 : stats) {
+        // NOTE: stat1 might equal stat2 - good edge case to test for
+
+        rsp = query("q","*:*", "sort",i1+" desc", "stats", "true",
+                    "stats.field", "{!" + stat1 + "=true " + stat2 + "=true}" + i1);
+
+        final List<String> statsExpected = new ArrayList<String>(2);
+        statsExpected.add(stat1);
+        if ( ! stat1.equals(stat2) ) {
+          statsExpected.add(stat2);
+        }
+
+        // ignore the FieldStatsInfo convinience class, and look directly at the NamedList
+        // so we don't need any sort of crazy reflection
+        NamedList<Object> svals = 
+          ((NamedList<NamedList<NamedList<Object>>>)
+           rsp.getResponse().get("stats")).get("stats_fields").get(i1);
+
+        assertNotNull("no stats for field " + i1, svals);
+        assertEquals("wrong quantity of stats", statsExpected.size(), svals.size());
+
+        
+        for (String s : statsExpected) {
+          assertNotNull("stat shouldn't be null: " + s, svals.get(s));
+          assertTrue("stat should be a Number: " + s + " -> " + svals.get(s).getClass(),
+                     svals.get(s) instanceof Number);
+          // some loose assertions since we're iterating over various stats
+          if (svals.get(s) instanceof Double) {
+            Double val = (Double) svals.get(s);
+            assertFalse("stat shouldn't be NaN: " + s, val.isNaN());
+            assertFalse("stat shouldn't be Inf: " + s, val.isInfinite());
+            assertFalse("stat shouldn't be 0: " + s, val.equals(0.0D));
+          } else {
+            // count or missing
+            assertTrue("stat should be count of missing: " + s,
+                       ("count".equals(s) || "missing".equals(s)));
+            assertTrue("stat should be a Long: " + s + " -> " + svals.get(s).getClass(),
+                       svals.get(s) instanceof Long);
+            Long val = (Long) svals.get(s);
+            assertFalse("stat shouldn't be 0: " + s, val.equals(0L));
+          }
+        }
+      }
+    }
+    
+    // all of these diff ways of asking for min & calcdistinct should have the same result
+    for (SolrParams p : new SolrParams[] {
+        params("stats.field", "{!min=true calcdistinct=true}" + i1),
+        params("stats.calcdistinct", "true",
+               "stats.field", "{!min=true}" + i1),
+        params("f."+i1+".stats.calcdistinct", "true",
+               "stats.field", "{!min=true}" + i1),
+        params("stats.calcdistinct", "false",
+               "f."+i1+".stats.calcdistinct", "true",
+               "stats.field", "{!min=true}" + i1),
+        params("stats.calcdistinct", "false",
+               "f."+i1+".stats.calcdistinct", "false",
+               "stats.field", "{!min=true calcdistinct=true}" + i1),
+      }) {
+      
+      rsp = query(SolrParams.wrapDefaults
+                  (p, params("q","*:*", "sort",i1+" desc", "stats", "true")));
+      FieldStatsInfo s = rsp.getFieldStatsInfo().get(i1);
+      assertNotNull(p+" no stats for " + i1, s);
+      //
+      assertEquals(p+" wrong min", -987.0D, (Double)s.getMin(), 0.0001D );
+      assertEquals(p+" wrong calcDistinct", new Long(13), s.getCountDistinct());
+      assertNotNull(p+" expected non-null list for distinct vals", s.getDistinctValues());
+      assertEquals(p+" expected list for distinct vals", 13, s.getDistinctValues().size());
+      //
+      assertNull(p+" expected null for mean", s.getMean() );
+      assertNull(p+" expected null for count", s.getCount());
+      assertNull(p+" expected null for max", s.getMax());
+      assertNull(p+" expected null for missing", s.getMissing());
+      assertNull(p+" expected null for stddev", s.getStddev());
+      assertNull(p+" expected null for sum", s.getSum());
+      
+    }
+
+    // all of these diff ways of excluding calcdistinct should have the same result
+    for (SolrParams p : new SolrParams[] {
+        params("stats.field", "{!min=true calcdistinct=false}" + i1),
+        params("stats.calcdistinct", "false",
+               "stats.field", "{!min=true}" + i1),
+        params("f."+i1+".stats.calcdistinct", "false",
+               "stats.field", "{!min=true}" + i1),
+        params("stats.calcdistinct", "true",
+               "f."+i1+".stats.calcdistinct", "false",
+               "stats.field", "{!min=true}" + i1),
+        params("stats.calcdistinct", "true",
+               "f."+i1+".stats.calcdistinct", "true",
+               "stats.field", "{!min=true calcdistinct=false}" + i1),
+      }) {
+      
+      rsp = query(SolrParams.wrapDefaults
+                  (p, params("q","*:*", "sort",i1+" desc", "stats", "true")));
+      FieldStatsInfo s = rsp.getFieldStatsInfo().get(i1);
+      assertNotNull(p+" no stats for " + i1, s);
+      //
+      assertEquals(p+" wrong min", -987.0D, (Double)s.getMin(), 0.0001D );
+      //
+      assertNull(p+" expected null for calcDistinct", s.getCountDistinct());
+      assertNull(p+" expected null for distinct vals", s.getDistinctValues());
+      //
+      assertNull(p+" expected null for mean", s.getMean() );
+      assertNull(p+" expected null for count", s.getCount());
+      assertNull(p+" expected null for max", s.getMax());
+      assertNull(p+" expected null for missing", s.getMissing());
+      assertNull(p+" expected null for stddev", s.getStddev());
+      assertNull(p+" expected null for sum", s.getSum());
+      
+    }
+
+    // this field doesn't exist in any doc in the result set.
+    // ensure we get expected values for the stats we ask for, but null for the stats
+    rsp = query("q","*:*", "sort",i1+" desc", "stats", "true",
+                "stats.field", "{!min=true mean=true stddev=true}does_not_exist_i");
+    { // don't leak variables 
+      FieldStatsInfo s = rsp.getFieldStatsInfo().get("does_not_exist_i");
+      assertNotNull("no stats for bogus field", s);
+
+      // things we explicit expect because we asked for them
+      // NOTE: min is expected to be null even though requested because of no values
+      assertEquals("wrong min", null, s.getMin()); 
+      assertTrue("mean should be NaN", ((Double)s.getMean()).isNaN());
+      assertEquals("wrong stddev", 0.0D, (Double)s.getStddev(), 0.0D );
+
+      // things that we didn't ask for, so they better be null
+      assertNull("expected null for count", s.getCount());
+      assertNull("expected null for calcDistinct", s.getCountDistinct());
+      assertNull("expected null for distinct vals", s.getDistinctValues());
+      assertNull("expected null for max", s.getMax());
+      assertNull("expected null for missing", s.getMissing());
+      assertNull("expected null for sum", s.getSum());
+    }
+
+    // look at stats on non numeric fields
+    //
+    // not all stats are supported on every field type, so some of these permutations will 
+    // result in no stats being computed but this at least lets us sanity check that for each 
+    // of these field+stats(s) combinations we get consistent results between the distribted 
+    // request and the single node situation.
+    EnumSet<Stat> allStats = EnumSet.allOf(Stat.class);
+    int numTotalStatQueries = 0;
+    // don't go overboard, just do all permutations of 1 or 2 stat params, for each field & query
+    final int numStatParamsAtOnce = 2; 
+    for (int numParams = 1; numParams <= numStatParamsAtOnce; numParams++) {
+      for (EnumSet<Stat> set : new StatSetCombinations(numParams, allStats)) {
+
+        for (String field : new String[] {
+            "foo_f", i1, tlong, tdate_a, oddField, "foo_sev_enum",
+            // fields that no doc has any value in
+            "bogus___s", "bogus___f", "bogus___i", "bogus___tdt", "bogus___sev_enum"
+          }) {
+
+          for ( String q : new String[] {
+              "*:*",                         // all docs
+              "bogus___s:bogus",             // no docs
+              "id:" + random().nextInt(50 ), // 0 or 1 doc...
+              "id:" + random().nextInt(50 ), 
+              "id:" + random().nextInt(100), 
+              "id:" + random().nextInt(100), 
+              "id:" + random().nextInt(200) 
+            }) {
+
+            // EnumSets use natural ordering, we want to randomize the order of the params
+            List<Stat> combo = new ArrayList<Stat>(set);
+            Collections.shuffle(combo, random());
+            
+            StringBuilder paras = new StringBuilder("{!key=k ");
+            
+            for (Stat stat : combo) {
+              paras.append(stat + "=true ");
+            }
+            
+            paras.append("}").append(field);
+            numTotalStatQueries++;
+            rsp = query("q","*:*", "rows", "0", "stats", "true",
+                        "stats.field", paras.toString());
+            // simple assert, mostly relying on comparison with single shard
+            FieldStatsInfo s = rsp.getFieldStatsInfo().get("k");
+            assertNotNull(s);
+
+            // TODO: if we had a programatic way to determine what stats are supported 
+            // by what field types, we could make more confident asserts here.
+          }
+        }
+      }
+    }
+    assertEquals("Sanity check failed: either test broke, or test changed, or you adjusted Stat enum" + 
+                 " (adjust constant accordingly if intentional)",
+                 3465, numTotalStatQueries);
+
 
     /*** TODO: the failure may come back in "exception"
     try {
@@ -567,6 +868,25 @@ public class TestDistributedSearch exten
         throw e;
       }
     }
+    
+    String fieldName = "severity";
+    indexr("id", "1", fieldName, "Not Available");
+    indexr("id", "2", fieldName, "Low");
+    indexr("id", "3", fieldName, "Medium");
+    indexr("id", "4", fieldName, "High");
+    indexr("id", "5", fieldName, "Critical");
+    
+    commit();
+    
+    rsp = query("q", "*:*", "stats", "true", "stats.field", fieldName);
+    assertEquals(new EnumFieldValue(0, "Not Available"),
+                 rsp.getFieldStatsInfo().get(fieldName).getMin());
+    query("q", "*:*", "stats", "true", "stats.field", fieldName,  
+          StatsParams.STATS_CALC_DISTINCT, "true");
+    assertEquals(new EnumFieldValue(4, "Critical"),
+                 rsp.getFieldStatsInfo().get(fieldName).getMax());
+    query("q", "*:*", "stats", "true", "stats.field", fieldName, 
+          "stats.facet", fieldName);
   }
 
   protected void checkMinCountsField(List<FacetField.Count> counts, Object[] pairs) {

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/cloud/CollectionsAPIDistributedZkTest.java Tue Mar 10 15:34:06 2015
@@ -139,7 +139,8 @@ public class CollectionsAPIDistributedZk
       // for now, always upload the config and schema to the canonical names
       AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, getCloudSolrConfig(), "solrconfig.xml");
       AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, "schema.xml", "schema.xml");
-
+      AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, "enumsConfig.xml", "enumsConfig.xml");
+      
       AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, "solrconfig.snippet.randomindexconfig.xml");
       AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, "stopwords.txt");
       AbstractZkTestCase.putConfig("conf2", zkClient, solrhome, "protwords.txt");

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallAdvancedTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallAdvancedTest.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallAdvancedTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallAdvancedTest.java Tue Mar 10 15:34:06 2015
@@ -89,19 +89,23 @@ public class DistributedFacetPivotSmallA
     handle.put("maxScore", SKIPVAL);
 
     doTestDeepPivotStatsOnString();
-    doTestTopStatsWithRefinement();
+
+    doTestTopStatsWithRefinement(true);
+    doTestTopStatsWithRefinement(false);
   }
 
   /**
    * we need to ensure that stats never "overcount" the values from a single shard
    * even if we hit that shard with a refinement request 
    */
-  private void doTestTopStatsWithRefinement() throws Exception {
-    
-    
+  private void doTestTopStatsWithRefinement(final boolean allStats) throws Exception {
+
+    String stat_param = allStats ? 
+      "{!tag=s1}foo_i" : "{!tag=s1 min=true max=true count=true missing=true}foo_i";
+
     ModifiableSolrParams coreParams = params("q", "*:*", "rows", "0",
                                              "stats", "true",
-                                             "stats.field", "{!tag=s1}foo_i" );
+                                             "stats.field", stat_param );
     ModifiableSolrParams facetParams = new ModifiableSolrParams(coreParams);
     facetParams.add(params("facet", "true",
                            "facet.limit", "1",
@@ -128,10 +132,18 @@ public class DistributedFacetPivotSmallA
       assertEquals(msg, 91.0, fieldStatsInfo.getMax());
       assertEquals(msg, 10, (long) fieldStatsInfo.getCount());
       assertEquals(msg, 0, (long) fieldStatsInfo.getMissing());
-      assertEquals(msg, 248.0, fieldStatsInfo.getSum());
-      assertEquals(msg, 15294.0, fieldStatsInfo.getSumOfSquares(), 0.1E-7);
-      assertEquals(msg, 24.8, (double) fieldStatsInfo.getMean(), 0.1E-7);
-      assertEquals(msg, 31.87405772027709, fieldStatsInfo.getStddev(), 0.1E-7);
+
+      if (allStats) {
+        assertEquals(msg, 248.0, fieldStatsInfo.getSum());
+        assertEquals(msg, 15294.0, fieldStatsInfo.getSumOfSquares(), 0.1E-7);
+        assertEquals(msg, 24.8, (double) fieldStatsInfo.getMean(), 0.1E-7);
+        assertEquals(msg, 31.87405772027709, fieldStatsInfo.getStddev(), 0.1E-7);
+      } else {
+        assertNull(msg, fieldStatsInfo.getSum());
+        assertNull(msg, fieldStatsInfo.getSumOfSquares());
+        assertNull(msg, fieldStatsInfo.getMean());
+        assertNull(msg, fieldStatsInfo.getStddev());
+      }
 
       if (params.getBool("facet", false)) {
         // if this was a facet request, then the top pivot constraint and pivot 
@@ -156,6 +168,12 @@ public class DistributedFacetPivotSmallA
         assertEquals(4, (long) dublinMicrosoftStatsInfo.getCount());
         assertEquals(0, (long) dublinMicrosoftStatsInfo.getMissing());
         
+        if (! allStats) {
+          assertNull(msg, dublinMicrosoftStatsInfo.getSum());
+          assertNull(msg, dublinMicrosoftStatsInfo.getSumOfSquares());
+          assertNull(msg, dublinMicrosoftStatsInfo.getMean());
+          assertNull(msg, dublinMicrosoftStatsInfo.getStddev());
+        }
       }
     }
 

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallTest.java?rev=1665579&r1=1665578&r2=1665579&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/DistributedFacetPivotSmallTest.java Tue Mar 10 15:34:06 2015
@@ -334,16 +334,21 @@ public class DistributedFacetPivotSmallT
       }
     }
 
-    doTestDeepPivotStats();
+    doTestDeepPivotStats(false); // all price stats
+    doTestDeepPivotStats(true); // just the mean price stat
 
     doTestPivotStatsFromOneShard();
   }
 
-  private void doTestDeepPivotStats() throws Exception {
+  /**
+   * @param justMean - only the mean stat is requested/computed
+   */
+  private void doTestDeepPivotStats(boolean justMean) throws Exception {
     SolrParams params = params("q", "*:*", "rows", "0", 
                                "facet", "true", "stats", "true", 
                                "facet.pivot", "{!stats=s1}place_t,company_t", 
-                               "stats.field", "{!key=avg_price tag=s1}price_ti");
+                               "stats.field", ("{!key=avg_price tag=s1 "+
+                                               (justMean ? "mean=true" : "") +"}price_ti"));
     QueryResponse rsp = query(params);
 
     List<PivotField> placePivots = rsp.getFacetPivot().get("place_t,company_t");
@@ -357,15 +362,24 @@ public class DistributedFacetPivotSmallT
     assertEquals(4, microsoftPivotField.getCount());
 
     FieldStatsInfo dublinMicrosoftStatsInfo = microsoftPivotField.getFieldStatsInfo().get("avg_price");
-    assertEquals(15.0, dublinMicrosoftStatsInfo.getMin());
-    assertEquals(29.0, dublinMicrosoftStatsInfo.getMax());
-    assertEquals(3, (long) dublinMicrosoftStatsInfo.getCount());
-    assertEquals(1, (long) dublinMicrosoftStatsInfo.getMissing());
-    assertEquals(63.0, dublinMicrosoftStatsInfo.getSum());
-    assertEquals(1427.0, dublinMicrosoftStatsInfo.getSumOfSquares(), 0.1E-7);
     assertEquals(21.0, (double) dublinMicrosoftStatsInfo.getMean(), 0.1E-7);
-    assertEquals(7.211102550927978, dublinMicrosoftStatsInfo.getStddev(), 0.1E-7);
-
+    if (justMean) {
+      assertNull(dublinMicrosoftStatsInfo.getMin());
+      assertNull(dublinMicrosoftStatsInfo.getMax());
+      assertNull(dublinMicrosoftStatsInfo.getCount());
+      assertNull(dublinMicrosoftStatsInfo.getMissing());
+      assertNull(dublinMicrosoftStatsInfo.getSum());
+      assertNull(dublinMicrosoftStatsInfo.getSumOfSquares());
+      assertNull(dublinMicrosoftStatsInfo.getStddev());
+    } else {
+      assertEquals(15.0, dublinMicrosoftStatsInfo.getMin());
+      assertEquals(29.0, dublinMicrosoftStatsInfo.getMax());
+      assertEquals(3, (long) dublinMicrosoftStatsInfo.getCount());
+      assertEquals(1, (long) dublinMicrosoftStatsInfo.getMissing());
+      assertEquals(63.0, dublinMicrosoftStatsInfo.getSum());
+      assertEquals(1427.0, dublinMicrosoftStatsInfo.getSumOfSquares(), 0.1E-7);
+      assertEquals(7.211102550927978, dublinMicrosoftStatsInfo.getStddev(), 0.1E-7);
+    }
 
     PivotField cardiffPivotField = placePivots.get(2);
     assertEquals("cardiff", cardiffPivotField.getValue());
@@ -376,15 +390,24 @@ public class DistributedFacetPivotSmallT
     assertEquals(3, polecatPivotField.getCount());
 
     FieldStatsInfo cardiffPolecatStatsInfo = polecatPivotField.getFieldStatsInfo().get("avg_price");
-    assertEquals(15.0, cardiffPolecatStatsInfo.getMin());
-    assertEquals(39.0, cardiffPolecatStatsInfo.getMax());
-    assertEquals(2, (long) cardiffPolecatStatsInfo.getCount());
-    assertEquals(1, (long) cardiffPolecatStatsInfo.getMissing());
-    assertEquals(54.0, cardiffPolecatStatsInfo.getSum());
-    assertEquals(1746.0, cardiffPolecatStatsInfo.getSumOfSquares(), 0.1E-7);
     assertEquals(27.0, (double) cardiffPolecatStatsInfo.getMean(), 0.1E-7);
-    assertEquals(16.97056274847714, cardiffPolecatStatsInfo.getStddev(), 0.1E-7);
-
+    if (justMean) {
+      assertNull(cardiffPolecatStatsInfo.getMin());
+      assertNull(cardiffPolecatStatsInfo.getMax());
+      assertNull(cardiffPolecatStatsInfo.getCount());
+      assertNull(cardiffPolecatStatsInfo.getMissing());
+      assertNull(cardiffPolecatStatsInfo.getSum());
+      assertNull(cardiffPolecatStatsInfo.getSumOfSquares());
+      assertNull(cardiffPolecatStatsInfo.getStddev());
+    } else {
+      assertEquals(15.0, cardiffPolecatStatsInfo.getMin());
+      assertEquals(39.0, cardiffPolecatStatsInfo.getMax());
+      assertEquals(2, (long) cardiffPolecatStatsInfo.getCount());
+      assertEquals(1, (long) cardiffPolecatStatsInfo.getMissing());
+      assertEquals(54.0, cardiffPolecatStatsInfo.getSum());
+      assertEquals(1746.0, cardiffPolecatStatsInfo.getSumOfSquares(), 0.1E-7);
+      assertEquals(16.97056274847714, cardiffPolecatStatsInfo.getStddev(), 0.1E-7);
+    }
 
     PivotField krakowPivotField = placePivots.get(3);
     assertEquals("krakow", krakowPivotField.getValue());
@@ -395,14 +418,25 @@ public class DistributedFacetPivotSmallT
     assertEquals(1, fujitsuPivotField.getCount());
 
     FieldStatsInfo krakowFujitsuStatsInfo = fujitsuPivotField.getFieldStatsInfo().get("avg_price");
-    assertEquals(null, krakowFujitsuStatsInfo.getMin());
-    assertEquals(null, krakowFujitsuStatsInfo.getMax());
-    assertEquals(0, (long) krakowFujitsuStatsInfo.getCount());
-    assertEquals(1, (long) krakowFujitsuStatsInfo.getMissing());
-    assertEquals(0.0, krakowFujitsuStatsInfo.getSum());
-    assertEquals(0.0, krakowFujitsuStatsInfo.getSumOfSquares(), 0.1E-7);
     assertEquals(Double.NaN, (double) krakowFujitsuStatsInfo.getMean(), 0.1E-7);
-    assertEquals(0.0, krakowFujitsuStatsInfo.getStddev(), 0.1E-7);
+    if (justMean) {
+      assertNull(krakowFujitsuStatsInfo.getMin());
+      assertNull(krakowFujitsuStatsInfo.getMax());
+      assertNull(krakowFujitsuStatsInfo.getCount());
+      assertNull(krakowFujitsuStatsInfo.getMissing());
+      assertNull(krakowFujitsuStatsInfo.getSum());
+      assertNull(krakowFujitsuStatsInfo.getSumOfSquares());
+      assertNull(krakowFujitsuStatsInfo.getStddev());
+     } else {
+      assertEquals(null, krakowFujitsuStatsInfo.getMin());
+      assertEquals(null, krakowFujitsuStatsInfo.getMax());
+      assertEquals(0, (long) krakowFujitsuStatsInfo.getCount());
+      assertEquals(1, (long) krakowFujitsuStatsInfo.getMissing());
+      assertEquals(0.0, krakowFujitsuStatsInfo.getSum());
+      assertEquals(0.0, krakowFujitsuStatsInfo.getSumOfSquares(), 0.1E-7);
+      assertEquals(Double.NaN, (double) krakowFujitsuStatsInfo.getMean(), 0.1E-7);
+      assertEquals(0.0, krakowFujitsuStatsInfo.getStddev(), 0.1E-7);
+    }
   }
 
   // Useful to check for errors, orders lists and does toString() equality check