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);
}
}
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;
}
Aggregations