epotyom commented on code in PR #12966: URL: https://github.com/apache/lucene/pull/12966#discussion_r1457584536
########## lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java: ########## @@ -347,8 +347,8 @@ private TopChildrenForPath computeTopChildren( } reuse = q.insertWithOverflow(reuse); if (q.size() == topN) { - bottomCount = q.top().value; - bottomOrd = q.top().value; + bottomCount = (int) q.top().value; + bottomOrd = (int) q.top().value; Review Comment: I wonder if we can remove these `bottomX` optimizations here and in other places, I think `insertWithOverflow` essentially does the same? ########## 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: I believe using `Number` here and in LabelAndValue is one of the things that limits our ability to add new types of facet results, for example, multi-aggregate facets that you've mentioned. I'd suggest that we use generic `<T>` (which may need to implement Comparable?) in these classes, and use Integer, Float, etc in TaxonomyFacets implementation. This would require API changes though... ########## lucene/facet/src/java/org/apache/lucene/facet/TopOrdAndNumberQueue.java: ########## @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.facet; + +import org.apache.lucene.util.PriorityQueue; + +/** Keeps highest results, first by largest value, then tie-break by smallest ord. */ +public abstract class TopOrdAndNumberQueue extends PriorityQueue<TopOrdAndNumberQueue.OrdAndValue> { + + /** Holds a single entry. */ + public static final class OrdAndValue { Review Comment: Instead of making this class final and lessThan abstract, maybe we should make this class abstract, with abstract `compare` method which we implement separately for floats/ints/multi-aggregations? This way we can use primitive types in OrdAndValue implementations and hopefully reduce some boxing costs? -- 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