Search in sources :

Example 66 with Expression

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

the class SnomedEclEvaluationRequestPropertyFilterTest method dialectAcceptable.

@Test
public void dialectAcceptable() throws Exception {
    generateAcceptableDescription(Concepts.ROOT_CONCEPT);
    // extra preferred description on another concept to demonstrate that it won't match
    generatePreferredDescription(Concepts.MODULE_ROOT);
    Expression actual = eval("* {{ dialect = en-gb (accept) }}");
    Expression expected = SnomedDocument.Expressions.ids(Set.of(Concepts.ROOT_CONCEPT));
    assertEquals(expected, actual);
}
Also used : Expression(com.b2international.index.query.Expression) Test(org.junit.Test)

Example 67 with Expression

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

the class SnomedValidationIssueDetailExtension method prepareQuery.

@Override
public void prepareQuery(ExpressionBuilder queryBuilder, Options options) {
    if (options.containsKey(COMPONENT_STATUS)) {
        final Boolean isActive = options.get(COMPONENT_STATUS, Boolean.class);
        queryBuilder.filter(Expressions.match(COMPONENT_STATUS, isActive));
    }
    if (options.containsKey(COMPONENT_MODULE_ID)) {
        final Collection<String> moduleIds = options.getCollection(COMPONENT_MODULE_ID, String.class);
        queryBuilder.filter(Expressions.matchAny(COMPONENT_MODULE_ID, moduleIds));
    }
    if (options.containsKey(CONCEPT_STATUS)) {
        final Boolean isConceptActive = options.get(CONCEPT_STATUS, Boolean.class);
        queryBuilder.filter(Expressions.match(CONCEPT_STATUS, isConceptActive));
    }
    if (options.containsKey(CONTENT_TYPE)) {
        final Boolean contentTypeFilter = options.get(CONTENT_TYPE, Boolean.class);
        if (contentTypeFilter != null && contentTypeFilter) {
            // published only
            if (options.containsKey(COMPONENT_EFFECTIVE_TIME_START) || options.containsKey(COMPONENT_EFFECTIVE_TIME_END)) {
                final Long start = options.get(COMPONENT_EFFECTIVE_TIME_START, Long.class);
                final Long end = options.get(COMPONENT_EFFECTIVE_TIME_END, Long.class);
                final Expression effectiveTimeExpression = Expressions.matchRange(SnomedDocument.Fields.EFFECTIVE_TIME, start == null ? 0L : start, end == null ? Long.MAX_VALUE : end);
                queryBuilder.filter(effectiveTimeExpression);
            } else {
                queryBuilder.filter(SnomedDocument.Expressions.effectiveTime(0L, Long.MAX_VALUE));
            }
        } else if (contentTypeFilter != null && !contentTypeFilter) {
            // unpublished only
            queryBuilder.filter(SnomedDocument.Expressions.effectiveTime(EffectiveTimes.UNSET_EFFECTIVE_TIME));
        }
    // Both
    }
}
Also used : Expression(com.b2international.index.query.Expression)

Example 68 with Expression

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

