Shradha26 commented on code in PR #13568:
URL: https://github.com/apache/lucene/pull/13568#discussion_r1679664044


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/taxonomy/TaxonomyFacetsCutter.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.sandbox.facet.taxonomy;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.facet.taxonomy.FacetLabel;
+import org.apache.lucene.facet.taxonomy.ParallelTaxonomyArrays;
+import org.apache.lucene.facet.taxonomy.TaxonomyReader;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.sandbox.facet.abstracts.FacetCutter;
+import org.apache.lucene.sandbox.facet.abstracts.FacetLeafCutter;
+import org.apache.lucene.sandbox.facet.abstracts.FacetRollup;
+import org.apache.lucene.sandbox.facet.abstracts.OrdinalIterator;
+
+/**
+ * {@link FacetCutter} for SortedNumericDocValues field. TODO: can remove 
"Taxonomy" from the name,
+ * it works with other fields too? Cons: Less obvious for users what to use 
for taxonomy.
+ */
+public class TaxonomyFacetsCutter implements FacetCutter, FacetRollup {
+
+  private final FacetsConfig facetsConfig;
+  private final TaxonomyReader taxoReader;
+  private final String indexFieldName;
+
+  private ParallelTaxonomyArrays.IntArray children;
+  private ParallelTaxonomyArrays.IntArray siblings;
+
+  /** Create {@link FacetCutter} for taxonomy facets. */
+  public TaxonomyFacetsCutter(
+      String indexFieldName, FacetsConfig facetsConfig, TaxonomyReader 
taxoReader) {
+    this.facetsConfig = facetsConfig;
+    this.indexFieldName = indexFieldName;
+    this.taxoReader = taxoReader;
+  }
+
+  /**
+   * Returns int[] mapping each ordinal to its first child; this is a large 
array and is computed
+   * (and then saved) the first time this method is invoked.
+   */
+  ParallelTaxonomyArrays.IntArray getChildren() throws IOException {
+    if (children == null) {
+      children = taxoReader.getParallelTaxonomyArrays().children();
+    }
+    return children;
+  }
+
+  /**
+   * Returns int[] mapping each ordinal to its next sibling; this is a large 
array and is computed
+   * (and then saved) the first time this method is invoked.
+   */
+  ParallelTaxonomyArrays.IntArray getSiblings() throws IOException {
+    if (siblings == null) {
+      siblings = taxoReader.getParallelTaxonomyArrays().siblings();
+    }
+    return siblings;
+  }
+
+  @Override
+  public FacetLeafCutter createLeafCutter(LeafReaderContext context) throws 
IOException {
+    SortedNumericDocValues multiValued =
+        DocValues.getSortedNumeric(context.reader(), indexFieldName);
+    if (multiValued == null) {
+      return null; // TODO: oh not not null!
+    }
+    TaxonomyLeafFacetCutterMultiValue leafCutter =
+        new TaxonomyLeafFacetCutterMultiValue(multiValued);
+    return leafCutter;
+
+    // TODO: does unwrapping Single valued makes things any faster? We still 
need to wrap it into
+    // FacetLeafCutter
+    // NumericDocValues singleValued = DocValues.unwrapSingleton(multiValued);
+  }
+
+  @Override
+  public OrdinalIterator getDimOrdsToRollup() {
+    // Rollup any necessary dims:
+    Iterator<Map.Entry<String, FacetsConfig.DimConfig>> dimensions =
+        facetsConfig.getDimConfigs().entrySet().iterator();
+    return new OrdinalIterator() {
+      @Override
+      public int nextOrd() throws IOException {
+        while (dimensions.hasNext()) {
+          Map.Entry<String, FacetsConfig.DimConfig> ent = dimensions.next();
+          String dim = ent.getKey();
+          FacetsConfig.DimConfig ft = ent.getValue();
+          if (ft.hierarchical
+              && ft.multiValued == false
+              && ft.indexFieldName.equals(indexFieldName)) {
+            int dimRootOrd = taxoReader.getOrdinal(new FacetLabel(dim));

Review Comment:
   That's a nice idea!



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