Search in sources :

Example 41 with Attribute

use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseRangeOpen.

private QueryBuilder createQueryClauseRangeOpen(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    Attribute attr = attributePath.get(attributePath.size() - 1);
    validateNumericalQueryField(attr);
    String fieldName = getQueryFieldName(attributePath);
    Object queryValue = getQueryValue(attr, queryRule.getValue());
    if (queryValue == null) {
        throw new MolgenisQueryException("Query value cannot be null");
    }
    RangeQueryBuilder filterBuilder = QueryBuilders.rangeQuery(fieldName);
    QueryRule.Operator operator = queryRule.getOperator();
    switch(operator) {
        case GREATER:
            filterBuilder = filterBuilder.gt(queryValue);
            break;
        case GREATER_EQUAL:
            filterBuilder = filterBuilder.gte(queryValue);
            break;
        case LESS:
            filterBuilder = filterBuilder.lt(queryValue);
            break;
        case LESS_EQUAL:
            filterBuilder = filterBuilder.lte(queryValue);
            break;
        case AND:
        case DIS_MAX:
        case EQUALS:
        case FUZZY_MATCH:
        case FUZZY_MATCH_NGRAM:
        case IN:
        case LIKE:
        case NESTED:
        case NOT:
        case OR:
        case RANGE:
        case SEARCH:
        case SHOULD:
            throw new MolgenisQueryException(format("Illegal query rule operator [%s]", operator.toString()));
        default:
            throw new UnexpectedEnumException(operator);
    }
    return QueryBuilders.constantScoreQuery(nestedQueryBuilder(attributePath, filterBuilder));
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute)

Example 42 with Attribute

use of org.molgenis.data.meta.model.Attribute 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);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 43 with Attribute

use of org.molgenis.data.meta.model.Attribute 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);
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) org.molgenis.data(org.molgenis.data) Iterator(java.util.Iterator) Stream.of(java.util.stream.Stream.of) LIKE(org.molgenis.data.QueryRule.Operator.LIKE) ScoreMode(org.apache.lucene.search.join.ScoreMode) Instant(java.time.Instant) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FIELD_NOT_ANALYZED(org.molgenis.data.elasticsearch.FieldConstants.FIELD_NOT_ANALYZED) Component(org.springframework.stereotype.Component) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) org.elasticsearch.index.query(org.elasticsearch.index.query) LocalDate(java.time.LocalDate) Objects.requireNonNull(java.util.Objects.requireNonNull) StreamSupport(java.util.stream.StreamSupport) Stream.concat(java.util.stream.Stream.concat) DEFAULT_ANALYZER(org.molgenis.data.elasticsearch.FieldConstants.DEFAULT_ANALYZER) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 44 with Attribute

use of org.molgenis.data.meta.model.Attribute 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);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 45 with Attribute

use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.

the class SortaCsvRepository method getEntityType.

public EntityType getEntityType() {
    if (entityType == null) {
        AttributeFactory attrMetaFactory = getApplicationContext().getBean(// FIXME do not use application context
        AttributeFactory.class);
        entityType = EntityType.newInstance(csvRepository.getEntityType(), DEEP_COPY_ATTRS, attrMetaFactory);
        entityType.setId(entityTypeId);
        entityType.setLabel(entityLabel);
        // FIXME do not hardcode backend name
        entityType.setBackend("PostgreSQL");
        entityType.addAttribute(attrMetaFactory.create().setName(ALLOWED_IDENTIFIER).setNillable(false), ROLE_ID);
        Attribute nameAttribute = entityType.getAttribute(SortaServiceImpl.DEFAULT_MATCHING_NAME_FIELD);
        if (nameAttribute != null) {
            nameAttribute.setLabelAttribute(true);
        }
    }
    return entityType;
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory)

Aggregations

Attribute (org.molgenis.data.meta.model.Attribute)600 Test (org.testng.annotations.Test)351 EntityType (org.molgenis.data.meta.model.EntityType)321 Entity (org.molgenis.data.Entity)109 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)54 DynamicEntity (org.molgenis.data.support.DynamicEntity)53 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)47 Stream (java.util.stream.Stream)39 AttributeType (org.molgenis.data.meta.AttributeType)34 QueryImpl (org.molgenis.data.support.QueryImpl)29 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)29 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)28 BeforeMethod (org.testng.annotations.BeforeMethod)20 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)18 WithMockUser (org.springframework.security.test.context.support.WithMockUser)18 Collectors.toList (java.util.stream.Collectors.toList)17 Relation (org.molgenis.data.semantic.Relation)17 Objects.requireNonNull (java.util.Objects.requireNonNull)16 MolgenisDataException (org.molgenis.data.MolgenisDataException)16 Package (org.molgenis.data.meta.model.Package)16