use of org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint in project stanbol by apache.
the class SolrQueryFactory method initRangeConstraint.
/**
* @param indexConstraint
* @param rangeConstraint
*/
private void initRangeConstraint(IndexConstraint indexConstraint) {
RangeConstraint rangeConstraint = (RangeConstraint) indexConstraint.getConstraint();
// we need to find the Index DataType for the range query
IndexDataType dataType = null;
ConstraintValue lowerBound = new ConstraintValue();
ConstraintValue upperBound = new ConstraintValue();
//init the boosts
addBoost(lowerBound, rangeConstraint);
addBoost(upperBound, rangeConstraint);
//init IndexValues and check for the dataType
if (rangeConstraint.getLowerBound() != null) {
IndexValue value = indexValueFactory.createIndexValue(rangeConstraint.getLowerBound());
lowerBound.getValues().add(value);
dataType = value.getType();
}
if (rangeConstraint.getUpperBound() != null) {
IndexValue value = indexValueFactory.createIndexValue(rangeConstraint.getUpperBound());
upperBound.getValues().add(value);
IndexDataType upperDataType = value.getType();
if (dataType == null) {
dataType = upperDataType;
} else {
if (!dataType.equals(upperDataType)) {
indexConstraint.setInvalid(String.format("A Range Query MUST use the same data type for the upper " + "and lover Bound! (lower:[value=%s|datatype=%s] | " + "upper:[value=%s|datatype=%s])", rangeConstraint.getLowerBound(), dataType, rangeConstraint.getUpperBound(), upperDataType));
}
}
}
if (dataType == null) {
indexConstraint.setInvalid("A Range Constraint MUST define at least a lower or an upper bound!");
} else {
//set the DATATYPE and FIED using an IndexField
indexConstraint.setIndexFieldConstraints(new IndexField(indexConstraint.getPath(), dataType));
}
//set the value range
if (rangeConstraint.isInclusive()) {
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.LE, upperBound);
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.GE, lowerBound);
} else {
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.LT, upperBound);
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.GT, lowerBound);
}
}
Aggregations