gsmiller commented on code in PR #11901:
URL: https://github.com/apache/lucene/pull/11901#discussion_r1044821853


##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final Range[] ranges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+  private final int numRanges;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      Range... ranges)
+      throws IOException {
+    super(fastMatchQuery);
+    this.field = field;
+    this.ranges = ranges;
+    this.numEncodedValueBytes = ranges[0].getEncodedValueBytes();
+    this.dims = ranges[0].dims;
+    this.labels = getLabels(ranges);
+    this.numRanges = ranges.length;
+    this.queryType = queryType;
+    counts = new int[numRanges];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> 
matchingDocs)
+      throws IOException {
+
+    int missingCount = 0;
+
+    for (int i = 0; i < matchingDocs.size(); i++) {
+
+      FacetsCollector.MatchingDocs hits = matchingDocs.get(i);
+      BinaryRangeDocValues binaryRangeDocValues =
+          new BinaryRangeDocValues(
+              DocValues.getBinary(hits.context.reader(), field), dims, 
numEncodedValueBytes);
+
+      final DocIdSetIterator it = createIterator(hits);
+      if (it == null) {
+        continue;
+      }
+
+      totCount += hits.totalHits;
+      for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
+        if (binaryRangeDocValues.advanceExact(doc)) {
+          boolean hasValidRange = false;
+          for (int range = 0; range < numRanges; range++) {
+            byte[] encodedRange = getEncodedRange(ranges[range]);
+            byte[] packedRange = binaryRangeDocValues.getPackedValue();
+            assert encodedRange.length == packedRange.length;
+            boolean matchesRange = true;
+            for (int dim = 0; dim < dims; dim++) {
+              if (queryType.matches(encodedRange, packedRange, dims, 
numEncodedValueBytes, dim)
+                  == false) {
+                matchesRange = false;
+                break;
+              }
+            }
+            if (matchesRange) {
+              counts[range]++;
+              hasValidRange = true;
+            }
+          }
+          if (hasValidRange == false) {
+            missingCount++;
+          }
+        } else {
+          missingCount++;
+        }
+        doc = it.nextDoc();
+      }
+    }
+    totCount -= missingCount;
+  }
+
+  @Override
+  public FacetResult getAllChildren(String dim, String... path) throws 
IOException {

Review Comment:
   Sounds right. Thanks!



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