/** * Add a field to the document. * * @return true if the field was successfully added */ public static boolean addField(Document doc, String field, Object fieldValue) { Class<?> clazz = fieldValue.getClass(); if (clazz == String.class) { doc.add(new TextField(field, (String) fieldValue, Store.NO)); } else if (clazz == Long.class) { doc.add(new LongPoint(field, (Long) fieldValue)); } else if (clazz == Integer.class) { doc.add(new IntPoint(field, (Integer) fieldValue)); } else if (clazz == Float.class) { doc.add(new FloatPoint(field, (Float) fieldValue)); } else if (clazz == Double.class) { doc.add(new DoublePoint(field, (Double) fieldValue)); } else { return false; } return true; }
/** * Create a query for matching an exact float value. * <p> * This is for simple one-dimension points, for multidimensional points use * {@link #newRangeQuery(String, float[], float[])} instead. * * @param field field name. must not be {@code null}. * @param value float value * @throws IllegalArgumentException if {@code field} is null. * @return a query matching documents with this exact value */ public static Query newExactQuery(String field, float value) { return newRangeQuery(field, value, value); }
@Override protected String toString(int dimension, byte[] value) { return Float.toString(decodeDimension(value, 0)); } };
@Override public Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, boolean hasDocValues) { float l = Float.NEGATIVE_INFINITY; float u = Float.POSITIVE_INFINITY; if (lowerTerm != null) { l = parse(lowerTerm, false); if (includeLower == false) { l = FloatPoint.nextUp(l); } } if (upperTerm != null) { u = parse(upperTerm, false); if (includeUpper == false) { u = FloatPoint.nextDown(u); } } Query query = FloatPoint.newRangeQuery(field, l, u); if (hasDocValues) { Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, NumericUtils.floatToSortableInt(l), NumericUtils.floatToSortableInt(u)); query = new IndexOrDocValuesQuery(query, dvQuery); } return query; }
/** * 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); } }
@Override public Query termQuery(String field, Object value) { float v = parse(value, false); return FloatPoint.newExactQuery(field, v); }
@Override Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper) { float l = Float.NEGATIVE_INFINITY; float u = Float.POSITIVE_INFINITY; if (lowerTerm != null) { l = parse(lowerTerm, false); if (includeLower == false) { l = FloatPoint.nextUp(l); } } if (upperTerm != null) { u = parse(upperTerm, false); if (includeUpper == false) { u = FloatPoint.nextDown(u); } } return FloatPoint.newRangeQuery(field, l, u); }
@Override public Query termsQuery(String field, List<Object> values) { float[] v = new float[values.size()]; for (int i = 0; i < values.size(); ++i) { v[i] = parse(values.get(i), false); } return FloatPoint.newSetQuery(field, v); }
@Override public Query toTermQuery(final String name, final String text) { return FloatPoint.newExactQuery(name, toFloat(text)); }
@Override public List<Field> createFields(String name, Number value, boolean indexed, boolean docValued, boolean stored) { List<Field> fields = new ArrayList<>(); if (indexed) { fields.add(new FloatPoint(name, value.floatValue())); } if (docValued) { fields.add(new SortedNumericDocValuesField(name, NumericUtils.floatToSortableInt(value.floatValue()))); } if (stored) { fields.add(new StoredField(name, value.floatValue())); } return fields; }
@Override public Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, boolean hasDocValues) { float l = Float.NEGATIVE_INFINITY; float u = Float.POSITIVE_INFINITY; if (lowerTerm != null) { l = parse(lowerTerm, false); if (includeLower == false) { l = FloatPoint.nextUp(l); } } if (upperTerm != null) { u = parse(upperTerm, false); if (includeUpper == false) { u = FloatPoint.nextDown(u); } } Query query = FloatPoint.newRangeQuery(field, l, u); if (hasDocValues) { Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, NumericUtils.floatToSortableInt(l), NumericUtils.floatToSortableInt(u)); query = new IndexOrDocValuesQuery(query, dvQuery); } return query; }
@Override public Query getQuery(LuceneIndex index) throws LuceneQueryException { if (luceneQuery == null) { luceneQuery = FloatPoint.newRangeQuery(fieldName, lowerValue, upperValue); // luceneQuery = DoublePoint.newRangeQuery(fieldName, lowerValue, upperValue); } System.out.println("IntRangeQueryProvider, using java serializable"); return luceneQuery; } }
@Override protected String toString(byte[] value) { assert value.length == Float.BYTES; return Float.toString(decodeDimension(value, 0)); } };
/** * 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); } }
@Override public Query termQuery(String field, Object value) { float v = parse(value, false); return FloatPoint.newExactQuery(field, v); }
@Override public void addFields(final String name, final Object value, final ViewSettings settings, final Document to) { to.add(boost(new FloatPoint(name, toFloat(value)), settings)); to.add(new FloatDocValuesField(name, toFloat(value))); }
@Override public Query rangeQuery(String field, Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, boolean hasDocValues) { float l = Float.NEGATIVE_INFINITY; float u = Float.POSITIVE_INFINITY; if (lowerTerm != null) { l = parse(lowerTerm, false); if (includeLower == false) { l = FloatPoint.nextUp(l); } } if (upperTerm != null) { u = parse(upperTerm, false); if (includeUpper == false) { u = FloatPoint.nextDown(u); } } Query query = FloatPoint.newRangeQuery(field, l, u); if (hasDocValues) { Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(field, NumericUtils.floatToSortableInt(l), NumericUtils.floatToSortableInt(u)); query = new IndexOrDocValuesQuery(query, dvQuery); } return query; }
/** * Create a range query for float values. * <p> * This is for simple one-dimension ranges, for multidimensional ranges use * {@link #newRangeQuery(String, float[], float[])} instead. * <p> * You can have half-open ranges (which are in fact </≤ or >/≥ queries) * by setting {@code lowerValue = Float.NEGATIVE_INFINITY} or {@code upperValue = Float.POSITIVE_INFINITY}. * <p> Ranges are inclusive. For exclusive ranges, pass {@link #nextUp(float) nextUp(lowerValue)} * or {@link #nextUp(float) nextDown(upperValue)}. * <p> * Range comparisons are consistent with {@link Float#compareTo(Float)}. * * @param field field name. must not be {@code null}. * @param lowerValue lower portion of the range (inclusive). * @param upperValue upper portion of the range (inclusive). * @throws IllegalArgumentException if {@code field} is null. * @return a query matching documents within this range. */ public static Query newRangeQuery(String field, float lowerValue, float upperValue) { return newRangeQuery(field, new float[] { lowerValue }, new float[] { upperValue }); }
@Override public String toString() { StringBuilder result = new StringBuilder(); result.append(getClass().getSimpleName()); result.append(" <"); result.append(name); result.append(':'); BytesRef bytes = (BytesRef) fieldsData; for (int dim = 0; dim < type.pointDataDimensionCount(); dim++) { if (dim > 0) { result.append(','); } result.append(decodeDimension(bytes.bytes, bytes.offset + dim * Float.BYTES)); } result.append('>'); return result.toString(); }
@Override Query termsQuery(String field, List<Object> values) { float[] v = new float[values.size()]; for (int i = 0; i < values.size(); ++i) { v[i] = parse(values.get(i), false); } return FloatPoint.newSetQuery(field, v); }