Search in sources :

Example 6 with RangeConstraint

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);
    }
}
Also used : RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) IndexDataType(org.apache.stanbol.entityhub.yard.solr.model.IndexDataType) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) IndexField(org.apache.stanbol.entityhub.yard.solr.model.IndexField)

Aggregations

RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)6 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)4 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)4 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)3 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)3 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)2 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 DataTypeEnum (org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum)1 IndexDataType (org.apache.stanbol.entityhub.yard.solr.model.IndexDataType)1 IndexField (org.apache.stanbol.entityhub.yard.solr.model.IndexField)1 IndexValue (org.apache.stanbol.entityhub.yard.solr.model.IndexValue)1 JSONArray (org.codehaus.jettison.json.JSONArray)1