Apache Solr open-source search software
LUCENE-7232: Fixed InetAddressPoint.newPrefixQuery.
Adrien Grand2724b99b5c40
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7e58dabdcc1..87550b174ab 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -90,6 +90,9 @@ Bug Fixes* LUCENE-7209: Fixed explanations of FunctionScoreQuery. (Adrien Grand)+* LUCENE-7232: Fixed InetAddressPoint.newPrefixQuery, which was generating an+ incorrect query when the prefix length was not a multiple of 8. (Adrien Grand)+Documentation* LUCENE-7223: Improve XXXPoint javadocs to make it clear that youdiff --git a/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java b/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.javaindex 7ebabab02eb..a445f2384ca 100644--- a/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java+++ b/lucene/sandbox/src/java/org/apache/lucene/document/InetAddressPoint.java@@ -174,8 +174,9 @@ public class InetAddressPoint extends Field {byte lower[] = value.getAddress();byte upper[] = value.getAddress();for (int i = prefixLength; i < 8 * lower.length; i++) {- lower[i >> 3] &= ~(1 << (i & 7));- upper[i >> 3] |= 1 << (i & 7);+ int m = 1 << (7 - (i & 7));+ lower[i >> 3] &= ~m;+ upper[i >> 3] |= m;}try {return newRangeQuery(field, InetAddress.getByAddress(lower), InetAddress.getByAddress(upper));diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.javaindex 673ee296a48..0e30c18a9b1 100644--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java@@ -119,4 +119,16 @@ public class TestInetAddressPoint extends LuceneTestCase {assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(InetAddressPoint.newSetQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.7"))));}++ public void testPrefixQuery() throws Exception {+ assertEquals(+ InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.0"), InetAddress.getByName("1.2.3.255")),+ InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.127"), 24));+ assertEquals(+ InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.128"), InetAddress.getByName("1.2.3.255")),+ InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.213"), 25));+ assertEquals(+ InetAddressPoint.newRangeQuery("a", InetAddress.getByName("2001::a000:0"), InetAddress.getByName("2001::afff:ffff")),+ InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("2001::a6bd:fc80"), 100));+ }}