the class SnomedQueryValidationRuleEvaluator method eval.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<?> eval(BranchContext context, ValidationRule rule, Map<String, Object> params) throws Exception {
    checkArgument(type().equals(rule.getType()), "'%s' is not recognizable by this evaluator (accepts: %s)", rule, type());
    SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent> validationQuery = context.service(ObjectMapper.class).<SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent>>readValue(rule.getImplementation(), TYPE_REF);
    SnomedSearchRequestBuilder<?, PageableCollectionResource<SnomedComponent>> req = validationQuery.prepareSearch();
    String extensionModules = params != null && params.containsKey(ValidationConfiguration.MODULES) ? Strings.nullToEmpty((String) params.get(ValidationConfiguration.MODULES)) : "";
    String module = validationQuery.module;
    if (!Strings.isNullOrEmpty(module)) {
        // If there is a module given assume that it must be more specific and provided on purpose
        req.filterByModule(module);
    } else if (Boolean.TRUE.equals(validationQuery.extensionScope) && !Strings.isNullOrEmpty(extensionModules)) {
        req.filterByModule(extensionModules);
    }
    SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument> searchReq = (SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument>) req.build();
    final ExpressionBuilder expressionBuilder = Expressions.builder().filter(searchReq.toRawQuery(context));
    if (params != null && params.containsKey(ValidationConfiguration.IS_UNPUBLISHED_ONLY) && Boolean.TRUE.equals(params.get(ValidationConfiguration.IS_UNPUBLISHED_ONLY))) {
        expressionBuilder.filter(SnomedDocument.Expressions.effectiveTime(EffectiveTimes.UNSET_EFFECTIVE_TIME));
    }
    Expression where = expressionBuilder.build();
    // TODO check if the expression contains only the ID list, then skip scrolling and just report them
    List[] issues = { null };
    Query.select(String.class).from(validationQuery.getDocType()).fields(SnomedDocument.Fields.ID).where(where).limit(RULE_LIMIT).withScores(false).build().stream(context.service(RevisionSearcher.class)).forEachOrdered(page -> {
        if (issues[0] == null) {
            issues[0] = newArrayListWithExpectedSize(page.getTotal());
        }
        for (String affectedComponentId : page) {
            String affectedComponentType = SnomedComponent.getTypeSafe(affectedComponentId);
            if (TerminologyRegistry.UNKNOWN_COMPONENT_TYPE.equals(affectedComponentType)) {
                affectedComponentType = SnomedReferenceSetMember.TYPE;
            }
            issues[0].add(ComponentIdentifier.of(affectedComponentType, affectedComponentId));
        }
    });
    return issues[0] == null ? Collections.emptyList() : issues[0];
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SearchIndexResourceRequest(com.b2international.snowowl.core.request.SearchIndexResourceRequest) Expression(com.b2international.index.query.Expression) BranchContext(com.b2international.snowowl.core.domain.BranchContext) PageableCollectionResource(com.b2international.snowowl.core.domain.PageableCollectionResource) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 69 with Expression

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

the class DefaultRevisionWriter method setRevised.

private void setRevised(Class<?> type, Set<String> keysToUpdate, final String oldRevised, final String newRevised, RevisionBranchRef branchToUpdate) {
    if (Revision.class.isAssignableFrom(type)) {
        if (!keysToUpdate.isEmpty()) {
            final Map<String, Object> updateRevised = ImmutableMap.of("oldRevised", oldRevised, "newRevised", newRevised);
            for (List<String> keys : Lists.partition(List.copyOf(keysToUpdate), ((EsDocumentSearcher) index.searcher()).maxTermsCount())) {
                final Expression filter = Expressions.builder().filter(Expressions.matchAny(Revision.Fields.ID, keys)).filter(branchToUpdate.toRevisionFilter()).build();
                final BulkUpdate<Revision> update = new BulkUpdate<Revision>((Class<? extends Revision>) type, filter, Revision.UPDATE_REVISED, updateRevised);
                index.bulkUpdate(update);
            }
        }
    } else {
        index.remove(type, keysToUpdate);
    }
}
Also used : Expression(com.b2international.index.query.Expression) BulkUpdate(com.b2international.index.BulkUpdate)

Example 70 with Expression

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

the class SnomedConceptSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(BranchContext context) {
    ExpressionBuilder queryBuilder = Expressions.builder();
    addActiveClause(queryBuilder);
    addReleasedClause(queryBuilder);
    addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
    addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
    addEclFilter(context, queryBuilder, OptionKey.DEFINITION_STATUS, SnomedConceptDocument.Expressions::definitionStatusIds);
    addNamespaceFilter(queryBuilder);
    addNamespaceConceptIdFilter(context, queryBuilder);
    addEffectiveTimeClause(queryBuilder);
    addActiveMemberOfClause(context, queryBuilder);
    addMemberOfClause(context, queryBuilder);
    addFilter(queryBuilder, OptionKey.PARENT, String.class, SnomedConceptDocument.Expressions::parents);
    addFilter(queryBuilder, OptionKey.STATED_PARENT, String.class, SnomedConceptDocument.Expressions::statedParents);
    if (containsKey(OptionKey.ANCESTOR)) {
        final Collection<String> ancestorIds = getCollection(OptionKey.ANCESTOR, String.class);
        queryBuilder.filter(Expressions.builder().should(parents(ancestorIds)).should(ancestors(ancestorIds)).build());
    }
    if (containsKey(OptionKey.STATED_ANCESTOR)) {
        final Collection<String> ancestorIds = getCollection(OptionKey.STATED_ANCESTOR, String.class);
        queryBuilder.filter(Expressions.builder().should(statedParents(ancestorIds)).should(statedAncestors(ancestorIds)).build());
    }
    if (containsKey(OptionKey.ECL)) {
        final String ecl = getString(OptionKey.ECL);
        Expression eclExpression = EclExpression.of(ecl, Trees.INFERRED_FORM).resolveToExpression(context).getSync(3, TimeUnit.MINUTES);
        if (eclExpression.isMatchNone()) {
            throw new NoResultException();
        } else if (!eclExpression.isMatchAll()) {
            queryBuilder.filter(eclExpression);
        }
    }
    if (containsKey(OptionKey.STATED_ECL)) {
        final String ecl = getString(OptionKey.STATED_ECL);
        Expression statedEclExpression = EclExpression.of(ecl, Trees.STATED_FORM).resolveToExpression(context).getSync(3, TimeUnit.MINUTES);
        if (statedEclExpression.isMatchNone()) {
            throw new NoResultException();
        } else if (!statedEclExpression.isMatchAll()) {
            queryBuilder.filter(statedEclExpression);
        }
    }
    Expression searchProfileQuery = null;
    final Expression queryExpression;
    if (containsKey(OptionKey.SEMANTIC_TAG)) {
        queryBuilder.filter(SnomedConceptDocument.Expressions.semanticTags(getCollection(OptionKey.SEMANTIC_TAG, String.class)));
    }
    if (containsKey(OptionKey.TERM)) {
        final ExpressionBuilder bq = Expressions.builder();
        // nest current query
        bq.filter(queryBuilder.build());
        queryBuilder = bq;
        final TermFilter termFilter = containsKey(OptionKey.TERM) ? get(OptionKey.TERM, TermFilter.class) : null;
        final Map<String, Float> conceptScoreMap = executeDescriptionSearch(context, termFilter);
        if (termFilter != null) {
            try {
                final ComponentCategory category = SnomedIdentifiers.getComponentCategory(termFilter.getTerm());
                if (category == ComponentCategory.CONCEPT) {
                    conceptScoreMap.put(termFilter.getTerm(), Float.MAX_VALUE);
                }
            } catch (IllegalArgumentException e) {
            // ignored
            }
        }
        if (conceptScoreMap.isEmpty()) {
            throw new NoResultException();
        }
        queryBuilder.filter(RevisionDocument.Expressions.ids(conceptScoreMap.keySet()));
        final Expression q = addSearchProfile(searchProfileQuery, queryBuilder.build());
        queryExpression = Expressions.scriptScore(q, "doiFactor", Map.of("termScores", conceptScoreMap, "useDoi", containsKey(OptionKey.USE_DOI), "minDoi", MIN_DOI_VALUE, "maxDoi", MAX_DOI_VALUE));
    } else if (containsKey(OptionKey.USE_DOI)) {
        final Expression q = addSearchProfile(searchProfileQuery, queryBuilder.build());
        queryExpression = Expressions.scriptScore(q, "doi");
    } else {
        queryExpression = addSearchProfile(searchProfileQuery, queryBuilder.build());
    }
    return queryExpression;
}
Also used : EclExpression(com.b2international.snowowl.snomed.core.ecl.EclExpression) Expression(com.b2international.index.query.Expression) TermFilter(com.b2international.snowowl.core.request.TermFilter) Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory)

Aggregations

Expression (com.b2international.index.query.Expression)151 Test (org.junit.Test)142 BaseRevisionIndexTest (com.b2international.index.revision.BaseRevisionIndexTest)8 Ignore (org.junit.Ignore)5 Expressions (com.b2international.index.query.Expressions)2 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)2 RevisionData (com.b2international.index.revision.RevisionFixtures.RevisionData)2 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)2 BranchContext (com.b2international.snowowl.core.domain.BranchContext)2 Options (com.b2international.commons.options.Options)1 BulkUpdate (com.b2international.index.BulkUpdate)1 Searcher (com.b2international.index.Searcher)1 Query (com.b2international.index.query.Query)1 RangeData (com.b2international.index.revision.RevisionFixtures.RangeData)1 Ecl (com.b2international.snomed.ecl.Ecl)1 Any (com.b2international.snomed.ecl.ecl.Any)1 EclConceptReference (com.b2international.snomed.ecl.ecl.EclConceptReference)1 ExpressionConstraint (com.b2international.snomed.ecl.ecl.ExpressionConstraint)1 NestedExpression (com.b2international.snomed.ecl.ecl.NestedExpression)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1