Apache Lucene open-source search software
add InetAddressPoint.newSetQuery
Mike McCandless1fb9de8da5fd
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java b/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.javaindex 1c73d21c508..d9e9d376fb0 100644--- a/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java+++ b/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java@@ -34,6 +34,7 @@ import org.apache.lucene.util.BytesRef;* <li>{@link #newExactQuery newExactQuery()} for matching an exact network address.* <li>{@link #newPrefixQuery newPrefixQuery()} for matching a network based on CIDR prefix.* <li>{@link #newRangeQuery newRangeQuery()} for matching arbitrary network address ranges.+ * <li>{@link #newSetQuery newSetQuery()} for matching a set of addresses.* </ul>* <p>* This field supports both IPv4 and IPv6 addresses: IPv4 addresses are converted@@ -193,11 +194,11 @@ public class InetAddressPoint extends Field {public static Query newRangeQuery(String field, InetAddress lowerValue, boolean lowerInclusive, InetAddress upperValue, boolean upperInclusive) {byte[][] lowerBytes = new byte[1][];if (lowerValue != null) {- lowerBytes[0] = InetAddressPoint.encode(lowerValue);+ lowerBytes[0] = encode(lowerValue);}byte[][] upperBytes = new byte[1][];if (upperValue != null) {- upperBytes[0] = InetAddressPoint.encode(upperValue);+ upperBytes[0] = encode(upperValue);}return new PointRangeQuery(field, lowerBytes, new boolean[] { lowerInclusive }, upperBytes, new boolean[] { upperInclusive }) {@Override@@ -207,5 +208,42 @@ public class InetAddressPoint extends Field {};}- // nocommit newSetQuery+ /**+ * Create a query matching any of the specified 1D values. This is the points equivalent of {@code TermsQuery}.+ *+ * @param field field name. must not be {@code null}.+ * @param valuesIn all int values to match+ */+ public static Query newSetQuery(String field, InetAddress... valuesIn) throws IOException {++ // Don't unexpectedly change the user's incoming values array:+ InetAddress[] values = valuesIn.clone();++ Arrays.sort(values);++ final BytesRef value = new BytesRef(new byte[BYTES]);++ return new PointInSetQuery(field, 1, BYTES,+ new BytesRefIterator() {++ int upto;++ @Override+ public BytesRef next() {+ if (upto == values.length) {+ return null;+ } else {+ encode(values[upto], value.bytes, 0);+ upto++;+ return value;+ }+ }+ }) {+ @Override+ protected String toString(byte[] value) {+ assert value.length == BYTES;+ return decode(value).getHostAddress();+ }+ };+ }}diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.javaindex 19b02b2542d..89a3f849dcc 100644--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java@@ -44,6 +44,8 @@ public class TestInetAddressPoint extends LuceneTestCase {assertEquals(1, searcher.count(InetAddressPoint.newExactQuery("field", address)));assertEquals(1, searcher.count(InetAddressPoint.newPrefixQuery("field", address, 24)));assertEquals(1, searcher.count(InetAddressPoint.newRangeQuery("field", InetAddress.getByName("1.2.3.3"), false, InetAddress.getByName("1.2.3.5"), false)));+ assertEquals(1, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.3")));+ assertEquals(0, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.4")));reader.close();writer.close();