Search in sources :

Example 36 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class EntityTypeUtilsTest method isReferenceTypeAttrTypeProvider.

@DataProvider(name = "isReferenceTypeAttrTypeProvider")
public static Iterator<Object[]> isReferenceTypeAttrTypeProvider() {
    List<Object[]> dataList = Lists.newArrayList();
    for (AttributeType attrType : AttributeType.values()) {
        boolean isRefAttr = attrType == CATEGORICAL || attrType == CATEGORICAL_MREF || attrType == FILE || attrType == MREF || attrType == ONE_TO_MANY || attrType == XREF;
        dataList.add(new Object[] { attrType, isRefAttr });
    }
    return dataList.iterator();
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) DataProvider(org.testng.annotations.DataProvider)

Example 37 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class EntityTypeUtilsTest method isReferenceTypeAttrProvider.

@DataProvider(name = "isReferenceTypeAttrProvider")
public static Iterator<Object[]> isReferenceTypeAttrProvider() {
    List<Object[]> dataList = Lists.newArrayList();
    for (AttributeType attrType : AttributeType.values()) {
        Attribute attr = mock(Attribute.class);
        when(attr.getDataType()).thenReturn(attrType);
        when(attr.toString()).thenReturn("attr_" + attrType.toString());
        boolean isRefAttr = attrType == CATEGORICAL || attrType == CATEGORICAL_MREF || attrType == FILE || attrType == MREF || attrType == ONE_TO_MANY || attrType == XREF;
        dataList.add(new Object[] { attr, isRefAttr });
    }
    return dataList.iterator();
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType) DataProvider(org.testng.annotations.DataProvider)

Example 38 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseFuzzyMatchNgram.

private QueryBuilder createQueryClauseFuzzyMatchNgram(QueryRule queryRule, EntityType entityType) {
    String queryField = queryRule.getField();
    Object queryValue = queryRule.getValue();
    QueryBuilder queryBuilder;
    if (queryValue == null)
        throw new MolgenisQueryException("Query value cannot be null");
    if (queryField == null) {
        queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
    } else {
        Attribute attr = entityType.getAttribute(queryField);
        if (attr == null) {
            throw new UnknownAttributeException(entityType, queryField);
        }
        // construct query part
        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:
                queryField = getQueryFieldName(attr) + ".ngram";
                queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")");
                break;
            case MREF:
            case XREF:
                queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + ".ngram";
                queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
                break;
            default:
                throw new UnexpectedEnumException(dataType);
        }
    }
    return queryBuilder;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 39 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseEqualsValue.

private QueryBuilder createQueryClauseEqualsValue(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 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;
            }
            return nestedQueryBuilder(attributePath, QueryBuilders.termQuery(fieldName, queryValue));
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case XREF:
        case MREF:
        case FILE:
        case ONE_TO_MANY:
            if (attributePath.size() > 1) {
                throw new MolgenisQueryException("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;
            }
            return QueryBuilders.nestedQuery(fieldName, QueryBuilders.termQuery(indexFieldName, queryValue), ScoreMode.Avg);
        case COMPOUND:
            throw new MolgenisQueryException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 40 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseFuzzyMatch.

private QueryBuilder createQueryClauseFuzzyMatch(QueryRule queryRule, EntityType entityType) {
    String queryField = queryRule.getField();
    Object queryValue = queryRule.getValue();
    QueryBuilder queryBuilder;
    if (queryValue == null)
        throw new MolgenisQueryException("Query value cannot be null");
    if (queryField == null) {
        queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
    } else {
        Attribute attr = entityType.getAttribute(queryField);
        if (attr == null) {
            throw new UnknownAttributeException(entityType, queryField);
        }
        // construct query part
        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:
                queryBuilder = QueryBuilders.queryStringQuery(getQueryFieldName(attr) + ":(" + queryValue + ")");
                break;
            case MREF:
            case XREF:
            case CATEGORICAL:
            case CATEGORICAL_MREF:
            case ONE_TO_MANY:
            case FILE:
                queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute());
                queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
                break;
            case BOOL:
            case COMPOUND:
                throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.FUZZY_MATCH + "]");
            default:
                throw new UnexpectedEnumException(dataType);
        }
    }
    return queryBuilder;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Aggregations

AttributeType (org.molgenis.data.meta.AttributeType)46 Attribute (org.molgenis.data.meta.model.Attribute)24 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)24 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)10 Entity (org.molgenis.data.Entity)10 EntityType (org.molgenis.data.meta.model.EntityType)7 DataProvider (org.testng.annotations.DataProvider)7 ArrayList (java.util.ArrayList)5 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 String.format (java.lang.String.format)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.testng.annotations.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 UTC (java.time.ZoneOffset.UTC)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1