Apache Lucene open-source search software
Merge branch 'master' into point_set_query
Mike McCandless8a4c19777e46
diff --cc lucene/core/src/java/org/apache/lucene/document/IntPoint.javaindex 42091d95281,177ae9955d6..d4bb28a3552--- a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java+++ b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java@@@ -16,19 -16,23 +16,28 @@@*/package org.apache.lucene.document;+import java.io.IOException;+import java.util.Arrays;++import org.apache.lucene.search.PointInSetQuery;+ import org.apache.lucene.search.PointRangeQuery;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.BytesRefIterator;import org.apache.lucene.util.NumericUtils;- /** An int field that is indexed dimensionally such that finding- * all documents within an N-dimensional shape or range at search time is- * efficient. Multiple values for the same field in one documents- * is allowed. */-+ /**+ * An int field that is indexed dimensionally such that finding+ * all documents within an N-dimensional shape or range at search time is+ * efficient. Multiple values for the same field in one documents+ * is allowed.+ * <p>+ * This field defines static factory methods for creating common queries:+ * <ul>+ * <li>{@link #newExactQuery newExactQuery()} for matching an exact 1D point.+ * <li>{@link #newRangeQuery newRangeQuery()} for matching a 1D range.+ * <li>{@link #newMultiRangeQuery newMultiRangeQuery()} for matching points/ranges in n-dimensional space.+ * </ul>+ */public final class IntPoint extends Field {private static FieldType getType(int numDims) {@@@ -171,4 -140,76 +145,116 @@@public static Integer decodeDimension(byte value[], int offset) {return NumericUtils.bytesToInt(value, offset);}++ // static methods for generating queries++ /**+ * Create a query for matching an exact integer value.+ * <p>+ * This is for simple one-dimension points, for multidimensional points use+ * {@link #newMultiRangeQuery newMultiRangeQuery()} instead.+ *+ * @param field field name. must not be {@code null}.+ * @param value exact value+ * @throws IllegalArgumentException if {@code field} is null.+ * @return a query matching documents with this exact value+ */+ public static PointRangeQuery newExactQuery(String field, int value) {+ return newRangeQuery(field, value, true, value, true);+ }++ /**+ * Create a range query for integer values.+ * <p>+ * This is for simple one-dimension ranges, for multidimensional ranges use+ * {@link #newMultiRangeQuery newMultiRangeQuery()} instead.+ * <p>+ * You can have half-open ranges (which are in fact </≤ or >/≥ queries)+ * by setting the {@code lowerValue} or {@code upperValue} to {@code null}.+ * <p>+ * By setting inclusive ({@code lowerInclusive} or {@code upperInclusive}) to false, it will+ * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.+ *+ * @param field field name. must not be {@code null}.+ * @param lowerValue lower portion of the range. {@code null} means "open".+ * @param lowerInclusive {@code true} if the lower portion of the range is inclusive, {@code false} if it should be excluded.+ * @param upperValue upper portion of the range. {@code null} means "open".+ * @param upperInclusive {@code true} if the upper portion of the range is inclusive, {@code false} if it should be excluded.+ * @throws IllegalArgumentException if {@code field} is null.+ * @return a query matching documents within this range.+ */+ public static PointRangeQuery newRangeQuery(String field, Integer lowerValue, boolean lowerInclusive, Integer upperValue, boolean upperInclusive) {+ return newMultiRangeQuery(field,+ new Integer[] { lowerValue },+ new boolean[] { lowerInclusive },+ new Integer[] { upperValue },+ new boolean[] { upperInclusive });+ }++ /**+ * Create a multidimensional range query for integer values.+ * <p>+ * You can have half-open ranges (which are in fact </≤ or >/≥ queries)+ * by setting a {@code lowerValue} element or {@code upperValue} element to {@code null}.+ * <p>+ * By setting a dimension's inclusive ({@code lowerInclusive} or {@code upperInclusive}) to false, it will+ * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.+ *+ * @param field field name. must not be {@code null}.+ * @param lowerValue lower portion of the range. {@code null} values mean "open" for that dimension.+ * @param lowerInclusive {@code true} if the lower portion of the range is inclusive, {@code false} if it should be excluded.+ * @param upperValue upper portion of the range. {@code null} values mean "open" for that dimension.+ * @param upperInclusive {@code true} if the upper portion of the range is inclusive, {@code false} if it should be excluded.+ * @throws IllegalArgumentException if {@code field} is null, or if {@code lowerValue.length != upperValue.length}+ * @return a query matching documents within this range.+ */+ public static PointRangeQuery newMultiRangeQuery(String field, Integer[] lowerValue, boolean lowerInclusive[], Integer[] upperValue, boolean upperInclusive[]) {+ PointRangeQuery.checkArgs(field, lowerValue, upperValue);+ return new PointRangeQuery(field, IntPoint.encode(lowerValue), lowerInclusive, IntPoint.encode(upperValue), upperInclusive) {+ @Override+ protected String toString(byte[] value) {+ return IntPoint.decodeDimension(value, 0).toString();+ }+ };+ }++++ /**++ * Returns a query efficiently finding all documents indexed with any of the specified 1D values.++ *++ * @param field field name. must not be {@code null}.++ * @param valuesIn all int values to search for++ */++ public static PointInSetQuery newSetQuery(String field, int... valuesIn) throws IOException {++++ // Don't unexpectedly change the user's incoming array:++ int[] values = valuesIn.clone();++++ Arrays.sort(values);++++ final BytesRef value = new BytesRef(new byte[Integer.BYTES]);++ value.length = Integer.BYTES;++++ return new PointInSetQuery(field, 1, Integer.BYTES,++ new BytesRefIterator() {++++ int upto;++++ @Override++ public BytesRef next() {++ if (upto == values.length) {++ return null;++ } else {++ IntPoint.encodeDimension(values[upto], value.bytes, 0);++ upto++;++ return value;++ }++ }++ }) {++ @Override++ protected String toString(byte[] value) {++ assert value.length == Integer.BYTES;++ return Integer.toString(decodeDimension(value, 0));++ }++ };++ }}