Skip to content

Apache Lucene open-source search software

Merge branch 'master' into point_set_query

Mike McCandlessdfbcbf96cd3b
diff --cc lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
index 53973c9babc,2db2d856009..e69d0b993d3
--- a/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
@@@ -16,13 -16,9 +16,14 @@@
*/
package org.apache.lucene.document;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
+ import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.NumericUtils;
/**
diff --cc lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
index d262c18b141,027dc66f3cf..1f39a9f2dd8
--- a/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
@@@ -16,13 -16,9 +16,14 @@@
*/
package org.apache.lucene.document;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
+ import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.NumericUtils;
/**
diff --cc lucene/core/src/java/org/apache/lucene/document/IntPoint.java
index cdd10c7e191,2d411e94093..a78c30d60a5
--- a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java
@@@ -16,13 -16,9 +16,14 @@@
*/
package org.apache.lucene.document;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
+ import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.NumericUtils;
/**
diff --cc lucene/core/src/java/org/apache/lucene/document/LongPoint.java
index 9c0ba3f468e,69b511d1e48..d0c5655aa10
--- a/lucene/core/src/java/org/apache/lucene/document/LongPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/LongPoint.java
@@@ -16,13 -16,9 +16,14 @@@
*/
package org.apache.lucene.document;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
+ import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.NumericUtils;
/**
diff --cc lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
index 9e18c190b43,0ea9efb41f1..dca5f97c64b
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPoint.java
@@@ -83,6 -128,89 +128,91 @@@ public class LatLonPoint extends Field
public static double decodeLon(int x) {
return x / LON_SCALE;
}
+
+ // nocommit newSetQuery
+
+ /** Turns quantized value from byte array back into a double. */
+ public static double decodeLon(byte[] src, int offset) {
+ return decodeLon(NumericUtils.bytesToInt(src, offset));
+ }
+
+ /** sugar encodes a single point as a 2D byte array */
+ private static byte[][] encode(double lat, double lon) {
+ byte[][] bytes = new byte[2][];
+ bytes[0] = new byte[4];
+ NumericUtils.intToBytes(encodeLat(lat), bytes[0], 0);
+ bytes[1] = new byte[4];
+ NumericUtils.intToBytes(encodeLon(lon), bytes[1], 0);
+ return bytes;
+ }
+
+ // static methods for generating queries
+
+ /**
+ * Create a query for matching a bounding box.
+ * <p>
+ * The box may cross over the dateline.
+ */
+ public static Query newBoxQuery(String field, double minLat, double maxLat, double minLon, double maxLon) {
+ if (GeoUtils.isValidLat(minLat) == false) {
+ throw new IllegalArgumentException("minLat=" + minLat + " is not a valid latitude");
+ }
+ if (GeoUtils.isValidLat(maxLat) == false) {
+ throw new IllegalArgumentException("maxLat=" + maxLat + " is not a valid latitude");
+ }
+ if (GeoUtils.isValidLon(minLon) == false) {
+ throw new IllegalArgumentException("minLon=" + minLon + " is not a valid longitude");
+ }
+ if (GeoUtils.isValidLon(maxLon) == false) {
+ throw new IllegalArgumentException("maxLon=" + maxLon + " is not a valid longitude");
+ }
+
+ byte[][] lower = encode(minLat, minLon);
+ byte[][] upper = encode(maxLat, maxLon);
+ // Crosses date line: we just rewrite into OR of two bboxes, with longitude as an open range:
+ if (maxLon < minLon) {
+ // Disable coord here because a multi-valued doc could match both rects and get unfairly boosted:
+ BooleanQuery.Builder q = new BooleanQuery.Builder();
+ q.setDisableCoord(true);
+
+ // E.g.: maxLon = -179, minLon = 179
+ byte[][] leftOpen = new byte[2][];
+ leftOpen[0] = lower[0];
+ // leave longitude open (null)
+ Query left = newBoxInternal(field, leftOpen, upper);
+ q.add(new BooleanClause(left, BooleanClause.Occur.SHOULD));
+ byte[][] rightOpen = new byte[2][];
+ rightOpen[0] = upper[0];
+ // leave longitude open (null)
+ Query right = newBoxInternal(field, lower, rightOpen);
+ q.add(new BooleanClause(right, BooleanClause.Occur.SHOULD));
+ return new ConstantScoreQuery(q.build());
+ } else {
+ return newBoxInternal(field, lower, upper);
+ }
+ }
+
+ private static Query newBoxInternal(String field, byte[][] min, byte[][] max) {
+ return new PointRangeQuery(field, min, new boolean[] { true, true }, max, new boolean[] { false, false }) {
+ @Override
+ protected String toString(int dimension, byte[] value) {
+ if (dimension == 0) {
+ return Double.toString(decodeLat(value, 0));
+ } else if (dimension == 1) {
+ return Double.toString(decodeLon(value, 0));
+ } else {
+ throw new AssertionError();
+ }
+ }
+ };
+ }
+
+ /**
+ * Create a query for matching a polygon.
+ * <p>
+ * The supplied {@code polyLats}/{@code polyLons} must be clockwise or counter-clockwise.
+ */
+ public static Query newPolygonQuery(String field, double[] polyLats, double[] polyLons) {
+ return new PointInPolygonQuery(field, polyLats, polyLons);
+ }
}