From 42361a68bc27266d8f50e90b85ffd980ac953f36 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 9 Mar 2016 11:25:37 -0500 Subject: [PATCH] LUCENE-7079: add newSetQuery(String, Collection) to primitive Point types --- .../org/apache/lucene/document/DoublePoint.java | 16 ++++++++++++++++ .../org/apache/lucene/document/FloatPoint.java | 16 ++++++++++++++++ .../org/apache/lucene/document/IntPoint.java | 16 ++++++++++++++++ .../org/apache/lucene/document/LongPoint.java | 16 ++++++++++++++++ .../apache/lucene/search/TestPointQueries.java | 8 ++++++++ .../apache/lucene/search/join/TestBlockJoin.java | 7 +------ 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java b/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java index 26ac0ced6f1..1133b221393 100644 --- a/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java +++ b/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java @@ -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 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); + } } diff --git a/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java b/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java index c58881ec880..3d110db2cd8 100644 --- a/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java +++ b/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java @@ -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 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); + } } diff --git a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java index cb8315f2f20..53ae3d3dd2a 100644 --- a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java +++ b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java @@ -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 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); + } } diff --git a/lucene/core/src/java/org/apache/lucene/document/LongPoint.java b/lucene/core/src/java/org/apache/lucene/document/LongPoint.java index ff78132d7b6..c4fd8875205 100644 --- a/lucene/core/src/java/org/apache/lucene/document/LongPoint.java +++ b/lucene/core/src/java/org/apache/lucene/document/LongPoint.java @@ -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 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); + } } 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 4d9aa593f06..c72ab44ac39 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java @@ -1602,6 +1602,14 @@ public class TestPointQueries extends LuceneTestCase { r.close(); 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(); diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java index b5f2038a0dc..9c39299c4b4 100644 --- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java +++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java @@ -615,12 +615,7 @@ public class TestBlockJoin extends LuceneTestCase { } if (!toDelete.isEmpty()) { - // TODO: we should add newSetQuery(String, Collection) ? 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); }