jpountz commented on code in PR #14609:
URL: https://github.com/apache/lucene/pull/14609#discussion_r2074166256


##########
lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java:
##########
@@ -45,8 +49,8 @@
  * <p>For a single-dimensional field this query is a simple range query; in a 
multi-dimensional
  * field it's a box shape.
  *
- * @see PointValues
  * @lucene.experimental
+ * @see PointValues

Review Comment:
   I don't think we have a rule around it, but the lucene.experimental / 
lucene.internal tag is usually the last one, so let's not swap these two lines?



##########
lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java:
##########
@@ -580,4 +557,111 @@ public final String toString(String field) {
    * @return human readable value for debugging
    */
   protected abstract String toString(int dimension, byte[] value);
+
+  @Override
+  public Query rewrite(IndexSearcher searcher) throws IOException {
+    IndexReader reader = searcher.getIndexReader();
+
+    for (LeafReaderContext leaf : reader.leaves()) {
+      checkValidPointValues(leaf.reader().getPointValues(field));
+    }
+
+    // fetch the global min/max packed values across all segments
+    byte[] globalMinPacked = PointValues.getMinPackedValue(reader, getField());
+    byte[] globalMaxPacked = PointValues.getMaxPackedValue(reader, getField());
+    if (globalMinPacked == null || globalMaxPacked == null) {
+      return super.rewrite(searcher);
+    }
+
+    int dims = getNumDims();
+    int bytesPerDim = getBytesPerDim();
+    byte[] queryLow = getLowerPoint();
+    byte[] queryHigh = getUpperPoint();
+
+    int fullyContainedCount = 0;
+    int fullyExcludedCount = 0;
+
+    // Check each dimension individually
+    for (int dim = 0; dim < dims; dim++) {
+      int offset = dim * bytesPerDim;
+      BytesRef qLow = new BytesRef(queryLow, offset, bytesPerDim);
+      BytesRef qHigh = new BytesRef(queryHigh, offset, bytesPerDim);
+      BytesRef gMin = new BytesRef(globalMinPacked, offset, bytesPerDim);
+      BytesRef gMax = new BytesRef(globalMaxPacked, offset, bytesPerDim);
+
+      if (qLow.compareTo(gMin) <= 0 && qHigh.compareTo(gMax) >= 0) {
+        fullyContainedCount++;
+      } else if (qLow.compareTo(gMax) > 0 || qHigh.compareTo(gMin) < 0) {
+        fullyExcludedCount++;
+      }
+    }
+
+    if (fullyContainedCount == dims) {
+      if (canRewriteToMatchAllQuery(reader)) {
+        return new MatchAllDocsQuery();
+      } else if (canRewriteToFieldExistsQuery(reader)) {
+        return new FieldExistsQuery(field);
+      }
+    } else if (fullyExcludedCount == dims) {
+      return new MatchNoDocsQuery();
+    }

Review Comment:
   I wonder if we can somehow reuse the existing `relate(byte[], byte[])` 
method?



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