You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2022/05/23 19:06:08 UTC

[GitHub] [lucene] Yuti-G commented on a diff in pull request #915: LUCENE-10585: Scrub copy/paste code in the facets module and attempt to simplify a bit

Yuti-G commented on code in PR #915:
URL: https://github.com/apache/lucene/pull/915#discussion_r879786369


##########
lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.sortedset;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.PrimitiveIterator;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.facet.FacetsConfig.DimConfig;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.facet.TopOrdAndIntQueue;
+import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState.DimTree;
+import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState.OrdRange;
+import org.apache.lucene.index.SortedSetDocValues;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.PriorityQueue;
+
+/** Base class for SSDV faceting implementations. */
+abstract class AbstractSortedSetDocValueFacetCounts extends Facets {
+
+  private static final Comparator<FacetResult> FACET_RESULT_COMPARATOR =
+      new Comparator<>() {
+        @Override
+        public int compare(FacetResult a, FacetResult b) {
+          if (a.value.intValue() > b.value.intValue()) {
+            return -1;
+          } else if (b.value.intValue() > a.value.intValue()) {
+            return 1;
+          } else {
+            return a.dim.compareTo(b.dim);
+          }
+        }
+      };
+
+  final SortedSetDocValuesReaderState state;
+  final FacetsConfig stateConfig;
+  final SortedSetDocValues dv;
+  final String field;
+
+  AbstractSortedSetDocValueFacetCounts(SortedSetDocValuesReaderState state) throws IOException {
+    this.state = state;
+    this.field = state.getField();
+    this.stateConfig = state.getFacetsConfig();
+    this.dv = state.getDocValues();
+  }
+
+  @Override
+  public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
+    validateTopN(topN);
+    TopChildrenForPath topChildrenForPath = getTopChildrenForPath(topN, dim, path);
+    return createFacetResult(topChildrenForPath, dim, path);
+  }
+
+  @Override
+  public Number getSpecificValue(String dim, String... path) throws IOException {
+    if (path.length != 1) {
+      throw new IllegalArgumentException("path must be length=1");
+    }
+    int ord = (int) dv.lookupTerm(new BytesRef(FacetsConfig.pathToString(dim, path)));
+    if (ord < 0) {
+      return -1;
+    }
+
+    return getCount(ord);
+  }
+
+  @Override
+  public List<FacetResult> getAllDims(int topN) throws IOException {
+    validateTopN(topN);
+    List<FacetResult> results = new ArrayList<>();
+    for (String dim : state.getDims()) {
+      TopChildrenForPath topChildrenForPath = getTopChildrenForPath(topN, dim);
+      FacetResult facetResult = createFacetResult(topChildrenForPath, dim);
+      if (facetResult != null) {
+        results.add(facetResult);
+      }
+    }
+
+    // Sort by highest count:
+    results.sort(FACET_RESULT_COMPARATOR);
+    return results;
+  }
+
+  @Override
+  public List<FacetResult> getTopDims(int topNDims, int topNChildren) throws IOException {
+    validateTopN(topNDims);
+    validateTopN(topNChildren);
+
+    // Creates priority queue to store top dimensions and sort by their aggregated values/hits and
+    // string values.
+    PriorityQueue<DimValue> pq =
+        new PriorityQueue<>(topNDims) {
+          @Override
+          protected boolean lessThan(DimValue a, DimValue b) {
+            if (a.value > b.value) {
+              return false;
+            } else if (a.value < b.value) {
+              return true;
+            } else {
+              return a.dim.compareTo(b.dim) > 0;
+            }
+          }
+        };
+
+    // Keep track of intermediate results, if we compute them, so we can reuse them later:
+    Map<String, TopChildrenForPath> intermediateResults = null;
+
+    for (String dim : state.getDims()) {
+      DimConfig dimConfig = stateConfig.getDimConfig(dim);
+      int dimCount;
+      if (dimConfig.hierarchical) {
+        int dimOrd = state.getDimTree(dim).dimStartOrd;
+        dimCount = getCount(dimOrd);
+      } else {
+        OrdRange ordRange = state.getOrdRange(dim);
+        int dimOrd = ordRange.start;
+        if (dimConfig.multiValued && dimConfig.requireDimCount) {
+          dimCount = getCount(dimOrd);
+        } else {
+          PrimitiveIterator.OfInt childIt = ordRange.iterator();
+          TopChildrenForPath topChildrenForPath =
+              computeTopChildren(childIt, topNChildren, dimConfig, dimOrd);
+          if (intermediateResults == null) {
+            intermediateResults = new HashMap<>();
+          }
+          intermediateResults.put(dim, topChildrenForPath);
+          dimCount = topChildrenForPath.pathCount;
+        }
+      }
+
+      if (dimCount != 0) {
+        if (pq.size() < topNDims) {
+          pq.add(new DimValue(dim, dimCount));
+        } else {
+          if (dimCount > pq.top().value
+              || (dimCount == pq.top().value && dim.compareTo(pq.top().dim) < 0)) {
+            DimValue bottomDim = pq.top();
+            bottomDim.dim = dim;
+            bottomDim.value = dimCount;
+            pq.updateTop();
+          }
+        }
+      }
+    }
+
+    int resultSize = pq.size();
+    FacetResult[] results = new FacetResult[resultSize];
+
+    while (pq.size() > 0) {
+      DimValue dimValue = pq.pop();
+      assert dimValue != null;
+      TopChildrenForPath topChildrenForPath = null;
+      if (intermediateResults != null) {
+        topChildrenForPath = intermediateResults.get(dimValue.dim);
+      }
+      if (topChildrenForPath == null) {
+        topChildrenForPath = getTopChildrenForPath(topNChildren, dimValue.dim);
+      }
+      FacetResult facetResult = createFacetResult(topChildrenForPath, dimValue.dim);
+      // should not be null since only dims with non-zero values were considered earlier
+      assert facetResult != null;
+      resultSize--;
+      results[resultSize] = facetResult;
+    }
+    return Arrays.asList(results);
+  }
+
+  abstract int getCount(int ord);

Review Comment:
   nit: maybe add a javadoc here explaining if counts are aggregated at indexing time, we can just retrieve it.



-- 
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-unsubscribe@lucene.apache.org

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


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