Search in sources :

Example 1 with TermFilter

use of com.b2international.snowowl.core.request.TermFilter 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)

Example 2 with TermFilter

use of com.b2international.snowowl.core.request.TermFilter in project snow-owl by b2ihealthcare.

the class SnomedDescriptionSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(BranchContext context) {
    if (containsKey(OptionKey.TERM) && get(OptionKey.TERM, TermFilter.class).getTerm().length() < 2) {
        throw new BadRequestException("Description term must be at least 2 characters long.");
    }
    final ExpressionBuilder queryBuilder = Expressions.builder();
    // Add (presumably) most selective filters first
    addActiveClause(queryBuilder);
    addReleasedClause(queryBuilder);
    addLanguageFilter(queryBuilder);
    addNamespaceFilter(queryBuilder);
    addNamespaceConceptIdFilter(context, queryBuilder);
    addActiveMemberOfClause(context, queryBuilder);
    addMemberOfClause(context, queryBuilder);
    addEclFilter(context, queryBuilder, OptionKey.LANGUAGE_REFSET, ids -> {
        return Expressions.builder().should(preferredIn(ids)).should(acceptableIn(ids)).build();
    });
    addEclFilter(context, queryBuilder, OptionKey.ACCEPTABLE_IN, ids -> acceptableIn(ids));
    addEclFilter(context, queryBuilder, OptionKey.PREFERRED_IN, ids -> preferredIn(ids));
    // apply locale based filters
    addFilter(queryBuilder, OptionKey.LANGUAGE_REFSET_LOCALES, ExtendedLocale.class, locales -> {
        final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
        return Expressions.builder().should(preferredIn(languageRefSetIds)).should(acceptableIn(languageRefSetIds)).build();
    });
    addFilter(queryBuilder, OptionKey.ACCEPTABLE_IN_LOCALES, ExtendedLocale.class, locales -> {
        final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
        return acceptableIn(languageRefSetIds);
    });
    addFilter(queryBuilder, OptionKey.PREFERRED_IN_LOCALES, ExtendedLocale.class, locales -> {
        final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
        return preferredIn(languageRefSetIds);
    });
    addEffectiveTimeClause(queryBuilder);
    addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
    addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
    addEclFilter(context, queryBuilder, OptionKey.CONCEPT, SnomedDescriptionIndexEntry.Expressions::concepts);
    addEclFilter(context, queryBuilder, OptionKey.TYPE, SnomedDescriptionIndexEntry.Expressions::types);
    addEclFilter(context, queryBuilder, OptionKey.CASE_SIGNIFICANCE, SnomedDescriptionIndexEntry.Expressions::caseSignificances);
    if (containsKey(OptionKey.SEMANTIC_TAG)) {
        queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTags(getCollection(OptionKey.SEMANTIC_TAG, String.class)));
    }
    if (containsKey(OptionKey.SEMANTIC_TAG_REGEX)) {
        queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTagRegex(getString(OptionKey.SEMANTIC_TAG_REGEX)));
    }
    if (containsKey(OptionKey.TERM_REGEX)) {
        final String regex = getString(OptionKey.TERM_REGEX);
        queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.matchTermRegex(regex));
    }
    if (containsKey(OptionKey.TERM)) {
        final TermFilter termFilter = get(OptionKey.TERM, TermFilter.class);
        queryBuilder.must(toDescriptionTermQuery(termFilter));
    }
    return queryBuilder.build();
}
Also used : ExtendedLocale(com.b2international.commons.http.ExtendedLocale) TermFilter(com.b2international.snowowl.core.request.TermFilter) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Expressions) Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Aggregations

Expressions (com.b2international.index.query.Expressions)2 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)2 TermFilter (com.b2international.snowowl.core.request.TermFilter)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Expression (com.b2international.index.query.Expression)1 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)1 EclExpression (com.b2international.snowowl.snomed.core.ecl.EclExpression)1 Expressions (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Expressions)1