Search in sources :

Example 11 with ExpressionBuilder

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

the class ValidationIssueSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(ServiceProvider context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addIdFilter(queryBuilder, ids -> Expressions.matchAny(ValidationIssue.Fields.ID, ids));
    if (containsKey(OptionKey.RESOURCE_URI)) {
        queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.RESOURCE_URI, getCollection(OptionKey.RESOURCE_URI, String.class)));
    }
    Set<String> filterByRuleIds = null;
    if (containsKey(OptionKey.TOOLING_ID)) {
        final Collection<String> toolingIds = getCollection(OptionKey.TOOLING_ID, String.class);
        final Set<String> ruleIds = ValidationRequests.rules().prepareSearch().all().filterByToolings(toolingIds).setFields(ValidationRule.Fields.ID).build().execute(context).stream().map(ValidationRule::getId).collect(Collectors.toSet());
        if (ruleIds.isEmpty()) {
            queryBuilder.filter(Expressions.matchNone());
        } else {
            filterByRuleIds = newHashSet(ruleIds);
        }
    }
    if (containsKey(OptionKey.RULE_ID)) {
        Set<String> ruleFilter = ImmutableSet.copyOf(getCollection(OptionKey.RULE_ID, String.class));
        if (filterByRuleIds != null) {
            SetView<String> diff = Sets.difference(ruleFilter, filterByRuleIds);
            if (!diff.isEmpty()) {
                throw new BadRequestException("Some of the ruleId filter values '%s' belong to a non-specified toolingId.", diff);
            }
            filterByRuleIds = ruleFilter;
        } else {
            filterByRuleIds = newHashSet(ruleFilter);
        }
    }
    if (filterByRuleIds != null) {
        queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.RULE_ID, filterByRuleIds));
    }
    if (containsKey(OptionKey.AFFECTED_COMPONENT_ID)) {
        Collection<String> affectedComponentIds = getCollection(OptionKey.AFFECTED_COMPONENT_ID, String.class);
        queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, affectedComponentIds));
    }
    if (containsKey(OptionKey.AFFECTED_COMPONENT_LABEL)) {
        final String searchTerm = getString(OptionKey.AFFECTED_COMPONENT_LABEL);
        if (containsKey(OptionKey.AFFECTED_COMPONENT_ID)) {
            queryBuilder.must(Expressions.matchTextPhrase(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_TEXT, searchTerm));
        } else {
            queryBuilder.must(Expressions.builder().should(Expressions.dismaxWithScoreCategories(Expressions.matchTextPhrase(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_TEXT, searchTerm), Expressions.matchTextAll(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_PREFIX, searchTerm))).should(Expressions.boost(Expressions.exactMatch(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, searchTerm), 1000f)).build());
        }
    }
    if (containsKey(OptionKey.WHITELISTED)) {
        boolean whitelisted = getBoolean(OptionKey.WHITELISTED);
        queryBuilder.filter(Expressions.match(ValidationIssue.Fields.WHITELISTED, whitelisted));
    }
    if (containsKey(OptionKey.DETAILS)) {
        if (!containsKey(OptionKey.TOOLING_ID)) {
            throw new BadRequestException("At least one toolingId is required to be able to filter issues by details.");
        }
        final Collection<String> toolingIds = getCollection(OptionKey.TOOLING_ID, String.class);
        final Options options = getOptions(OptionKey.DETAILS);
        final ExpressionBuilder toolingQuery = Expressions.builder();
        final Collection<ValidationIssueDetailExtension> extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions();
        for (String toolingId : toolingIds) {
            extensions.stream().filter(ext -> toolingId.equals(ext.getToolingId())).findFirst().ifPresent(extension -> {
                final ExpressionBuilder extensionQuery = Expressions.builder();
                extension.prepareQuery(extensionQuery, options);
                toolingQuery.should(extensionQuery.build());
            });
        }
        // at least one tooling should match
        toolingQuery.setMinimumNumberShouldMatch(1);
        queryBuilder.filter(toolingQuery.build());
    }
    return queryBuilder.build();
}
Also used : Options(com.b2international.commons.options.Options) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 12 with ExpressionBuilder

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

the class VersionSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(RepositoryContext context) {
    final ExpressionBuilder query = Expressions.builder();
    addIdFilter(query, VersionDocument.Expressions::ids);
    addFilter(query, OptionKey.RESOURCE_TYPE, String.class, VersionDocument.Expressions::resourceTypes);
    // TODO add a security filter to return commits from resources that can be accessed by the current user
    addFilter(query, OptionKey.RESOURCE, String.class, resources -> Expressions.builder().should(VersionDocument.Expressions.resources(resources)).should(VersionDocument.Expressions.resourceIds(resources)).build());
    addFilter(query, OptionKey.VERSION, String.class, VersionDocument.Expressions::versions);
    addFilter(query, OptionKey.RESOURCE_BRANCHPATH, String.class, VersionDocument.Expressions::resourceBranchPaths);
    addFilter(query, OptionKey.AUTHOR, String.class, VersionDocument.Expressions::authors);
    if (containsKey(OptionKey.CREATED_AT_FROM) || containsKey(OptionKey.CREATED_AT_TO)) {
        final Long createdAtFrom = containsKey(OptionKey.CREATED_AT_FROM) ? get(OptionKey.CREATED_AT_FROM, Long.class) : 0L;
        final Long createdAtTo = containsKey(OptionKey.CREATED_AT_TO) ? get(OptionKey.CREATED_AT_TO, Long.class) : Long.MAX_VALUE;
        query.filter(VersionDocument.Expressions.createdAt(createdAtFrom, createdAtTo));
    }
    if (containsKey(OptionKey.EFFECTIVE_TIME_START) || containsKey(OptionKey.EFFECTIVE_TIME_END)) {
        final long from = containsKey(OptionKey.EFFECTIVE_TIME_START) ? get(OptionKey.EFFECTIVE_TIME_START, Long.class) : 0;
        final long to = containsKey(OptionKey.EFFECTIVE_TIME_END) ? get(OptionKey.EFFECTIVE_TIME_END, Long.class) : Long.MAX_VALUE;
        query.filter(VersionDocument.Expressions.effectiveTime(from, to));
    }
    return query.build();
}
Also used : Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 13 with ExpressionBuilder

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

