Search in sources :

Example 1 with MolgenisQueryException

use of org.molgenis.data.MolgenisQueryException in project molgenis by molgenis.

the class AggregateQueryRsqlVisitor method getAttribute.

private Attribute getAttribute(ComparisonNode node) {
    List<String> args = node.getArguments();
    if (args.size() != 1) {
        throw new MolgenisQueryException(String.format("RSQL query value must have exactly one value instead of [%s]", StringUtils.join(args, ',')));
    }
    String attrName = args.iterator().next();
    String[] attrTokens = attrName.split("\\.");
    Attribute attr = entityType.getAttribute(attrTokens[0]);
    if (attr == null) {
        throw new UnknownAttributeException(entityType, attrName);
    }
    EntityType entityTypeAtDepth;
    for (int i = 1; i < attrTokens.length; ++i) {
        entityTypeAtDepth = attr.getRefEntity();
        attr = entityTypeAtDepth.getAttribute(attrTokens[i]);
        if (attr == null) {
            throw new UnknownAttributeException(entityTypeAtDepth, attrName);
        }
    }
    return attr;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MolgenisQueryException(org.molgenis.data.MolgenisQueryException) Attribute(org.molgenis.data.meta.model.Attribute) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 2 with MolgenisQueryException

use of org.molgenis.data.MolgenisQueryException in project molgenis by molgenis.

the class MolgenisRSQLVisitor method visit.

@Override
public Query<Entity> visit(ComparisonNode node) {
    String attrName = node.getSelector();
    String symbol = node.getOperator().getSymbol();
    List<String> values = node.getArguments();
    switch(symbol) {
        case "=notlike=":
            String notLikeValue = values.get(0);
            q.not().like(attrName, notLikeValue);
            break;
        case "=q=":
            String searchValue = values.get(0);
            if (attrName.equals("*")) {
                q.search(searchValue);
            } else {
                q.search(attrName, searchValue);
            }
            break;
        case "==":
            Object eqValue = rsqlValueParser.parse(values.get(0), getAttribute(node));
            q.eq(attrName, eqValue);
            break;
        case "=in=":
            Attribute inAttr = getAttribute(node);
            q.in(attrName, values.stream().map(value -> rsqlValueParser.parse(value, inAttr)).collect(toList()));
            break;
        case "=lt=":
        case "<":
            Attribute ltAttr = getAttribute(node);
            validateNumericOrDate(ltAttr);
            Object ltValue = rsqlValueParser.parse(values.get(0), ltAttr);
            q.lt(attrName, ltValue);
            break;
        case "=le=":
        case "<=":
            Attribute leAttr = getAttribute(node);
            validateNumericOrDate(leAttr);
            Object leValue = rsqlValueParser.parse(values.get(0), leAttr);
            q.le(attrName, leValue);
            break;
        case "=gt=":
        case ">":
            Attribute gtAttr = getAttribute(node);
            validateNumericOrDate(gtAttr);
            Object gtValue = rsqlValueParser.parse(values.get(0), gtAttr);
            q.gt(attrName, gtValue);
            break;
        case "=ge=":
        case ">=":
            Attribute geAttr = getAttribute(node);
            validateNumericOrDate(geAttr);
            Object geValue = rsqlValueParser.parse(values.get(0), geAttr);
            q.ge(attrName, geValue);
            break;
        case "=rng=":
            Attribute rngAttr = getAttribute(node);
            validateNumericOrDate(rngAttr);
            Object fromValue = values.get(0) != null ? rsqlValueParser.parse(values.get(0), rngAttr) : null;
            Object toValue = values.get(1) != null ? rsqlValueParser.parse(values.get(1), rngAttr) : null;
            q.rng(attrName, fromValue, toValue);
            break;
        case "=like=":
            String likeValue = values.get(0);
            q.like(attrName, likeValue);
            break;
        case "!=":
            Object notEqValue = rsqlValueParser.parse(values.get(0), getAttribute(node));
            q.not().eq(attrName, notEqValue);
            break;
        case "=should=":
            throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
        case "=dismax=":
            throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
        case "=fuzzy=":
            throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
        default:
            throw new MolgenisQueryException("Unknown RSQL query operator [" + symbol + "]");
    }
    return q;
}
Also used : MolgenisQueryException(org.molgenis.data.MolgenisQueryException) Attribute(org.molgenis.data.meta.model.Attribute)

Example 3 with MolgenisQueryException

use of org.molgenis.data.MolgenisQueryException in project molgenis by molgenis.

the class ClientFacade method search.

private SearchHits search(QueryBuilder query, int from, int size, Sort sort, List<Index> indexes) {
    if (size > 10000) {
        throw new MolgenisQueryException(String.format("Batch size of %s exceeds the maximum batch size of %s for search queries", size, MAX_BATCH_SIZE));
    }
    if (LOG.isTraceEnabled()) {
        if (sort != null) {
            LOG.trace("Searching docs [{}-{}] in index(es) '{}' with query '{}' sorted by '{}' ...", from, from + size, toString(indexes), query, sort);
        } else {
            LOG.trace("Searching docs [{}-{}] in index(es) '{}' with query '{}' ...", from, from + size, toString(indexes), query);
        }
    }
    SearchRequestBuilder searchRequest = createSearchRequest(query, from, size, sort, null, indexes);
    SearchResponse searchResponse;
    try {
        searchResponse = searchRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.error("", e);
        throw new UnknownIndexException(toIndexNames(indexes));
    } catch (ElasticsearchException e) {
        LOG.error("", e);
        throw new IndexException(format("Error searching docs in index(es) '%s' with query '%s'.", toString(indexes), query));
    }
    if (searchResponse.getFailedShards() > 0) {
        LOG.error(stream(searchResponse.getShardFailures()).map(ShardSearchFailure::toString).collect(joining("\n")));
        throw new IndexException(format("Error searching docs in index(es) '%s' with query '%s'.", toString(indexes), query));
    }
    if (searchResponse.isTimedOut()) {
        throw new IndexException(format("Timeout searching counting docs in index(es) '%s'  with query '%s'.", toString(indexes), query));
    }
    if (LOG.isDebugEnabled()) {
        if (sort != null) {
            LOG.debug("Searched {} docs in index(es) '{}' with query '{}' sorted by '{}' in {}ms.", searchResponse.getHits().getTotalHits(), toString(indexes), query, sort, searchResponse.getTookInMillis());
        } else {
            LOG.debug("Searched {} docs in index(es) '{}' with query '{}' in {}ms.", searchResponse.getHits().getTotalHits(), toString(indexes), query, searchResponse.getTookInMillis());
        }
    }
    return createSearchResponse(searchResponse);
}
Also used : MolgenisQueryException(org.molgenis.data.MolgenisQueryException) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

MolgenisQueryException (org.molgenis.data.MolgenisQueryException)3 Attribute (org.molgenis.data.meta.model.Attribute)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 UnknownAttributeException (org.molgenis.data.UnknownAttributeException)1 IndexException (org.molgenis.data.index.exception.IndexException)1 UnknownIndexException (org.molgenis.data.index.exception.UnknownIndexException)1 EntityType (org.molgenis.data.meta.model.EntityType)1