Search in sources :

Example 1 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class BaseResourceSearchRequest method prepareQuery.

@Override
protected final Expression prepareQuery(RepositoryContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addSecurityFilter(context, queryBuilder);
    addFilter(queryBuilder, OptionKey.BUNDLE_ID, String.class, ResourceDocument.Expressions::bundleIds);
    if (containsKey(OptionKey.BUNDLE_ANCESTOR_ID)) {
        final Collection<String> ancestorIds = getCollection(OptionKey.BUNDLE_ANCESTOR_ID, String.class);
        queryBuilder.filter(Expressions.builder().should(ResourceDocument.Expressions.bundleIds(ancestorIds)).should(ResourceDocument.Expressions.bundleAncestorIds(ancestorIds)).build());
    }
    addFilter(queryBuilder, OptionKey.OID, String.class, ResourceDocument.Expressions::oids);
    addFilter(queryBuilder, OptionKey.STATUS, String.class, ResourceDocument.Expressions::statuses);
    addIdFilter(queryBuilder, ResourceDocument.Expressions::ids);
    addTitleFilter(queryBuilder);
    addTitleExactFilter(queryBuilder);
    addUrlFilter(queryBuilder);
    prepareAdditionalFilters(context, queryBuilder);
    return queryBuilder.build();
}
Also used : Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 2 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SnomedEclEvaluationRequest method eval.

protected Promise<Expression> eval(BranchContext context, final DialectIdFilter dialectIdFilter) {
    final Multimap<String, String> languageRefSetsByAcceptability = HashMultimap.create();
    final ExpressionBuilder dialectQuery = Expressions.builder();
    for (Dialect dialect : dialectIdFilter.getDialects()) {
        final ExpressionConstraint languageRefSetId = dialect.getLanguageRefSetId();
        final Set<String> evaluatedIds = EclExpression.of(languageRefSetId, expressionForm).resolve(context).getSync();
        final Set<String> acceptabilitiesToMatch = getAcceptabilityFields(dialect.getAcceptability());
        // empty set means that acceptability values are not valid and it should not match any descriptions/concepts
        if (acceptabilitiesToMatch.isEmpty()) {
            return Promise.immediate(Expressions.matchNone());
        }
        for (String acceptability : acceptabilitiesToMatch) {
            languageRefSetsByAcceptability.putAll(acceptability, evaluatedIds);
        }
    }
    languageRefSetsByAcceptability.asMap().forEach((key, values) -> {
        Operator op = Operator.fromString(dialectIdFilter.getOp());
        switch(op) {
            case EQUALS:
                dialectQuery.should(Expressions.matchAny(key, values));
                break;
            case NOT_EQUALS:
                dialectQuery.mustNot(Expressions.matchAny(key, values));
                break;
            default:
                throw new NotImplementedException("Unsupported dialectIdFilter operator '%s'", dialectIdFilter.getOp());
        }
    });
    return Promise.immediate(dialectQuery.build());
}
Also used : NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 3 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SnomedEclRefinementEvaluator method evalAxiomStatements.

