LUCENE-7079: add newSetQuery(String, Collection) to primitive Point types

This commit is contained in:
Robert Muir 2016-03-09 11:25:37 -05:00
parent 5a43a3e772
commit 42361a68bc
6 changed files with 73 additions and 6 deletions

View File

@ -17,6 +17,7 @@
package org.apache.lucene.document;
import java.util.Arrays;
import java.util.Collection;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.PointInSetQuery;
@ -247,4 +248,19 @@ public final class DoublePoint extends Field {
}
};
}
/**
* 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 values all values to match
*/
public static Query newSetQuery(String field, Collection<Double> values) {
Double[] boxed = values.toArray(new Double[0]);
double[] unboxed = new double[boxed.length];
for (int i = 0; i < boxed.length; i++) {
unboxed[i] = boxed[i];
}
return newSetQuery(field, unboxed);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.lucene.document;
import java.util.Arrays;
import java.util.Collection;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.PointInSetQuery;
@ -247,4 +248,19 @@ public final class FloatPoint extends Field {
}
};
}
/**
* 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 values all values to match
*/
public static Query newSetQuery(String field, Collection<Float> values) {
Float[] boxed = values.toArray(new Float[0]);
float[] unboxed = new float[boxed.length];
for (int i = 0; i < boxed.length; i++) {
unboxed[i] = boxed[i];
}
return newSetQuery(field, unboxed);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.lucene.document;
import java.util.Arrays;
import java.util.Collection;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.PointInSetQuery;
@ -247,4 +248,19 @@ public final class IntPoint extends Field {
}
};
}
/**
* 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 values all values to match
*/
public static Query newSetQuery(String field, Collection<Integer> values) {
Integer[] boxed = values.toArray(new Integer[0]);
int[] unboxed = new int[boxed.length];
for (int i = 0; i < boxed.length; i++) {
unboxed[i] = boxed[i];
}
return newSetQuery(field, unboxed);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.lucene.document;
import java.util.Arrays;
import java.util.Collection;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.PointInSetQuery;
@ -247,4 +248,19 @@ public final class LongPoint extends Field {
}
};
}
/**
* 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 values all values to match
*/
public static Query newSetQuery(String field, Collection<Long> values) {
Long[] boxed = values.toArray(new Long[0]);
long[] unboxed = new long[boxed.length];
for (int i = 0; i < boxed.length; i++) {
unboxed[i] = boxed[i];
}
return newSetQuery(field, unboxed);
}
}

View File

@ -1603,6 +1603,14 @@ public class TestPointQueries extends LuceneTestCase {
dir.close();
}
/** Boxed methods for primitive types should behave the same as unboxed: just sugar */
public void testPointIntSetBoxed() throws Exception {
assertEquals(IntPoint.newSetQuery("foo", 1, 2, 3), IntPoint.newSetQuery("foo", Arrays.asList(1, 2, 3)));
assertEquals(FloatPoint.newSetQuery("foo", 1F, 2F, 3F), FloatPoint.newSetQuery("foo", Arrays.asList(1F, 2F, 3F)));
assertEquals(LongPoint.newSetQuery("foo", 1L, 2L, 3L), LongPoint.newSetQuery("foo", Arrays.asList(1L, 2L, 3L)));
assertEquals(DoublePoint.newSetQuery("foo", 1D, 2D, 3D), DoublePoint.newSetQuery("foo", Arrays.asList(1D, 2D, 3D)));
}
public void testBasicMultiValuedPointInSetQuery() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();

View File

@ -615,12 +615,7 @@ public class TestBlockJoin extends LuceneTestCase {
}
if (!toDelete.isEmpty()) {
// TODO: we should add newSetQuery(String, Collection<T>) ? this is awkward.
int[] array = new int[toDelete.size()];
for (int i = 0; i < toDelete.size(); i++) {
array[i] = toDelete.get(i);
}
Query query = IntPoint.newSetQuery("blockID", array);
Query query = IntPoint.newSetQuery("blockID", toDelete);
w.deleteDocuments(query);
joinW.deleteDocuments(query);
}