sigram commented on a change in pull request #1506:
URL: https://github.com/apache/lucene-solr/pull/1506#discussion_r429854601



##########
File path: 
solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/StatsStream.java
##########
@@ -266,91 +252,106 @@ public void close() throws IOException {
   }
 
   public Tuple read() throws IOException {
-    if(index == 0) {
-      ++index;
+    if(!done) {
+      done = true;
       return tuple;
     } else {
-      Map fields = new HashMap();
-      fields.put("EOF", true);
-      Tuple tuple = new Tuple(fields);
-      return tuple;
+      return Tuple.EOF();
     }
   }
 
-  private String getJsonFacetString(Metric[] _metrics) {
-    StringBuilder buf = new StringBuilder();
-    appendJson(buf, _metrics);
-    return "{"+buf.toString()+"}";
+  public StreamComparator getStreamSort() {
+    return null;
   }
 
-  private void appendJson(StringBuilder buf,
-                          Metric[] _metrics) {
-    
-    int metricCount = 0;
+  private void addStats(ModifiableSolrParams params, Metric[] _metrics) {
+    Map<String, List<String>> m = new HashMap<>();
     for(Metric metric : _metrics) {
-      String identifier = metric.getIdentifier();
-      if(!identifier.startsWith("count(")) {
-        if(metricCount>0) {
-          buf.append(",");
+      String metricId = metric.getIdentifier();
+      if(metricId.contains("(")) {
+        metricId = metricId.substring(0, metricId.length()-1);
+        String[] parts = metricId.split("\\(");
+        String function = parts[0];
+        String column = parts[1];
+        List<String> stats = m.get(column);
+
+        if(stats == null) {
+          stats = new ArrayList<>();
         }
-        if(identifier.startsWith("per(")) {
-          
buf.append("\"facet_").append(metricCount).append("\":\"").append(identifier.replaceFirst("per",
 "percentile")).append('"');
-        } else if(identifier.startsWith("std(")) {
-          
buf.append("\"facet_").append(metricCount).append("\":\"").append(identifier.replaceFirst("std",
 "stddev")).append('"');
-        } else {
-          
buf.append("\"facet_").append(metricCount).append("\":\"").append(identifier).append('"');
+
+        if(!column.equals("*")) {
+          m.put(column, stats);
+        }
+
+        if(function.equals("min")) {
+          stats.add("min");
+        } else if(function.equals("max")) {
+          stats.add("max");
+        } else if(function.equals("sum")) {
+          stats.add("sum");
+        } else if(function.equals("avg")) {
+          stats.add("mean");
+        } else if(function.equals("count")) {
+          this.doCount = true;
         }
-        ++metricCount;
       }
     }
-  }
 
-  private void getTuples(NamedList response,
-                         Metric[] metrics) {
+    for(Entry<String, List<String>> entry : m.entrySet()) {
+      StringBuilder buf = new StringBuilder();
+      List<String> stats = entry.getValue();
+      buf.append("{!");
+
+      for(String stat : stats) {
+        buf.append(stat).append("=").append("true ");
+      }
 
-    this.tuple = new Tuple(new HashMap());
-    NamedList facets = (NamedList)response.get("facets");
-    fillTuple(tuple, facets, metrics);
+      buf.append("}").append(entry.getKey());
+      params.add("stats.field", buf.toString());
+    }
   }
 
-  private void fillTuple(Tuple t,
-                         NamedList nl,
-                         Metric[] _metrics) {
+  private Tuple getTuple(NamedList response) {
+    Tuple tuple = new Tuple();
+    SolrDocumentList solrDocumentList = (SolrDocumentList) 
response.get("response");
+
+    long count = solrDocumentList.getNumFound();
 
-    if(nl == null) {
-      return;
+    if(doCount) {
+      tuple.put("count(*)", count);
     }
 
-    int m = 0;
-    for(Metric metric : _metrics) {
-      String identifier = metric.getIdentifier();
-      if(!identifier.startsWith("count(")) {
-        if(nl.get("facet_"+m) != null) {
-          Object d = nl.get("facet_" + m);
-          if(d instanceof Number) {
-            if (metric.outputLong) {
-              t.put(identifier, Math.round(((Number)d).doubleValue()));
-            } else {
-              t.put(identifier, ((Number)d).doubleValue());
-            }
-          } else {
-            t.put(identifier, d);
-          }
+    if(count != 0) {
+      NamedList stats = (NamedList)response.get("stats");
+      NamedList statsFields = (NamedList)stats.get("stats_fields");
+
+      for(int i=0; i<statsFields.size(); i++) {
+        String field = statsFields.getName(i);
+        NamedList theStats = (NamedList)statsFields.getVal(i);
+        for(int s=0; s<theStats.size(); s++) {
+          addStat(tuple, field, theStats.getName(s), theStats.getVal(s));
         }
-        ++m;
-      } else {
-        long l = ((Number)nl.get("count")).longValue();
-        t.put("count(*)", l);
       }
     }
+    return tuple;
   }
 
   public int getCost() {
     return 0;
   }
 
-  @Override
-  public StreamComparator getStreamSort() {
-    return null;
+  private void addStat(Tuple tuple, String field, String stat, Object val) {
+    if(stat.equals("mean")) {

Review comment:
       It's actually inherited ;) the original style has this all over the 
place.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to