private static NumericType getNumericDataType(Number number) throws QueryNodeException { if (number instanceof Long) { return NumericType.LONG; } else if (number instanceof Integer) { return NumericType.INT; } else if (number instanceof Double) { return NumericType.DOUBLE; } else if (number instanceof Float) { return NumericType.FLOAT; } else { throw new QueryNodeException( new MessageImpl( QueryParserMessages.NUMBER_CLASS_NOT_SUPPORTED_BY_NUMERIC_RANGE_QUERY, number.getClass())); } }
private static void setFieldValues(NumberType numberType, HashMap<String,Field> numericFieldMap) { Number number = getNumberType(numberType, NumericType.DOUBLE .name()); numericFieldMap.get(NumericType.DOUBLE.name()).setDoubleValue( number.doubleValue()); number = getNumberType(numberType, NumericType.INT.name()); numericFieldMap.get(NumericType.INT.name()).setIntValue( number.intValue()); number = getNumberType(numberType, NumericType.LONG.name()); numericFieldMap.get(NumericType.LONG.name()).setLongValue( number.longValue()); number = getNumberType(numberType, NumericType.FLOAT.name()); numericFieldMap.get(NumericType.FLOAT.name()).setFloatValue( number.floatValue()); number = getNumberType(numberType, DATE_FIELD_NAME); numericFieldMap.get(DATE_FIELD_NAME).setLongValue(number.longValue()); }
public void assertSimpleQuery(NumberType numberType, int expectedDocCount) throws QueryNodeException, IOException { StringBuilder sb = new StringBuilder(); for (NumericType type : NumericType.values()) { String numberStr = numberToString(getNumberType(numberType, type.name())); sb.append('+').append(type.name()).append(":\"").append(numberStr) .append("\" "); } String dateStr = ESCAPER.escape( DATE_FORMAT.format(new Date(getNumberType(numberType, DATE_FIELD_NAME) .longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString(); sb.append('+').append(DATE_FIELD_NAME).append(":\"").append(dateStr) .append('"'); testQuery(sb.toString(), expectedDocCount); }
@Override public NumericType getNumericType() { switch (type) { case INTEGER: return NumericType.INT; case LONG: case DATE: return NumericType.LONG; case FLOAT: return NumericType.FLOAT; case DOUBLE: return NumericType.DOUBLE; default: throw new AssertionError(); } }
private NumericRangeQuery(final String field, final int precisionStep, final NumericType dataType, T min, T max, final boolean minInclusive, final boolean maxInclusive ) { super(field); if (precisionStep < 1) throw new IllegalArgumentException("precisionStep must be >=1"); this.precisionStep = precisionStep; this.dataType = dataType; this.min = min; this.max = max; this.minInclusive = minInclusive; this.maxInclusive = maxInclusive; }
/** * Sets the upper and lower bounds of this range query node and the * {@link NumericConfig} associated with these bounds. * * @param lower the lower bound * @param upper the upper bound * @param lowerInclusive <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code> * @param upperInclusive <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code> * @param numericConfig the {@link NumericConfig} that represents associated with the upper and lower bounds * */ public void setBounds(NumericQueryNode lower, NumericQueryNode upper, boolean lowerInclusive, boolean upperInclusive, NumericConfig numericConfig) throws QueryNodeException { if (numericConfig == null) { throw new IllegalArgumentException("numericConfig cannot be null!"); } NumericType lowerNumberType, upperNumberType; if (lower != null && lower.getValue() != null) { lowerNumberType = getNumericDataType(lower.getValue()); } else { lowerNumberType = null; } if (upper != null && upper.getValue() != null) { upperNumberType = getNumericDataType(upper.getValue()); } else { upperNumberType = null; } if (lowerNumberType != null && !lowerNumberType.equals(numericConfig.getType())) { throw new IllegalArgumentException( "lower value's type should be the same as numericConfig type: " + lowerNumberType + " != " + numericConfig.getType()); } if (upperNumberType != null && !upperNumberType.equals(numericConfig.getType())) { throw new IllegalArgumentException( "upper value's type should be the same as numericConfig type: " + upperNumberType + " != " + numericConfig.getType()); } super.setBounds(lower, upper, lowerInclusive, upperInclusive); this.numericConfig = numericConfig; }
private static Query extractQuery(IndexSchema schema, String term, String value) { String field = term; if (hasBlockJoinHint(term)) { field = term.split(BLOCK_JOIN_PATH_HINT)[1]; } if (field.indexOf(":") > -1) { String[] keyValue = field.split(":"); field = keyValue[0]; String query = keyValue[1]; return buildQueryFromText(field, schema, query); } else if (value.matches("^.*:\\[.*\\sTO\\s.*\\]$")) { return buildQueryFromText(field, schema, value); } else if (null != tryParseDate(value)) { if (schema.getField(field).getType() instanceof TrieDateField) { return new TrieDateField().getFieldQuery(null, schema.getField(field), value); } throw new RuntimeException("Can not group on date field not a TrieDateField"); } else if (value.matches("^-{0,1}[0-9]+")) { // number FieldType type = schema.getField(field).getType(); NumericType numericType = type.getNumericType(); if (numericType == NumericType.FLOAT) { return new TrieFloatField().getFieldQuery(null, schema.getField(field), value); } else if (numericType == NumericType.INT) { return new TrieIntField().getFieldQuery(null, schema.getField(field), value); } else if (numericType == NumericType.LONG) { return new TrieLongField().getFieldQuery(null, schema.getField(field), value); } else if (numericType == NumericType.DOUBLE) { return new TrieDoubleField().getFieldQuery(null, schema.getField(field), value); } else { return new WildcardQuery(new Term(field, null != value ? value : "*")); } } else { return new WildcardQuery(new Term(field, null != value ? value : "*")); } }
@Override public NumericRangeQuery<? extends Number> build(QueryNode queryNode) throws QueryNodeException { NumericRangeQueryNode numericRangeNode = (NumericRangeQueryNode) queryNode; NumericQueryNode lowerNumericNode = numericRangeNode.getLowerBound(); NumericQueryNode upperNumericNode = numericRangeNode.getUpperBound(); Number lowerNumber = lowerNumericNode.getValue(); Number upperNumber = upperNumericNode.getValue(); NumericConfig numericConfig = numericRangeNode.getNumericConfig(); NumericType numberType = numericConfig.getType(); String field = StringUtils.toString(numericRangeNode.getField()); boolean minInclusive = numericRangeNode.isLowerInclusive(); boolean maxInclusive = numericRangeNode.isUpperInclusive(); int precisionStep = numericConfig.getPrecisionStep(); switch (numberType) { case LONG: return NumericRangeQuery.newLongRange(field, precisionStep, (Long) lowerNumber, (Long) upperNumber, minInclusive, maxInclusive); case INT: return NumericRangeQuery.newIntRange(field, precisionStep, (Integer) lowerNumber, (Integer) upperNumber, minInclusive, maxInclusive); case FLOAT: return NumericRangeQuery.newFloatRange(field, precisionStep, (Float) lowerNumber, (Float) upperNumber, minInclusive, maxInclusive); case DOUBLE: return NumericRangeQuery.newDoubleRange(field, precisionStep, (Double) lowerNumber, (Double) upperNumber, minInclusive, maxInclusive); default : throw new QueryNodeException(new MessageImpl( QueryParserMessages.UNSUPPORTED_NUMERIC_DATA_TYPE, numberType)); } }
public void assertRangeQuery(NumberType lowerType, NumberType upperType, boolean lowerInclusive, boolean upperInclusive, int expectedDocCount) throws QueryNodeException, IOException { StringBuilder sb = new StringBuilder(); String lowerInclusiveStr = (lowerInclusive ? "[" : "{"); String upperInclusiveStr = (upperInclusive ? "]" : "}"); for (NumericType type : NumericType.values()) { String lowerStr = numberToString(getNumberType(lowerType, type.name())); String upperStr = numberToString(getNumberType(upperType, type.name())); sb.append("+").append(type.name()).append(':').append(lowerInclusiveStr) .append('"').append(lowerStr).append("\" TO \"").append(upperStr) .append('"').append(upperInclusiveStr).append(' '); } Number lowerDateNumber = getNumberType(lowerType, DATE_FIELD_NAME); Number upperDateNumber = getNumberType(upperType, DATE_FIELD_NAME); String lowerDateStr; String upperDateStr; if (lowerDateNumber != null) { lowerDateStr = ESCAPER.escape( DATE_FORMAT.format(new Date(lowerDateNumber.longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString(); } else { lowerDateStr = "*"; } if (upperDateNumber != null) { upperDateStr = ESCAPER.escape( DATE_FORMAT.format(new Date(upperDateNumber.longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString(); } else { upperDateStr = "*"; } sb.append("+").append(DATE_FIELD_NAME).append(':') .append(lowerInclusiveStr).append('"').append(lowerDateStr).append( "\" TO \"").append(upperDateStr).append('"').append( upperInclusiveStr); testQuery(sb.toString(), expectedDocCount); }
public void assertOpenRangeQuery(NumberType boundType, String operator, int expectedDocCount) throws QueryNodeException, IOException { StringBuilder sb = new StringBuilder(); for (NumericType type : NumericType.values()) { String boundStr = numberToString(getNumberType(boundType, type.name())); sb.append("+").append(type.name()).append(operator).append('"').append(boundStr).append('"').append(' '); } String boundDateStr = ESCAPER.escape( DATE_FORMAT.format(new Date(getNumberType(boundType, DATE_FIELD_NAME) .longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString(); sb.append("+").append(DATE_FIELD_NAME).append(operator).append('"').append(boundDateStr).append('"'); testQuery(sb.toString(), expectedDocCount); }
public void testEquals() throws Exception { FieldType ft = new FieldType(); assertEquals(ft, ft); assertFalse(ft.equals(null)); FieldType ft2 = new FieldType(); assertEquals(ft, ft2); assertEquals(ft.hashCode(), ft2.hashCode()); FieldType ft3 = new FieldType(); ft3.setIndexOptions(IndexOptions.DOCS_AND_FREQS); assertFalse(ft3.equals(ft)); FieldType ft4 = new FieldType(); ft4.setDocValueType(DocValuesType.BINARY); assertFalse(ft4.equals(ft)); FieldType ft5 = new FieldType(); ft5.setIndexed(true); assertFalse(ft5.equals(ft)); FieldType ft6 = new FieldType(); ft6.setStored(true); assertFalse(ft6.equals(ft)); FieldType ft7 = new FieldType(); ft7.setOmitNorms(true); assertFalse(ft7.equals(ft)); FieldType ft8 = new FieldType(); ft8.setNumericType(NumericType.DOUBLE); assertFalse(ft8.equals(ft)); FieldType ft9 = new FieldType(); ft9.setNumericPrecisionStep(3); assertFalse(ft9.equals(ft)); FieldType ft10 = new FieldType(); ft10.setStoreTermVectors(true); assertFalse(ft10.equals(ft)); }
@Override public IndexableField createField(SchemaField field, Object value, float boost) { boolean indexed = field.indexed(); boolean stored = field.stored(); boolean docValues = field.hasDocValues(); if (!indexed && !stored && !docValues) { if (log.isTraceEnabled()) log.trace("Ignoring unindexed/unstored field: " + field); return null; } FieldType ft = new FieldType(); ft.setStored(stored); ft.setTokenized(true); ft.setIndexed(indexed); ft.setOmitNorms(field.omitNorms()); ft.setIndexOptions(getIndexOptions(field, value.toString())); switch (type) { case INTEGER: ft.setNumericType(NumericType.INT); break; case FLOAT: ft.setNumericType(NumericType.FLOAT); break; case LONG: ft.setNumericType(NumericType.LONG); break; case DOUBLE: ft.setNumericType(NumericType.DOUBLE); break; case DATE: ft.setNumericType(NumericType.LONG); break; default: throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type); } ft.setNumericPrecisionStep(precisionStep); final org.apache.lucene.document.Field f; switch (type) { case INTEGER: int i = (value instanceof Number) ? ((Number)value).intValue() : Integer.parseInt(value.toString()); f = new org.apache.lucene.document.IntField(field.getName(), i, ft); break; case FLOAT: float fl = (value instanceof Number) ? ((Number)value).floatValue() : Float.parseFloat(value.toString()); f = new org.apache.lucene.document.FloatField(field.getName(), fl, ft); break; case LONG: long l = (value instanceof Number) ? ((Number)value).longValue() : Long.parseLong(value.toString()); f = new org.apache.lucene.document.LongField(field.getName(), l, ft); break; case DOUBLE: double d = (value instanceof Number) ? ((Number)value).doubleValue() : Double.parseDouble(value.toString()); f = new org.apache.lucene.document.DoubleField(field.getName(), d, ft); break; case DATE: Date date = (value instanceof Date) ? ((Date)value) : dateField.parseMath(null, value.toString()); f = new org.apache.lucene.document.LongField(field.getName(), date.getTime(), ft); break; default: throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type); } f.setBoost(boost); return f; }