LUCENE-7307: Add getters to the PointInSetQuery and PointRangeQuery queries.

This commit is contained in:
Martijn van Groningen 2016-06-01 11:42:00 +02:00
parent 9863d3d2d7
commit b977275185
4 changed files with 101 additions and 0 deletions

View File

@ -172,6 +172,9 @@ Other
* LUCENE-7206: Improve the ToParentBlockJoinQuery's explain by including the explain
of the best matching child doc. (Ilya Kasnacheev, Jeff Evans via Martijn van Groningen)
* LUCENE-7307: Add getters to the PointInSetQuery and PointRangeQuery queries.
(Martijn van Groningen, Adrien Grand)
Build
* LUCENE-7292: Use '-release' instead of '-source/-target' during

View File

@ -17,7 +17,15 @@
package org.apache.lucene.search;
import java.io.IOException;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.FieldInfo;
@ -28,6 +36,7 @@ import org.apache.lucene.index.PointValues.Relation;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.index.PrefixCodedTerms.TermIterator;
import org.apache.lucene.index.PrefixCodedTerms;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.BytesRefIterator;
@ -303,6 +312,54 @@ public abstract class PointInSetQuery extends Query {
}
}
public Collection<byte[]> getPackedPoints() {
return new AbstractCollection<byte[]>() {
@Override
public Iterator<byte[]> iterator() {
int size = (int) sortedPackedPoints.size();
PrefixCodedTerms.TermIterator iterator = sortedPackedPoints.iterator();
return new Iterator<byte[]>() {
int upto = 0;
@Override
public boolean hasNext() {
return upto < size;
}
@Override
public byte[] next() {
if (upto == size) {
throw new NoSuchElementException();
}
upto++;
BytesRef next = iterator.next();
return Arrays.copyOfRange(next.bytes, next.offset, next.length);
}
};
}
@Override
public int size() {
return (int) sortedPackedPoints.size();
}
};
}
public String getField() {
return field;
}
public int getNumDims() {
return numDims;
}
public int getBytesPerDim() {
return bytesPerDim;
}
@Override
public final int hashCode() {
int hash = classHash();

View File

@ -218,6 +218,26 @@ public abstract class PointRangeQuery extends Query {
};
}
public String getField() {
return field;
}
public int getNumDims() {
return numDims;
}
public int getBytesPerDim() {
return bytesPerDim;
}
public byte[] getLowerPoint() {
return lowerPoint.clone();
}
public byte[] getUpperPoint() {
return upperPoint.clone();
}
@Override
public final int hashCode() {
int hash = classHash();

View File

@ -22,9 +22,12 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
@ -1854,6 +1857,24 @@ public class TestPointQueries extends LuceneTestCase {
assertEquals("bytes:{[12] [2a]}", BinaryPoint.newSetQuery("bytes", new byte[] {42}, new byte[] {18}).toString());
}
public void testPointInSetQueryGetPackedPoints() throws Exception {
int numValues = randomIntValue(1, 32);
List<byte[]> values = new ArrayList<>(numValues);
for (byte i = 0; i < numValues; i++) {
values.add(new byte[]{i});
}
PointInSetQuery query = (PointInSetQuery) BinaryPoint.newSetQuery("field", values.toArray(new byte[][]{}));
Collection<byte[]> packedPoints = query.getPackedPoints();
assertEquals(numValues, packedPoints.size());
Iterator<byte[]> iterator = packedPoints.iterator();
for (byte[] expectedValue : values) {
assertArrayEquals(expectedValue, iterator.next());
}
expectThrows(NoSuchElementException.class, () -> iterator.next());
assertFalse(iterator.hasNext());
}
public void testRangeOptimizesIfAllPointsMatch() throws IOException {
final int numDims = TestUtil.nextInt(random(), 1, 3);
Directory dir = newDirectory();