stefanvodita commented on code in PR #12966:
URL: https://github.com/apache/lucene/pull/12966#discussion_r1518795915


##########
lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyFacets.java:
##########
@@ -142,6 +249,301 @@ DimConfig verifyDim(String dim) {
     return dimConfig;
   }
 
+  /**
+   * Roll-up the aggregation values from {@code childOrdinal} to {@code 
ordinal}. Overrides should
+   * probably call this to update the counts. Overriding allows us to work 
with primitive types for
+   * the aggregation values, keeping aggregation efficient.
+   */
+  protected void updateValueFromRollup(int ordinal, int childOrdinal) throws 
IOException {
+    setCount(ordinal, getCount(ordinal) + rollup(childOrdinal));
+  }
+
+  /**
+   * Return a {@link TopOrdAndNumberQueue} of the appropriate type, i.e. a 
{@link TopOrdAndIntQueue}
+   * or a {@link org.apache.lucene.facet.TopOrdAndFloatQueue}.
+   */
+  protected TopOrdAndNumberQueue makeTopOrdAndNumberQueue(int topN) {
+    return new TopOrdAndIntQueue(Math.min(taxoReader.getSize(), topN));
+  }
+
+  // TODO: We don't need this if we're okay with having an integer -1 in the 
results even for float
+  // aggregations.
+  /** Return the value for a missing aggregation, i.e. {@code -1} or {@code 
-1f}. */
+  protected Number missingAggregationValue() {
+    return -1;
+  }
+
+  /** Rolls up any single-valued hierarchical dimensions. */
+  void rollup() throws IOException {
+    if (initialized == false) {
+      return;
+    }
+
+    // Rollup any necessary dims:
+    int[] children = null;
+    for (Map.Entry<String, FacetsConfig.DimConfig> ent : 
config.getDimConfigs().entrySet()) {
+      String dim = ent.getKey();
+      FacetsConfig.DimConfig ft = ent.getValue();
+      if (ft.hierarchical && ft.multiValued == false) {
+        int dimRootOrd = taxoReader.getOrdinal(new FacetLabel(dim));
+        // It can be -1 if this field was declared in the
+        // config but never indexed:
+        if (dimRootOrd > 0) {
+          if (children == null) {
+            // lazy init
+            children = getChildren();
+          }
+          updateValueFromRollup(dimRootOrd, children[dimRootOrd]);
+        }
+      }
+    }
+  }
+
+  private int rollup(int ord) throws IOException {
+    int[] children = getChildren();
+    int[] siblings = getSiblings();
+    int aggregatedValue = 0;
+    while (ord != TaxonomyReader.INVALID_ORDINAL) {
+      int currentValue = getCount(ord);
+      int newValue = currentValue + rollup(children[ord]);
+      setCount(ord, newValue);
+      aggregatedValue += getCount(ord);
+      ord = siblings[ord];
+    }
+    return aggregatedValue;
+  }
+
+  /**
+   * Create a FacetResult for the provided dim + path and intermediate 
results. Does the extra work
+   * of resolving ordinals -> labels, etc. Will return null if there are no 
children.
+   */
+  private FacetResult createFacetResult(
+      TopChildrenForPath topChildrenForPath, String dim, String... path) 
throws IOException {
+    // If the intermediate result is null or there are no children, we return 
null:
+    if (topChildrenForPath == null || topChildrenForPath.childCount == 0) {
+      return null;
+    }
+
+    TopOrdAndNumberQueue q = topChildrenForPath.childQueue;
+    assert q != null;
+
+    LabelAndValue[] labelValues = new LabelAndValue[q.size()];
+    int[] ordinals = new int[labelValues.length];
+    Number[] values = new Number[labelValues.length];

Review Comment:
   Maybe we can consider this separately? I hope we can avoid all API changes 
in this PR.



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

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