Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

get multi-dim point-in-set testcase working; add explicit tests for 'no points queried' cases; fix last nocommits

Mike McCandlessa1f526928025
diff --git a/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java b/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
index b8cbc1e96b6..9c4de991aa0 100644
--- a/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
+import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
@@ -224,6 +225,11 @@ public final class BinaryPoint extends Field {
}
}
+ if (bytesPerDim == -1) {
+ // There are no points, and we cannot guess the bytesPerDim here, so we return an equivalent query:
+ return new MatchNoDocsQuery();
+ }
+
// Don't unexpectedly change the user's incoming values array:
byte[][] values = valuesIn.clone();
@@ -235,12 +241,6 @@ public final class BinaryPoint extends Field {
}
});
- // Silliness:
- if (bytesPerDim == -1) {
- // nocommit make sure this is tested
- bytesPerDim = 1;
- }
-
final BytesRef value = new BytesRef(new byte[bytesPerDim]);
return new PointInSetQuery(field, 1, bytesPerDim,
diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
index d34e3f79a93..348f598f27b 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
@@ -50,7 +50,7 @@ public class PointInSetQuery extends Query {
final int numDims;
final int bytesPerDim;
- /** In the 1D case, the {@code packedPoints} iterator must be in sorted order. */
+ /** The {@code packedPoints} iterator must be in sorted order. */
protected PointInSetQuery(String field, int numDims, int bytesPerDim, BytesRefIterator packedPoints) throws IOException {
this.field = field;
if (bytesPerDim < 1 || bytesPerDim > PointValues.MAX_NUM_BYTES) {
@@ -78,8 +78,8 @@ public class PointInSetQuery extends Query {
int cmp = previous.get().compareTo(current);
if (cmp == 0) {
continue; // deduplicate
- } else if (numDims == 1 && cmp > 0) {
- throw new IllegalArgumentException("numDims=1 and values are out of order: saw " + previous + " before " + current);
+ } else if (cmp > 0) {
+ throw new IllegalArgumentException("values are out of order: saw " + previous + " before " + current);
}
}
builder.add(field, current);
@@ -230,7 +230,6 @@ public class PointInSetQuery extends Query {
private final DocIdSetBuilder result;
private final int[] hitCount;
- public BytesRef point;
private final byte[] pointBytes;
public SinglePointVisitor(int[] hitCount, DocIdSetBuilder result) {
@@ -258,7 +257,7 @@ public class PointInSetQuery extends Query {
@Override
public void visit(int docID, byte[] packedValue) {
- assert packedValue.length == point.length;
+ assert packedValue.length == pointBytes.length;
if (Arrays.equals(packedValue, pointBytes)) {
// The point for this doc matches the point we are querying on
hitCount[0]++;
@@ -292,7 +291,6 @@ public class PointInSetQuery extends Query {
if (crosses) {
return Relation.CELL_CROSSES_QUERY;
} else {
- // nocommit make sure tests hit this case:
// NOTE: we only hit this if we are on a cell whose min and max values are exactly equal to our point,
// which can easily happen if many docs share this one value
return Relation.CELL_INSIDE_QUERY;
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
index 095ddc93043..4a232a650b6 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
+import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -64,6 +65,7 @@ import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.TestUtil;
import org.junit.BeforeClass;
@@ -1281,8 +1283,6 @@ public class TestPointQueries extends LuceneTestCase {
iwc.setCodec(getCodec());
RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
- // nocommit multi-valued too
-
int[] docValues = new int[numDocs];
for(int i=0;i<numDocs;i++) {
int x = values[random().nextInt(values.length)];
@@ -1370,7 +1370,150 @@ public class TestPointQueries extends LuceneTestCase {
IOUtils.close(r, dir);
}
- // nocommit need 2D test too
+ // TODO: in the future, if there is demand for real usage, we can "graduate" this test-only query factory as IntPoint.newMultiSetQuery or
+ // something (and same for other XXXPoint classes):
+ private static Query newMultiDimIntSetQuery(String field, final int numDims, int... valuesIn) throws IOException {
+ if (valuesIn.length % numDims != 0) {
+ throw new IllegalArgumentException("incongruent number of values: valuesIn.length=" + valuesIn.length + " but numDims=" + numDims);
+ }
+
+ // Pack all values:
+ byte[][] packedValues = new byte[valuesIn.length / numDims][];
+ for(int i=0;i<packedValues.length;i++) {
+ byte[] packedValue = new byte[numDims * Integer.BYTES];
+ packedValues[i] = packedValue;
+ for(int dim=0;dim<numDims;dim++) {
+ IntPoint.encodeDimension(valuesIn[i*numDims+dim], packedValue, dim*Integer.BYTES);
+ }
+ }
+
+ // Sort:
+ Arrays.sort(packedValues,
+ new Comparator<byte[]>() {
+ @Override
+ public int compare(byte[] a, byte[] b) {
+ return StringHelper.compare(a.length, a, 0, b, 0);
+ }
+ });
+
+ final BytesRef value = new BytesRef();
+ value.length = numDims * Integer.BYTES;
+
+ return new PointInSetQuery(field,
+ numDims,
+ Integer.BYTES,
+ new BytesRefIterator() {
+ int upto;
+ @Override
+ public BytesRef next() {
+ if (upto >= packedValues.length) {
+ return null;
+ }
+ value.bytes = packedValues[upto];
+ upto++;
+ return value;
+ }
+ }) {
+ @Override
+ protected String toString(byte[] value) {
+ assert value.length == numDims * Integer.BYTES;
+ StringBuilder sb = new StringBuilder();
+ for(int dim=0;dim<numDims;dim++) {
+ if (dim > 0) {
+ sb.append(',');
+ }
+ sb.append(Integer.toString(IntPoint.decodeDimension(value, dim*Integer.BYTES)));
+ }
+
+ return sb.toString();
+ }
+ };
+ }
+
+ public void testBasicMultiDimPointInSetQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setCodec(getCodec());
+ IndexWriter w = new IndexWriter(dir, iwc);
+
+ Document doc = new Document();
+ doc.add(new IntPoint("int", 17, 42));
+ w.addDocument(doc);
+ IndexReader r = DirectoryReader.open(w);
+ IndexSearcher s = newSearcher(r);
+
+ assertEquals(0, s.count(newMultiDimIntSetQuery("int", 2, 17, 41)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, 17, 42)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, -7, -7, 17, 42)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, 17, 42, -14, -14)));
+
+ w.close();
+ r.close();
+ dir.close();
+ }
+
+ public void testBasicMultiValueMultiDimPointInSetQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setCodec(getCodec());
+ IndexWriter w = new IndexWriter(dir, iwc);
+
+ Document doc = new Document();
+ doc.add(new IntPoint("int", 17, 42));
+ doc.add(new IntPoint("int", 34, 79));
+ w.addDocument(doc);
+ IndexReader r = DirectoryReader.open(w);
+ IndexSearcher s = newSearcher(r);
+
+ assertEquals(0, s.count(newMultiDimIntSetQuery("int", 2, 17, 41)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, 17, 42)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, 17, 42, 34, 79)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, -7, -7, 17, 42)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, -7, -7, 34, 79)));
+ assertEquals(1, s.count(newMultiDimIntSetQuery("int", 2, 17, 42, -14, -14)));
+
+ assertEquals("int:{-14,-14 17,42}", newMultiDimIntSetQuery("int", 2, 17, 42, -14, -14).toString());
+
+ w.close();
+ r.close();
+ dir.close();
+ }
+
+ public void testManyEqualValuesMultiDimPointInSetQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setCodec(getCodec());
+ IndexWriter w = new IndexWriter(dir, iwc);
+
+ int zeroCount = 0;
+ for(int i=0;i<10000;i++) {
+ int x = random().nextInt(2);
+ if (x == 0) {
+ zeroCount++;
+ }
+ Document doc = new Document();
+ doc.add(new IntPoint("int", x, x));
+ w.addDocument(doc);
+ }
+ IndexReader r = DirectoryReader.open(w);
+ IndexSearcher s = newSearcher(r);
+
+ assertEquals(zeroCount, s.count(newMultiDimIntSetQuery("int", 2, 0, 0)));
+ assertEquals(10000-zeroCount, s.count(newMultiDimIntSetQuery("int", 2, 1, 1)));
+ assertEquals(0, s.count(newMultiDimIntSetQuery("int", 2, 2, 2)));
+
+ w.close();
+ r.close();
+ dir.close();
+ }
+
+ public void testInvalidMultiDimPointInSetQuery() throws Exception {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class,
+ () -> {
+ newMultiDimIntSetQuery("int", 2, 3, 4, 5);
+ });
+ assertEquals("incongruent number of values: valuesIn.length=3 but numDims=2", expected.getMessage());
+ }
public void testBasicPointInSetQuery() throws Exception {
Directory dir = newDirectory();
@@ -1432,7 +1575,6 @@ public class TestPointQueries extends LuceneTestCase {
assertEquals(3, s.count(DoublePoint.newSetQuery("double", 17, 20, 42, 97)));
assertEquals(3, s.count(DoublePoint.newSetQuery("double", 17, 105, 42, 97)));
- // nocommit make sure invalid bytes length hits iae
assertEquals(0, s.count(BinaryPoint.newSetQuery("bytes", new byte[] {0, 16})));
assertEquals(1, s.count(BinaryPoint.newSetQuery("bytes", new byte[] {0, 17})));
assertEquals(3, s.count(BinaryPoint.newSetQuery("bytes", new byte[] {0, 17}, new byte[] {0, 97}, new byte[] {0, 42})));
@@ -1501,6 +1643,33 @@ public class TestPointQueries extends LuceneTestCase {
dir.close();
}
+ public void testEmptyPointInSetQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setCodec(getCodec());
+ IndexWriter w = new IndexWriter(dir, iwc);
+
+ Document doc = new Document();
+ doc.add(new IntPoint("int", 17));
+ doc.add(new LongPoint("long", 17L));
+ doc.add(new FloatPoint("float", 17.0f));
+ doc.add(new DoublePoint("double", 17.0));
+ doc.add(new BinaryPoint("bytes", new byte[] {0, 17}));
+ w.addDocument(doc);
+
+ IndexReader r = DirectoryReader.open(w);
+ IndexSearcher s = newSearcher(r);
+ assertEquals(0, s.count(IntPoint.newSetQuery("int")));
+ assertEquals(0, s.count(LongPoint.newSetQuery("long")));
+ assertEquals(0, s.count(FloatPoint.newSetQuery("float")));
+ assertEquals(0, s.count(DoublePoint.newSetQuery("double")));
+ assertEquals(0, s.count(BinaryPoint.newSetQuery("bytes")));
+
+ w.close();
+ r.close();
+ dir.close();
+ }
+
public void testPointInSetQueryManyEqualValues() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java
index 6b0d6963952..b5d80228e15 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java
@@ -45,6 +45,7 @@ public class TestBigIntegerPoint extends LuceneTestCase {
assertEquals(1, searcher.count(BigIntegerPoint.newRangeQuery("field", large.subtract(BigInteger.ONE), false, large.add(BigInteger.ONE), false)));
assertEquals(1, searcher.count(BigIntegerPoint.newSetQuery("field", large)));
assertEquals(0, searcher.count(BigIntegerPoint.newSetQuery("field", large.subtract(BigInteger.ONE))));
+ assertEquals(0, searcher.count(BigIntegerPoint.newSetQuery("field"));
reader.close();
writer.close();
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java
index 500edefa51c..b2f41c4f346 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java
@@ -46,6 +46,7 @@ public class TestInetAddressPoint extends LuceneTestCase {
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.4"))));
assertEquals(0, searcher.count(InetAddressPoint.newSetQuery("field", InetAddress.getByName("1.2.3.3"))));
+ assertEquals(0, searcher.count(InetAddressPoint.newSetQuery("field"));
reader.close();
writer.close();