Search in sources :

Example 1 with IndexDataType

use of org.apache.stanbol.entityhub.yard.solr.model.IndexDataType in project stanbol by apache.

the class SolrQueryFactory method initValueConstraint.

/**
     * @param indexConstraint
     * @param refConstraint
     */
private void initValueConstraint(IndexConstraint indexConstraint) {
    ValueConstraint valueConstraint = (ValueConstraint) indexConstraint.getConstraint();
    if (valueConstraint.getValues() == null) {
        indexConstraint.setInvalid(String.format("ValueConstraint without a value - that check only any value for " + "the parsed datatypes %s is present - can not be supported by a Solr query!", valueConstraint.getDataTypes()));
    } else {
        // first process the parsed dataTypes to get the supported types
        List<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
        List<String> acceptedDataTypes = new ArrayList<String>();
        if (valueConstraint.getDataTypes() != null) {
            for (String dataType : valueConstraint.getDataTypes()) {
                IndexDataTypeEnum indexDataTypeEnumEntry = IndexDataTypeEnum.forUri(dataType);
                if (indexDataTypeEnumEntry != null) {
                    indexDataTypes.add(indexDataTypeEnumEntry.getIndexType());
                    acceptedDataTypes.add(dataType);
                } else {
                    // TODO: Add possibility to add warnings to indexConstraints
                    log.warn("A Datatype parsed for a ValueConstraint is not " + "supported and will be ignored (dataTypeUri={})", dataType);
                }
            }
        }
        //we support only a single dataType ...
        //  ... therefore remove additional data types from the ValueConstraint
        IndexDataType indexDataType = null;
        if (!indexDataTypes.isEmpty()) {
            indexDataType = indexDataTypes.get(0);
            if (indexDataTypes.size() > 1) {
                log.warn("Only a single DataType is supported for ValueConstraints!");
                while (acceptedDataTypes.size() > 1) {
                    String ignored = acceptedDataTypes.remove(acceptedDataTypes.size() - 1);
                    log.warn("  > ignore parsed dataType {}", ignored);
                }
            }
        }
        //else empty we will initialise based on the first parsed value!
        ConstraintValue constraintValue = new ConstraintValue(valueConstraint.getMode());
        //init the boost
        addBoost(constraintValue, valueConstraint);
        for (Object value : valueConstraint.getValues()) {
            IndexValue indexValue;
            if (indexDataType == null) {
                // get the dataType based on the type of the value
                try {
                    indexValue = indexValueFactory.createIndexValue(value);
                } catch (NoConverterException e) {
                    // if not found use the toString() and string as type
                    log.warn("Unable to create IndexValue for value {} (type: {}). Create IndexValue manually by using the first parsed IndexDataType {}", new Object[] { value, value.getClass(), IndexDataTypeEnum.STR.getIndexType() });
                    indexValue = new IndexValue(value.toString(), IndexDataTypeEnum.STR.getIndexType());
                }
                //initialise the IndexDataType for this query based on the first parsed value
                indexDataType = indexValue.getType();
            } else {
                indexValue = new IndexValue(value.toString(), indexDataType);
            }
            //add the constraint
            constraintValue.getValues().add(indexValue);
        }
        //indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, indexDataType);
        IndexField indexField;
        if (IndexDataTypeEnum.TXT.getIndexType().equals(indexDataType)) {
            //NOTE: in case of TEXT we need also to add the language to create a valid
            //query!
            // * We take the language of the first parsed element
            indexField = new IndexField(indexConstraint.getPath(), indexDataType, constraintValue.getValues().iterator().next().getLanguage());
        } else {
            indexField = new IndexField(indexConstraint.getPath(), indexDataType);
        }
        //set FIELD, DATATYPE and LANGUAGE constraint by using the indexField
        indexConstraint.setIndexFieldConstraints(indexField);
        //set the VALUE
        //TODO: We need to somehow pass the MODE so that the encoder knows how
        //      to encode the values
        indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, constraintValue);
        //update this constraint!
        if (valueConstraint instanceof ReferenceConstraint) {
            indexConstraint.setFieldQueryConstraint(valueConstraint);
        } else {
            indexConstraint.setFieldQueryConstraint(new ValueConstraint(valueConstraint.getValues(), Arrays.asList(indexDataType.getId())));
        }
    }
}
Also used : IndexDataType(org.apache.stanbol.entityhub.yard.solr.model.IndexDataType) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) ArrayList(java.util.ArrayList) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException) IndexDataTypeEnum(org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum) IndexField(org.apache.stanbol.entityhub.yard.solr.model.IndexField)

Example 2 with IndexDataType

use of org.apache.stanbol.entityhub.yard.solr.model.IndexDataType 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

IndexDataType (org.apache.stanbol.entityhub.yard.solr.model.IndexDataType)2 IndexField (org.apache.stanbol.entityhub.yard.solr.model.IndexField)2 IndexValue (org.apache.stanbol.entityhub.yard.solr.model.IndexValue)2 ArrayList (java.util.ArrayList)1 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)1 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)1 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)1 IndexDataTypeEnum (org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum)1 NoConverterException (org.apache.stanbol.entityhub.yard.solr.model.NoConverterException)1