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


##########
lucene/core/src/java/org/apache/lucene/document/LongRange.java:
##########
@@ -61,6 +61,17 @@ public LongRange(String name, final long[] min, final long[] 
max) {
     setRangeValues(min, max);
   }
 
+  /**
+   * Create a new LongRange type with one dimension
+   *
+   * @param name field name. must not be null.
+   * @param min min value
+   * @param max max value
+   */
+  public LongRange(String name, final long min, final long max) {

Review Comment:
   I like the idea of adding these (along with adding single-valued ctors to 
the Field classes as mentioned elsewhere), but let's do it in a separate PR 
since it's not strictly required for what you're building here. I think that 
would help keep the change tight and allow future "digital archeologists" to 
understand the work better.



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

Review Comment:
   Is there a need to separately store the labels here? Could we just reference 
the `Range[] range` array instead to get the labels as needed?



##########
lucene/core/src/java/org/apache/lucene/document/LongRangeDocValuesFacetField.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.document;
+
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * A subclass of LongRangeDocValuesField that only allows single dimension 
range representation.
+ * Meant to be used with RangeOnRange faceting
+ */
+public class LongRangeDocValuesFacetField extends LongRangeDocValuesField {

Review Comment:
   I'm not sure we need to create new Field classes for faceting. Can we 
simplify things a bit here and implement range faceting for the existing range 
fields? For example, if a user wants to facet on long-based ranges, can they 
just index a `LongRangeDocValuesField` directly and we facet over that?
   
   Looking at this subclass definition, it seems like the added value is just 
the single-dim ctor. Is that right? I think that's nice syntactic sugar, but 
maybe we add it directly to `LongRangeDocValuesField` instead? (If we want to 
do that, I'd suggest doing that in a separate PR just to separate things a bit).



##########
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:
   Can you add some javadoc explaining that we will guarantee that children 
will be returned in the order the ranges are provided? See the javadoc in 
`LongRangeFacetCounts` for an example. In general, this method doesn't 
guarantee any ordering, but it's useful to have the guaranteed ordering 
property for these cases where users provide ranges in a specific order.



##########
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]);

Review Comment:
   Let's pull the encoding out of the loop. What if we encoded all the ranges 
up-front before entering the loop? One way we could do this is have the ctors 
actually require the encoded ranges to be provided, and then the concrete 
subclasses could encode up-front before calling `super` and there wouldn't be a 
need to expose this `getEncodedRange` method at all.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/LongRangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 static org.apache.lucene.document.LongRange.verifyAndEncode;
+
+import java.io.IOException;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.search.Query;
+
+/** Represents counts for long range on range faceting */
+public class LongRangeOnRangeFacetCounts extends RangeOnRangeFacetCounts {
+
+  /**
+   * Represents counts for long range on range faceting (inclusive)
+   *
+   * @param field document's field
+   * @param hits hits we want the counts of
+   * @param queryType type of intersection we want to count
+   * @param ranges ranges we want the counts of
+   * @throws IOException low level exception
+   */
+  public LongRangeOnRangeFacetCounts(
+      String field, FacetsCollector hits, RangeFieldQuery.QueryType queryType, 
LongRange... ranges)
+      throws IOException {
+    super(field, hits, queryType, null, ranges);
+  }
+
+  /**
+   * Represents counts for long range on range faceting
+   *
+   * @param field document's field
+   * @param hits hits we want the counts of
+   * @param queryType type of intersection we want to count
+   * @param fastMatchQuery query to quickly discard hits
+   * @param ranges ranges we want the counts of
+   * @throws IOException low level exception
+   */
+  public LongRangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      LongRange... ranges)
+      throws IOException {
+    super(field, hits, queryType, fastMatchQuery, ranges);
+  }
+
+  @Override
+  public byte[] getEncodedRange(Range range) {

Review Comment:
   What about changing the ctor of `RangeOnRangeFacetCounts` to accept the 
already-encoded ranges instead of exposing this callback method? Then the ctors 
in `LongRangeOnRangeFacetCounts` could just do the encoding "privately" and 
pass the encoded ranges to super()?



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

Review Comment:
   Maybe add a TODO somewhere describing the idea that we may be able to do the 
counting more efficiently than exhaustively checking all ranges for each doc? 
Seems like we could probably leverage some sort of space-partitioning data 
structure here to identify "matching" ranges to count for each doc instead of 
just looping over all of them linearly?



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

Review Comment:
   There's a version of `QueryType#matches` that checks all dims instead of 
just a single dim. Let's use that so you don't need to re-implement the loop 
over all the dims.



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