static Set<Property> evalAxiomStatements(final BranchContext context, final boolean groupedRelationshipsOnly, final Collection<String> sourceIds, final Collection<String> typeIds, final Collection<String> destinationIds) {
    try {
        // search existing axioms (no values!) defined for the given set of conceptIds
        ExpressionBuilder axiomFilter = Expressions.builder().filter(hasDestinationId());
        if (typeIds != null) {
            axiomFilter.filter(typeIds(typeIds));
        }
        if (destinationIds != null) {
            axiomFilter.filter(destinationIds(destinationIds));
        }
        if (groupedRelationshipsOnly) {
            axiomFilter.filter(relationshipGroup(1, Integer.MAX_VALUE));
        }
        ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active()).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, axiomFilter.build()));
        if (sourceIds != null) {
            activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(sourceIds));
        }
        final Query<SnomedRefSetMemberIndexEntry> activeAxiomStatementsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
        return context.service(RevisionSearcher.class).search(activeAxiomStatementsQuery).stream().filter(owlMember -> !CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())).flatMap(owlMember -> {
            return owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> {
                return (typeIds == null || typeIds.contains(classAxiom.getTypeId())) && (destinationIds == null || destinationIds.contains(classAxiom.getDestinationId())) && (!groupedRelationshipsOnly || classAxiom.getRelationshipGroup() >= 1);
            }).map(classAxiom -> {
                return new Property(owlMember.getReferencedComponentId(), classAxiom.getTypeId(), classAxiom.getDestinationId(), classAxiom.getRelationshipGroup());
            });
        }).collect(Collectors.toSet());
    } catch (IOException e) {
        throw new SnowowlRuntimeException(e);
    }
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) PolymorphicDispatcher(org.eclipse.xtext.util.PolymorphicDispatcher) Query(com.b2international.index.query.Query) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Promise(com.b2international.snowowl.core.events.util.Promise) ID(com.b2international.index.revision.Revision.Fields.ID) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) com.b2international.snomed.ecl.ecl(com.b2international.snomed.ecl.ecl) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) ResourceURI(com.b2international.snowowl.core.ResourceURI) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Collection(java.util.Collection) SnomedRelationshipSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRelationshipSearchRequestBuilder) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Trees(com.b2international.snowowl.snomed.core.tree.Trees) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) Entry(java.util.Map.Entry) Expression(com.b2international.index.query.Expression) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Fields(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Fields) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) IOException(java.io.IOException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 4 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SnomedEclRefinementEvaluator method evalAxiomsWithValue.

private Promise<Collection<Property>> evalAxiomsWithValue(BranchContext context, Set<String> focusConceptIds, Collection<String> typeIds, RelationshipValue value, SearchResourceRequest.Operator operator) {
    // search existing axioms defined for the given set of conceptIds
    ExpressionBuilder axiomFilter = Expressions.builder();
    if (typeIds != null) {
        axiomFilter.filter(typeIds(typeIds));
    }
    switch(operator) {
        case EQUALS:
            axiomFilter.filter(values(List.of(value)));
            break;
        case GREATER_THAN:
            axiomFilter.filter(valueGreaterThan(value, false));
            break;
        case GREATER_THAN_EQUALS:
            axiomFilter.filter(valueGreaterThan(value, true));
            break;
        case LESS_THAN:
            axiomFilter.filter(valueLessThan(value, false));
            break;
        case LESS_THAN_EQUALS:
            axiomFilter.filter(valueLessThan(value, true));
            break;
        case NOT_EQUALS:
            axiomFilter.mustNot(values(List.of(value)));
            break;
        default:
            throw new IllegalStateException("Unexpected operator '" + operator + "'.");
    }
    ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active()).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, axiomFilter.build()));
    if (focusConceptIds != null) {
        activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(focusConceptIds));
    }
    final Query<SnomedRefSetMemberIndexEntry> activeAxiomStatementsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(context.service(RepositoryConfiguration.class).getIndexConfiguration().getResultWindow()).build();
    final Set<Property> axiomProperties = newHashSet();
    context.service(RevisionSearcher.class).stream(activeAxiomStatementsQuery).forEach(chunk -> {
        chunk.stream().filter(owlMember -> !CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())).forEachOrdered(owlMember -> {
            owlMember.getClassAxiomRelationships().stream().filter(r -> typeIds == null || typeIds.contains(r.getTypeId())).filter(r -> r.getValueAsObject().matches(operator, value)).map(r -> new Property(owlMember.getReferencedComponentId(), r.getTypeId(), r.getValueAsObject().toObject(), r.getRelationshipGroup())).forEachOrdered(axiomProperties::add);
        });
    });
    return Promise.immediate(axiomProperties);
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) PolymorphicDispatcher(org.eclipse.xtext.util.PolymorphicDispatcher) Query(com.b2international.index.query.Query) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Promise(com.b2international.snowowl.core.events.util.Promise) ID(com.b2international.index.revision.Revision.Fields.ID) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) com.b2international.snomed.ecl.ecl(com.b2international.snomed.ecl.ecl) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) ResourceURI(com.b2international.snowowl.core.ResourceURI) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Collection(java.util.Collection) SnomedRelationshipSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRelationshipSearchRequestBuilder) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Trees(com.b2international.snowowl.snomed.core.tree.Trees) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) Entry(java.util.Map.Entry) Expression(com.b2international.index.query.Expression) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Fields(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Fields) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 5 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SearchMergeRequest method doExecute.

@Override
public Merges doExecute(RepositoryContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    // add mergerequest type filter
    queryBuilder.filter(RemoteJobEntry.Expressions.matchRequestType(ImmutableSet.of(BranchRebaseRequest.class.getSimpleName(), BranchMergeRequest.class.getSimpleName())));
    if (containsKey(OptionKey.SOURCE)) {
        queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(SOURCE_FIELD, getCollection(OptionKey.SOURCE, String.class)));
    }
    if (containsKey(OptionKey.TARGET)) {
        queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(TARGET_FIELD, getCollection(OptionKey.TARGET, String.class)));
    }
    if (containsKey(OptionKey.STATUS)) {
        queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(STATUS_FIELD, getCollection(OptionKey.STATUS, String.class)));
    }
    final RemoteJobs jobs = context.service(RemoteJobTracker.class).search(queryBuilder.build(), Integer.MAX_VALUE);
    final ObjectMapper mapper = context.service(ObjectMapper.class);
    final List<Merge> items = jobs.stream().map(job -> createMergefromJobEntry(job, mapper)).collect(Collectors.toList());
    return new Merges(items, jobs.getSearchAfter(), jobs.getLimit(), jobs.getTotal());
}
Also used : RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ImmutableSet(com.google.common.collect.ImmutableSet) Status(com.b2international.snowowl.core.merge.Merge.Status) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Merge(com.b2international.snowowl.core.merge.Merge) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) Collectors(java.util.stream.Collectors) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) List(java.util.List) Merges(com.b2international.snowowl.core.merge.Merges) Expressions(com.b2international.index.query.Expressions) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Map(java.util.Map) ApiError(com.b2international.commons.exceptions.ApiError) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Permission(com.b2international.snowowl.core.identity.Permission) Builder(com.b2international.snowowl.core.merge.Merge.Builder) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) RemoteJobTracker(com.b2international.snowowl.core.jobs.RemoteJobTracker) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) Merges(com.b2international.snowowl.core.merge.Merges) RemoteJobTracker(com.b2international.snowowl.core.jobs.RemoteJobTracker) Merge(com.b2international.snowowl.core.merge.Merge) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)47 Expressions (com.b2international.index.query.Expressions)17 Expression (com.b2international.index.query.Expression)6 List (java.util.List)6 Set (java.util.Set)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)5 Options (com.b2international.commons.options.Options)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)5 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)5 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)4 IOException (java.io.IOException)4 CompareUtils (com.b2international.commons.CompareUtils)3 Query (com.b2international.index.query.Query)3 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)3 BranchContext (com.b2international.snowowl.core.domain.BranchContext)3 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)3