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/16 06:04:05 UTC

[GitHub] [lucene] shaie commented on a diff in pull request #841: LUCENE-10274: Add hyperrectangle faceting capabilities

shaie commented on code in PR #841:
URL: https://github.com/apache/lucene/pull/841#discussion_r873326117


##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/DoubleHyperRectangle.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.util.NumericUtils;
+
+/** Stores a hyper rectangle as an array of DoubleRangePairs */
+public class DoubleHyperRectangle extends HyperRectangle {
+
+  /** Stores pair as LongRangePair */
+  private final DoubleRangePair[] pairs;
+
+  /** Created DoubleHyperRectangle */
+  public DoubleHyperRectangle(String label, DoubleRangePair... pairs) {
+    super(label, pairs.length);
+    this.pairs = pairs;
+  }
+
+  @Override
+  public LongHyperRectangle.LongRangePair getComparableDimRange(int dim) {
+    long longMin = NumericUtils.doubleToSortableLong(pairs[dim].min);
+    long longMax = NumericUtils.doubleToSortableLong(pairs[dim].max);
+    return new LongHyperRectangle.LongRangePair(longMin, true, longMax, true);
+  }
+
+  /** Defines a single range in a DoubleHyperRectangle */
+  public static class DoubleRangePair {
+    /** Inclusive min */
+    public final double min;
+
+    /** Inclusive max */
+    public final double max;
+
+    /**
+     * Creates a LongRangePair, very similar to the constructor of {@link
+     * org.apache.lucene.facet.range.DoubleRange}
+     *
+     * @param minIn Min value of pair
+     * @param minInclusive If minIn is inclusive
+     * @param maxIn Max value of pair
+     * @param maxInclusive If maxIn is inclusive
+     */
+    public DoubleRangePair(double minIn, boolean minInclusive, double maxIn, boolean maxInclusive) {
+      if (Double.isNaN(minIn)) {
+        throw new IllegalArgumentException("min cannot be NaN");
+      }
+      if (!minInclusive) {
+        minIn = Math.nextUp(minIn);
+      }
+
+      if (Double.isNaN(maxIn)) {

Review Comment:
   Move this check before `is (!minInclusive)` and I suggest unifying both checks like this:
   
   ```
   if (Double.isNaN(minIn) || Double.isNaN(maxIn)) {
     throw new IllegalArgumentException("min and max cannot be NaN: min=" + minIn + ", max=" + maxIn);
   }
   ```



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hyperrectangle;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+
+/** Get counts given a list of HyperRectangles (which must be of the same type) */
+public class HyperRectangleFacetCounts extends Facets {
+  /** Hypper rectangles passed to constructor. */
+  protected final HyperRectangle[] hyperRectangles;
+
+  /** Counts, initialized in by subclass. */

Review Comment:
   Either remove "in" or "by", I think one of them is redundant



##########
lucene/facet/src/test/org/apache/lucene/facet/hyperrectangle/TestHyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetTestCase;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.FacetsCollectorManager;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.tests.index.RandomIndexWriter;
+
+public class TestHyperRectangleFacetCounts extends FacetTestCase {
+
+  public void testBasicLong() throws Exception {
+    Directory d = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), d);
+
+    for (long l = 0; l < 100; l++) {
+      Document doc = new Document();
+      LongPointFacetField field = new LongPointFacetField("field", l, l + 1L, l + 2L);
+      doc.add(field);
+      w.addDocument(doc);
+    }
+
+    // Also add point with Long.MAX_VALUE
+    Document doc = new Document();
+    LongPointFacetField field =
+        new LongPointFacetField("field", Long.MAX_VALUE - 2L, Long.MAX_VALUE - 1L, Long.MAX_VALUE);
+    doc.add(field);
+    w.addDocument(doc);
+
+    IndexReader r = w.getReader();
+    w.close();
+
+    IndexSearcher s = newSearcher(r);
+    FacetsCollector fc = s.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
+
+    Facets facets =
+        new HyperRectangleFacetCounts(
+            "field",
+            fc,
+            new LongHyperRectangle(
+                "less than (10, 11, 12)",
+                new LongHyperRectangle.LongRangePair(0L, true, 10L, false),
+                new LongHyperRectangle.LongRangePair(0L, true, 11L, false),
+                new LongHyperRectangle.LongRangePair(0L, true, 12L, false)),
+            new LongHyperRectangle(
+                "less than or equal to (10, 11, 12)",
+                new LongHyperRectangle.LongRangePair(0L, true, 10L, true),
+                new LongHyperRectangle.LongRangePair(0L, true, 11L, true),
+                new LongHyperRectangle.LongRangePair(0L, true, 12L, true)),
+            new LongHyperRectangle(
+                "over (90, 91, 92)",
+                new LongHyperRectangle.LongRangePair(90L, false, 100L, false),
+                new LongHyperRectangle.LongRangePair(91L, false, 101L, false),
+                new LongHyperRectangle.LongRangePair(92L, false, 102L, false)),
+            new LongHyperRectangle(
+                "(90, 91, 92) or above",
+                new LongHyperRectangle.LongRangePair(90L, true, 100L, false),
+                new LongHyperRectangle.LongRangePair(91L, true, 101L, false),
+                new LongHyperRectangle.LongRangePair(92L, true, 102L, false)),
+            new LongHyperRectangle(
+                "over (1000, 1000, 1000)",
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE - 2L, true),
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE - 1L, true),
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE, true)));
+
+    FacetResult result = facets.getTopChildren(10, "field");
+    assertEquals(
+        """
+                        dim=field path=[] value=22 childCount=5
+                          less than (10, 11, 12) (10)
+                          less than or equal to (10, 11, 12) (11)
+                          over (90, 91, 92) (9)
+                          (90, 91, 92) or above (10)
+                          over (1000, 1000, 1000) (1)
+                        """,
+        result.toString());
+
+    // test getTopChildren(0, dim)
+    expectThrows(
+        IllegalArgumentException.class,
+        () -> {
+          facets.getTopChildren(0, "field");
+        });
+
+    r.close();
+    d.close();
+  }
+
+  public void testBasicDouble() throws Exception {
+    Directory d = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), d);
+
+    for (double l = 0; l < 100; l++) {
+      Document doc = new Document();
+      DoublePointFacetField field = new DoublePointFacetField("field", l, l + 1.0, l + 2.0);
+      doc.add(field);
+      w.addDocument(doc);
+    }
+
+    // Also add point with Long.MAX_VALUE
+    Document doc = new Document();
+    DoublePointFacetField field =
+        new DoublePointFacetField(
+            "field", Double.MAX_VALUE - 2.0, Double.MAX_VALUE - 1.0, Double.MAX_VALUE);
+    doc.add(field);
+    w.addDocument(doc);
+
+    IndexReader r = w.getReader();
+    w.close();
+
+    IndexSearcher s = newSearcher(r);
+    FacetsCollector fc = s.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
+
+    Facets facets =
+        new HyperRectangleFacetCounts(
+            "field",
+            fc,
+            new DoubleHyperRectangle(
+                "less than (10, 11, 12)",
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 10.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 11.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 12.0, false)),
+            new DoubleHyperRectangle(
+                "less than or equal to (10, 11, 12)",
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 10.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 11.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 12.0, true)),
+            new DoubleHyperRectangle(
+                "over (90, 91, 92)",
+                new DoubleHyperRectangle.DoubleRangePair(90.0, false, 100.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(91.0, false, 101.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(92.0, false, 102.0, false)),
+            new DoubleHyperRectangle(
+                "(90, 91, 92) or above",
+                new DoubleHyperRectangle.DoubleRangePair(90.0, true, 100.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(91.0, true, 101.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(92.0, true, 102.0, false)),
+            new DoubleHyperRectangle(
+                "over (1000, 1000, 1000)",
+                new DoubleHyperRectangle.DoubleRangePair(
+                    1000.0, false, Double.MAX_VALUE - 2.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(
+                    1000.0, false, Double.MAX_VALUE - 1.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(1000.0, false, Double.MAX_VALUE, true)));
+
+    FacetResult result = facets.getTopChildren(10, "field");
+    assertEquals(
+        """
+                        dim=field path=[] value=22 childCount=5

Review Comment:
   nit: personally I prefer less this type of assertions as they are very fragile. If we change the `toString()` tomorrow we'll need to fix all the tests. Can we change to test to make explicit assertions on the label + count? Eventually we want to test the returned facets, not their `toString()` representation.



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangle.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.hyperrectangle;
+
+/** Holds the name and the number of dims for a HyperRectangle */
+public abstract class HyperRectangle {
+  /** Label that identifies this range. */
+  public final String label;
+
+  /** How many dimensions this hyper rectangle has (IE: a regular rectangle would have dims=2) */
+  public final int dims;
+
+  /** Sole constructor. */
+  protected HyperRectangle(String label, int dims) {
+    if (label == null) {
+      throw new NullPointerException("label must not be null");
+    }
+    this.label = label;
+    this.dims = dims;

Review Comment:
   Do we want to add some validity checks on dims?



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/DoubleHyperRectangle.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.util.NumericUtils;
+
+/** Stores a hyper rectangle as an array of DoubleRangePairs */
+public class DoubleHyperRectangle extends HyperRectangle {
+
+  /** Stores pair as LongRangePair */

Review Comment:
   DoubleRangePair? 



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangle.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.hyperrectangle;
+
+/** Holds the name and the number of dims for a HyperRectangle */
+public abstract class HyperRectangle {
+  /** Label that identifies this range. */
+  public final String label;
+
+  /** How many dimensions this hyper rectangle has (IE: a regular rectangle would have dims=2) */
+  public final int dims;
+
+  /** Sole constructor. */
+  protected HyperRectangle(String label, int dims) {
+    if (label == null) {
+      throw new NullPointerException("label must not be null");

Review Comment:
   I find it consistent that you throw NPE here and IAE in `DoubleRangePair` for illegal arguments.



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/DoubleHyperRectangle.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.util.NumericUtils;
+
+/** Stores a hyper rectangle as an array of DoubleRangePairs */
+public class DoubleHyperRectangle extends HyperRectangle {
+
+  /** Stores pair as LongRangePair */
+  private final DoubleRangePair[] pairs;
+
+  /** Created DoubleHyperRectangle */
+  public DoubleHyperRectangle(String label, DoubleRangePair... pairs) {
+    super(label, pairs.length);

Review Comment:
   Q: can `pairs` be null? If so perhaps we should add a null check? Also, is it a valid case to receive an empty `pairs`?



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hyperrectangle;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+
+/** Get counts given a list of HyperRectangles (which must be of the same type) */
+public class HyperRectangleFacetCounts extends Facets {
+  /** Hypper rectangles passed to constructor. */
+  protected final HyperRectangle[] hyperRectangles;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Number of dimensions for field */
+  protected final int dims;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of long hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, LongHyperRectangle... hyperRectangles)

Review Comment:
   I think `hits` should be named `facetsCollector`? Since these are not the actual hits to collect facets on. If you accept this, please rename in all the places



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hyperrectangle;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+
+/** Get counts given a list of HyperRectangles (which must be of the same type) */
+public class HyperRectangleFacetCounts extends Facets {
+  /** Hypper rectangles passed to constructor. */
+  protected final HyperRectangle[] hyperRectangles;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Number of dimensions for field */
+  protected final int dims;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of long hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, LongHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of double hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, DoubleHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  private HyperRectangleFacetCounts(
+      boolean discarded, String field, FacetsCollector hits, HyperRectangle... hyperRectangles)
+      throws IOException {
+    assert hyperRectangles.length > 0 : "Hyper rectangle ranges cannot be empty";
+    this.field = field;
+    this.hyperRectangles = hyperRectangles;
+    this.dims = hyperRectangles[0].dims;
+    for (HyperRectangle hyperRectangle : hyperRectangles) {

Review Comment:
   nit: currently you loop through this list always, even when `-ea` is not set. If you factor it out to a method you could call `assert hyperRectanglesDims(this.dims)`



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hyperrectangle;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+
+/** Get counts given a list of HyperRectangles (which must be of the same type) */
+public class HyperRectangleFacetCounts extends Facets {
+  /** Hypper rectangles passed to constructor. */
+  protected final HyperRectangle[] hyperRectangles;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Number of dimensions for field */
+  protected final int dims;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of long hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, LongHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of double hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, DoubleHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  private HyperRectangleFacetCounts(
+      boolean discarded, String field, FacetsCollector hits, HyperRectangle... hyperRectangles)
+      throws IOException {
+    assert hyperRectangles.length > 0 : "Hyper rectangle ranges cannot be empty";
+    this.field = field;
+    this.hyperRectangles = hyperRectangles;
+    this.dims = hyperRectangles[0].dims;
+    for (HyperRectangle hyperRectangle : hyperRectangles) {
+      assert hyperRectangle.dims == this.dims
+          : "All hyper rectangles must be the same dimensionality";
+    }
+    this.counts = new int[hyperRectangles.length];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  private void count(String field, List<FacetsCollector.MatchingDocs> matchingDocs)
+      throws IOException {
+
+    for (int i = 0; i < matchingDocs.size(); i++) {
+
+      FacetsCollector.MatchingDocs hits = matchingDocs.get(i);
+
+      BinaryDocValues binaryDocValues = DocValues.getBinary(hits.context.reader(), field);
+
+      final DocIdSetIterator it = hits.bits.iterator();
+      if (it == null) {
+        continue;
+      }
+
+      for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
+        if (binaryDocValues.advanceExact(doc)) {
+          long[] point = LongPoint.unpack(binaryDocValues.binaryValue());
+          assert point.length == dims : "Point dimension is incompatible with hyper rectangle";

Review Comment:
   nit: is it useful to add the `point.length` and `dims` to the assertion message?



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/HyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hyperrectangle;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.LongPoint;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+
+/** Get counts given a list of HyperRectangles (which must be of the same type) */
+public class HyperRectangleFacetCounts extends Facets {
+  /** Hypper rectangles passed to constructor. */
+  protected final HyperRectangle[] hyperRectangles;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Number of dimensions for field */
+  protected final int dims;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of long hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, LongHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  /**
+   * Create HyperRectangleFacetCounts using
+   *
+   * @param field Field name
+   * @param hits Hits to facet on
+   * @param hyperRectangles List of double hyper rectangle facets
+   * @throws IOException If there is a problem reading the field
+   */
+  public HyperRectangleFacetCounts(
+      String field, FacetsCollector hits, DoubleHyperRectangle... hyperRectangles)
+      throws IOException {
+    this(true, field, hits, hyperRectangles);
+  }
+
+  private HyperRectangleFacetCounts(
+      boolean discarded, String field, FacetsCollector hits, HyperRectangle... hyperRectangles)

Review Comment:
   I see `discarded` is always `true` now, am I missing a call?



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/DoubleHyperRectangle.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.util.NumericUtils;
+
+/** Stores a hyper rectangle as an array of DoubleRangePairs */
+public class DoubleHyperRectangle extends HyperRectangle {
+
+  /** Stores pair as LongRangePair */
+  private final DoubleRangePair[] pairs;
+
+  /** Created DoubleHyperRectangle */
+  public DoubleHyperRectangle(String label, DoubleRangePair... pairs) {
+    super(label, pairs.length);
+    this.pairs = pairs;
+  }
+
+  @Override
+  public LongHyperRectangle.LongRangePair getComparableDimRange(int dim) {

Review Comment:
   perf: this method is called many times, IIUC once for each dimension for each requested rectangle for each matching document. Consider passing a `LongPair` instance in, and only update its fields.



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/LongHyperRectangle.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.hyperrectangle;
+
+/** Stores a hyper rectangle as an array of LongRangePairs */
+public class LongHyperRectangle extends HyperRectangle {
+
+  private final LongRangePair[] pairs;
+
+  /** Created LongHyperRectangle */
+  public LongHyperRectangle(String label, LongRangePair... pairs) {
+    super(label, pairs.length);
+    this.pairs = pairs;
+  }
+
+  @Override
+  public LongRangePair getComparableDimRange(int dim) {
+    return pairs[dim];
+  }
+
+  /** Defines a single range in a LongHyperRectangle */
+  public static class LongRangePair {
+    /** Inclusive min */
+    public final long min;
+
+    /** Inclusive max */
+    public final long max;
+
+    /**
+     * Creates a LongRangePair, very similar to the constructor of {@link
+     * org.apache.lucene.facet.range.LongRange}
+     *
+     * @param minIn Min value of pair
+     * @param minInclusive If minIn is inclusive
+     * @param maxIn Max value of pair
+     * @param maxInclusive If maxIn is inclusive
+     */
+    public LongRangePair(long minIn, boolean minInclusive, long maxIn, boolean maxInclusive) {
+      if (!minInclusive) {
+        if (minIn != Long.MAX_VALUE) {
+          minIn++;
+        } else {
+          throw new IllegalArgumentException("Invalid min input");
+        }
+      }
+
+      if (!maxInclusive) {
+        if (maxIn != Long.MIN_VALUE) {
+          maxIn--;
+        } else {
+          throw new IllegalArgumentException("Invalid max input");

Review Comment:
   Add the actual input value to the exception for clarity



##########
lucene/facet/src/test/org/apache/lucene/facet/hyperrectangle/TestHyperRectangleFacetCounts.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.hyperrectangle;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetTestCase;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.FacetsCollectorManager;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.tests.index.RandomIndexWriter;
+
+public class TestHyperRectangleFacetCounts extends FacetTestCase {
+
+  public void testBasicLong() throws Exception {
+    Directory d = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), d);
+
+    for (long l = 0; l < 100; l++) {
+      Document doc = new Document();
+      LongPointFacetField field = new LongPointFacetField("field", l, l + 1L, l + 2L);
+      doc.add(field);
+      w.addDocument(doc);
+    }
+
+    // Also add point with Long.MAX_VALUE
+    Document doc = new Document();
+    LongPointFacetField field =
+        new LongPointFacetField("field", Long.MAX_VALUE - 2L, Long.MAX_VALUE - 1L, Long.MAX_VALUE);
+    doc.add(field);
+    w.addDocument(doc);
+
+    IndexReader r = w.getReader();
+    w.close();
+
+    IndexSearcher s = newSearcher(r);
+    FacetsCollector fc = s.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
+
+    Facets facets =
+        new HyperRectangleFacetCounts(
+            "field",
+            fc,
+            new LongHyperRectangle(
+                "less than (10, 11, 12)",
+                new LongHyperRectangle.LongRangePair(0L, true, 10L, false),
+                new LongHyperRectangle.LongRangePair(0L, true, 11L, false),
+                new LongHyperRectangle.LongRangePair(0L, true, 12L, false)),
+            new LongHyperRectangle(
+                "less than or equal to (10, 11, 12)",
+                new LongHyperRectangle.LongRangePair(0L, true, 10L, true),
+                new LongHyperRectangle.LongRangePair(0L, true, 11L, true),
+                new LongHyperRectangle.LongRangePair(0L, true, 12L, true)),
+            new LongHyperRectangle(
+                "over (90, 91, 92)",
+                new LongHyperRectangle.LongRangePair(90L, false, 100L, false),
+                new LongHyperRectangle.LongRangePair(91L, false, 101L, false),
+                new LongHyperRectangle.LongRangePair(92L, false, 102L, false)),
+            new LongHyperRectangle(
+                "(90, 91, 92) or above",
+                new LongHyperRectangle.LongRangePair(90L, true, 100L, false),
+                new LongHyperRectangle.LongRangePair(91L, true, 101L, false),
+                new LongHyperRectangle.LongRangePair(92L, true, 102L, false)),
+            new LongHyperRectangle(
+                "over (1000, 1000, 1000)",
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE - 2L, true),
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE - 1L, true),
+                new LongHyperRectangle.LongRangePair(1000L, false, Long.MAX_VALUE, true)));
+
+    FacetResult result = facets.getTopChildren(10, "field");
+    assertEquals(
+        """
+                        dim=field path=[] value=22 childCount=5
+                          less than (10, 11, 12) (10)
+                          less than or equal to (10, 11, 12) (11)
+                          over (90, 91, 92) (9)
+                          (90, 91, 92) or above (10)
+                          over (1000, 1000, 1000) (1)
+                        """,
+        result.toString());
+
+    // test getTopChildren(0, dim)
+    expectThrows(
+        IllegalArgumentException.class,
+        () -> {
+          facets.getTopChildren(0, "field");
+        });
+
+    r.close();
+    d.close();
+  }
+
+  public void testBasicDouble() throws Exception {
+    Directory d = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), d);
+
+    for (double l = 0; l < 100; l++) {
+      Document doc = new Document();
+      DoublePointFacetField field = new DoublePointFacetField("field", l, l + 1.0, l + 2.0);
+      doc.add(field);
+      w.addDocument(doc);
+    }
+
+    // Also add point with Long.MAX_VALUE
+    Document doc = new Document();
+    DoublePointFacetField field =
+        new DoublePointFacetField(
+            "field", Double.MAX_VALUE - 2.0, Double.MAX_VALUE - 1.0, Double.MAX_VALUE);
+    doc.add(field);
+    w.addDocument(doc);
+
+    IndexReader r = w.getReader();
+    w.close();
+
+    IndexSearcher s = newSearcher(r);
+    FacetsCollector fc = s.search(new MatchAllDocsQuery(), new FacetsCollectorManager());
+
+    Facets facets =
+        new HyperRectangleFacetCounts(
+            "field",
+            fc,
+            new DoubleHyperRectangle(
+                "less than (10, 11, 12)",
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 10.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 11.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 12.0, false)),
+            new DoubleHyperRectangle(
+                "less than or equal to (10, 11, 12)",
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 10.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 11.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(0.0, true, 12.0, true)),
+            new DoubleHyperRectangle(
+                "over (90, 91, 92)",
+                new DoubleHyperRectangle.DoubleRangePair(90.0, false, 100.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(91.0, false, 101.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(92.0, false, 102.0, false)),
+            new DoubleHyperRectangle(
+                "(90, 91, 92) or above",
+                new DoubleHyperRectangle.DoubleRangePair(90.0, true, 100.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(91.0, true, 101.0, false),
+                new DoubleHyperRectangle.DoubleRangePair(92.0, true, 102.0, false)),
+            new DoubleHyperRectangle(
+                "over (1000, 1000, 1000)",
+                new DoubleHyperRectangle.DoubleRangePair(
+                    1000.0, false, Double.MAX_VALUE - 2.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(
+                    1000.0, false, Double.MAX_VALUE - 1.0, true),
+                new DoubleHyperRectangle.DoubleRangePair(1000.0, false, Double.MAX_VALUE, true)));
+
+    FacetResult result = facets.getTopChildren(10, "field");
+    assertEquals(
+        """
+                        dim=field path=[] value=22 childCount=5
+                          less than (10, 11, 12) (10)
+                          less than or equal to (10, 11, 12) (11)
+                          over (90, 91, 92) (9)
+                          (90, 91, 92) or above (10)
+                          over (1000, 1000, 1000) (1)
+                        """,
+        result.toString());
+
+    // test getTopChildren(0, dim)
+    expectThrows(

Review Comment:
   I think this test is just repeated for the Long + Double cases. If you factor it out to its own test I think (1) it will be clearer what is being tested, e.g. `testInvalidTopN` and (2) you won't need to test it twice.



##########
lucene/facet/src/java/org/apache/lucene/facet/hyperrectangle/LongHyperRectangle.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.hyperrectangle;
+
+/** Stores a hyper rectangle as an array of LongRangePairs */
+public class LongHyperRectangle extends HyperRectangle {
+
+  private final LongRangePair[] pairs;
+
+  /** Created LongHyperRectangle */
+  public LongHyperRectangle(String label, LongRangePair... pairs) {
+    super(label, pairs.length);
+    this.pairs = pairs;
+  }
+
+  @Override
+  public LongRangePair getComparableDimRange(int dim) {
+    return pairs[dim];
+  }
+
+  /** Defines a single range in a LongHyperRectangle */
+  public static class LongRangePair {
+    /** Inclusive min */
+    public final long min;
+
+    /** Inclusive max */
+    public final long max;
+
+    /**
+     * Creates a LongRangePair, very similar to the constructor of {@link
+     * org.apache.lucene.facet.range.LongRange}
+     *
+     * @param minIn Min value of pair
+     * @param minInclusive If minIn is inclusive
+     * @param maxIn Max value of pair
+     * @param maxInclusive If maxIn is inclusive
+     */
+    public LongRangePair(long minIn, boolean minInclusive, long maxIn, boolean maxInclusive) {
+      if (!minInclusive) {
+        if (minIn != Long.MAX_VALUE) {
+          minIn++;
+        } else {
+          throw new IllegalArgumentException("Invalid min input");

Review Comment:
   Add the actual input value to the exception for clarity



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