Search in sources :

Example 1 with EclExpression

use of com.b2international.snowowl.snomed.core.ecl.EclExpression 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)1 Expressions (com.b2international.index.query.Expressions)1 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 TermFilter (com.b2international.snowowl.core.request.TermFilter)1 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)1 EclExpression (com.b2international.snowowl.snomed.core.ecl.EclExpression)1