the class ValidationIssueDeleteRequest method execute.

@Override
public Boolean execute(ServiceProvider context) {
    ExpressionBuilder query = Expressions.builder();
    if (!CompareUtils.isEmpty(resourceURIs)) {
        query.filter(Expressions.matchAny(ValidationIssue.Fields.RESOURCE_URI, resourceURIs));
    }
    if (!CompareUtils.isEmpty(toolingIds)) {
        final Set<String> rulesToDelete = ValidationRequests.rules().prepareSearch().all().filterByToolings(toolingIds).build().execute(context).stream().map(ValidationRule::getId).collect(Collectors.toSet());
        query.filter(Expressions.matchAny(ValidationIssue.Fields.RULE_ID, rulesToDelete));
    }
    return context.service(ValidationRepository.class).write(writer -> {
        writer.bulkDelete(new BulkDelete<>(ValidationIssue.class, query.build()));
        writer.commit();
        new ValidationDeleteNotification(resourceURIs, toolingIds).publish(context.service(IEventBus.class));
        return Boolean.TRUE;
    });
}
Also used : ValidationRepository(com.b2international.snowowl.core.internal.validation.ValidationRepository) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ValidationDeleteNotification(com.b2international.snowowl.core.validation.ValidationDeleteNotification) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Example 14 with ExpressionBuilder

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

the class ValidationWhiteListSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(ServiceProvider context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addIdFilter(queryBuilder, ids -> Expressions.matchAny(ValidationWhiteList.Fields.ID, ids));
    if (containsKey(OptionKey.TERM)) {
        String searchTerm = getString(OptionKey.TERM);
        queryBuilder.must(Expressions.builder().should(Expressions.dismaxWithScoreCategories(Expressions.matchTextPhrase(ValidationWhiteList.Fields.AFFECTED_COMPONENT_LABELS_TEXT, searchTerm), Expressions.matchTextAll(ValidationWhiteList.Fields.AFFECTED_COMPONENT_LABELS_PREFIX, searchTerm))).should(Expressions.boost(Expressions.matchAny(ValidationWhiteList.Fields.COMPONENT_ID, Collections.singleton(searchTerm)), 1000f)).should(Expressions.boost(Expressions.prefixMatch(ValidationWhiteList.Fields.REPORTER, searchTerm), 1000f)).build());
    }
    if (containsKey(OptionKey.RULE_ID)) {
        Collection<String> ruleIds = getCollection(OptionKey.RULE_ID, String.class);
        queryBuilder.filter(Expressions.matchAny(ValidationWhiteList.Fields.RULE_ID, ruleIds));
    }
    if (containsKey(OptionKey.COMPONENT_ID)) {
        Collection<String> componentIds = getCollection(OptionKey.COMPONENT_ID, String.class);
        queryBuilder.filter(Expressions.matchAny(ValidationWhiteList.Fields.COMPONENT_ID, componentIds));
    }
    addFilter(queryBuilder, OptionKey.COMPONENT_TYPE, String.class, ids -> Expressions.matchAny(ValidationWhiteList.Fields.TERMINOLOGY_COMPONENT_ID, ids));
    if (containsKey(OptionKey.REPORTER)) {
        Collection<String> reporters = getCollection(OptionKey.REPORTER, String.class);
        queryBuilder.filter(Expressions.matchAny(ValidationWhiteList.Fields.REPORTER, reporters));
    }
    if (containsKey(OptionKey.CREATED_START) || containsKey(OptionKey.CREATED_END)) {
        final long createdAfter = containsKey(OptionKey.CREATED_START) ? get(OptionKey.CREATED_START, Long.class) : Long.MIN_VALUE;
        final long createdBefore = containsKey(OptionKey.CREATED_END) ? get(OptionKey.CREATED_END, Long.class) : Long.MAX_VALUE;
        queryBuilder.filter(Expressions.matchRange(ValidationWhiteList.Fields.CREATED_AT, createdAfter, createdBefore));
    }
    return queryBuilder.build();
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 15 with ExpressionBuilder

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

the class ValidationRuleSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(ServiceProvider context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addIdFilter(queryBuilder, ids -> Expressions.matchAny(ValidationRule.Fields.ID, ids));
    if (containsKey(OptionKey.SEVERITY)) {
        final Set<String> values = getCollection(OptionKey.SEVERITY, Severity.class).stream().map(Severity::name).collect(Collectors.toSet());
        queryBuilder.filter(Expressions.matchAny(ValidationRule.Fields.SEVERITY, values));
    }
    if (containsKey(OptionKey.TOOLING_ID)) {
        final Collection<String> toolingIds = getCollection(OptionKey.TOOLING_ID, String.class);
        queryBuilder.filter(Expressions.matchAny(ValidationRule.Fields.TOOLING_ID, toolingIds));
    }
    return queryBuilder.build();
}
Also used : Severity(com.b2international.snowowl.core.validation.rule.ValidationRule.Severity) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

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