use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseSearchAttribute.
private QueryBuilder createQueryClauseSearchAttribute(QueryRule queryRule, EntityType entityType) {
List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
Attribute attr = attributePath.get(attributePath.size() - 1);
Object queryValue = getQueryValue(attr, queryRule.getValue());
String fieldName = getQueryFieldName(attributePath);
AttributeType dataType = attr.getDataType();
switch(dataType) {
case DATE:
case DATE_TIME:
case DECIMAL:
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case INT:
case LONG:
case SCRIPT:
case STRING:
case TEXT:
return nestedQueryBuilder(attributePath, QueryBuilders.matchQuery(fieldName, queryValue));
case CATEGORICAL:
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
case XREF:
case FILE:
if (attributePath.size() > 1) {
throw new UnsupportedOperationException("Can not filter on references deeper than 1.");
}
return QueryBuilders.nestedQuery(fieldName, QueryBuilders.matchQuery(fieldName + '.' + "_all", queryValue), ScoreMode.Avg);
case BOOL:
throw new MolgenisQueryException("Cannot execute search query on [" + dataType + "] attribute");
case COMPOUND:
throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.SEARCH + "]");
default:
throw new UnexpectedEnumException(dataType);
}
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseIn.
private QueryBuilder createQueryClauseIn(QueryRule queryRule, EntityType entityType) {
List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
Attribute attr = attributePath.get(attributePath.size() - 1);
Object queryRuleValue = queryRule.getValue();
if (queryRuleValue == null) {
throw new MolgenisQueryException("Query value cannot be null");
}
if (!(queryRuleValue instanceof Iterable<?>)) {
throw new MolgenisQueryException("Query value must be a Iterable instead of [" + queryRuleValue.getClass().getSimpleName() + "]");
}
Object[] queryValues = StreamSupport.stream(((Iterable<?>) queryRuleValue).spliterator(), false).map(aQueryRuleValue -> getQueryValue(attr, aQueryRuleValue)).toArray();
QueryBuilder queryBuilder;
String fieldName = getQueryFieldName(attr);
AttributeType dataType = attr.getDataType();
switch(dataType) {
case BOOL:
case DATE:
case DATE_TIME:
case DECIMAL:
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case INT:
case LONG:
case SCRIPT:
case STRING:
case TEXT:
if (useNotAnalyzedField(attr)) {
fieldName = fieldName + '.' + FIELD_NOT_ANALYZED;
}
// note: inFilter expects array, not iterable
queryBuilder = QueryBuilders.termsQuery(fieldName, queryValues);
queryBuilder = nestedQueryBuilder(attributePath, queryBuilder);
break;
case CATEGORICAL:
case CATEGORICAL_MREF:
case MREF:
case XREF:
case FILE:
case ONE_TO_MANY:
if (attributePath.size() > 1) {
throw new UnsupportedOperationException("Can not filter on references deeper than 1.");
}
Attribute refIdAttr = attr.getRefEntity().getIdAttribute();
List<Attribute> refAttributePath = concat(attributePath.stream(), of(refIdAttr)).collect(toList());
String indexFieldName = getQueryFieldName(refAttributePath);
if (useNotAnalyzedField(refIdAttr)) {
indexFieldName = indexFieldName + '.' + FIELD_NOT_ANALYZED;
}
queryBuilder = QueryBuilders.termsQuery(indexFieldName, queryValues);
queryBuilder = QueryBuilders.nestedQuery(fieldName, queryBuilder, ScoreMode.Avg);
break;
case COMPOUND:
throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.IN + "]");
default:
throw new UnexpectedEnumException(dataType);
}
return QueryBuilders.constantScoreQuery(queryBuilder);
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseLike.
private QueryBuilder createQueryClauseLike(QueryRule queryRule, EntityType entityType) {
List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
Attribute attr = attributePath.get(attributePath.size() - 1);
Object queryValue = getQueryValue(attr, queryRule.getValue());
String fieldName = getQueryFieldName(attributePath);
AttributeType attrType = attr.getDataType();
switch(attrType) {
case EMAIL:
case ENUM:
case HYPERLINK:
case STRING:
return nestedQueryBuilder(attributePath, QueryBuilders.matchPhrasePrefixQuery(fieldName, queryValue).maxExpansions(50).slop(10).analyzer(DEFAULT_ANALYZER));
case BOOL:
case COMPOUND:
case DATE:
case DATE_TIME:
case DECIMAL:
case INT:
case LONG:
throw new MolgenisQueryException(format("Illegal data type [%s] for operator [%s]", attrType, LIKE));
case CATEGORICAL:
case CATEGORICAL_MREF:
case FILE:
// due to size would result in large amount of ngrams
case HTML:
case MREF:
case ONE_TO_MANY:
// due to size would result in large amount of ngrams
case SCRIPT:
// due to size would result in large amount of ngrams
case TEXT:
case XREF:
throw new UnsupportedOperationException(format("Unsupported data type [%s] for operator [%s]", attrType, LIKE));
default:
throw new UnexpectedEnumException(attrType);
}
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class AggregationGenerator method getAggregateFieldName.
private String getAggregateFieldName(Attribute attr) {
String fieldName = documentIdGenerator.generateId(attr);
AttributeType dataType = attr.getDataType();
switch(dataType) {
case BOOL:
case INT:
case LONG:
case DECIMAL:
return fieldName;
case DATE:
case DATE_TIME:
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
// use non-analyzed field
return fieldName + '.' + FieldConstants.FIELD_NOT_ANALYZED;
case CATEGORICAL:
case CATEGORICAL_MREF:
case XREF:
case MREF:
case FILE:
case ONE_TO_MANY:
// use id attribute of nested field
return fieldName + '.' + getAggregateFieldName(attr.getRefEntity().getIdAttribute());
case COMPOUND:
throw new UnsupportedOperationException();
default:
throw new UnexpectedEnumException(dataType);
}
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class AlgorithmServiceImpl method convert.
@SuppressWarnings("unchecked")
private Object convert(Object value, Attribute attr) {
Object convertedValue;
AttributeType attrType = attr.getDataType();
switch(attrType) {
case BOOL:
convertedValue = value != null ? toBoolean(value) : null;
break;
case CATEGORICAL:
case XREF:
case FILE:
convertedValue = value != null ? entityManager.getReference(attr.getRefEntity(), convert(value, attr.getRefEntity().getIdAttribute())) : null;
break;
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
Collection<Object> valueIds = (Collection<Object>) value;
convertedValue = valueIds.stream().map(valueId -> entityManager.getReference(attr.getRefEntity(), convert(valueId, attr.getRefEntity().getIdAttribute()))).collect(toList());
break;
case DATE:
convertedValue = convertToDate(value);
break;
case DATE_TIME:
convertedValue = convertToDateTime(value);
break;
case DECIMAL:
convertedValue = convertToDouble(value);
break;
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
convertedValue = value != null ? value.toString() : null;
break;
case INT:
convertedValue = convertToInteger(value);
break;
case LONG:
convertedValue = convertToLong(value);
break;
case COMPOUND:
throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
default:
throw new UnexpectedEnumException(attrType);
}
return convertedValue;
}
Aggregations