Search in sources :

Example 6 with IndexField

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

Example 7 with IndexField

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

the class SolrYard method createRepresentation.

/**
     * Creates the Representation for the parsed SolrDocument!
     * 
     * @param doc
     *            The Solr Document to convert
     * @param fields
     *            if NOT NULL only this fields are added to the Representation
     * @return the Representation
     */
protected final Representation createRepresentation(SolrDocument doc, Set<String> fields) {
    if (fieldMapper == null) {
        throw new IllegalArgumentException("The parsed FieldMapper MUST NOT be NULL!");
    }
    if (doc == null) {
        throw new IllegalArgumentException("The parsed SolrDocument MUST NOT be NULL!");
    }
    Object id = doc.getFirstValue(fieldMapper.getDocumentIdField());
    if (id == null) {
        throw new IllegalStateException(String.format("The parsed Solr Document does not contain a value for the %s Field!", fieldMapper.getDocumentIdField()));
    }
    Representation rep = getValueFactory().createRepresentation(id.toString());
    for (String fieldName : doc.getFieldNames()) {
        IndexField indexField = fieldMapper.getField(fieldName);
        if (indexField != null && indexField.getPath().size() == 1) {
            String lang = indexField.getLanguages().isEmpty() ? null : indexField.getLanguages().iterator().next();
            if (fields == null || fields.contains(indexField.getPath().get(0))) {
                for (Object value : doc.getFieldValues(fieldName)) {
                    if (value != null) {
                        IndexDataTypeEnum dataTypeEnumEntry = IndexDataTypeEnum.forIndexType(indexField.getDataType());
                        if (dataTypeEnumEntry != null) {
                            Object javaValue = indexValueFactory.createValue(dataTypeEnumEntry.getJavaType(), indexField.getDataType(), value, lang);
                            if (javaValue != null) {
                                rep.add(indexField.getPath().iterator().next(), javaValue);
                            } else {
                                log.warn(String.format("java value=null for index value %s", value));
                            }
                        } else {
                            log.warn(String.format("No DataType Configuration found for Index Data Type %s!", indexField.getDataType()));
                        }
                    }
                // else index value == null -> ignore
                }
            // end for all values
            }
        } else {
            if (indexField != null) {
                log.warn(String.format("Unable to prozess Index Field %s (for IndexDocument Field: %s)", indexField, fieldName));
            }
        }
    }
    // end for all fields
    return rep;
}
Also used : IndexDataTypeEnum(org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IndexField(org.apache.stanbol.entityhub.yard.solr.model.IndexField)

Aggregations

IndexField (org.apache.stanbol.entityhub.yard.solr.model.IndexField)7 IndexDataTypeEnum (org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum)4 ArrayList (java.util.ArrayList)3 IndexValue (org.apache.stanbol.entityhub.yard.solr.model.IndexValue)3 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)2 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)2 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)2 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)2 IndexDataType (org.apache.stanbol.entityhub.yard.solr.model.IndexDataType)2 HashMap (java.util.HashMap)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)1 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)1 NoConverterException (org.apache.stanbol.entityhub.yard.solr.model.NoConverterException)1