Search in sources :

Example 26 with ExpressionBuilder

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

the class SnomedRefSetSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(BranchContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addActiveClause(queryBuilder);
    addReleasedClause(queryBuilder);
    addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
    addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
    addEffectiveTimeClause(queryBuilder);
    if (containsKey(OptionKey.TYPE)) {
        queryBuilder.filter(refSetTypes(getCollection(OptionKey.TYPE, SnomedRefSetType.class)));
    } else {
        // always add type filter, so only concept docs with refset props will be returned
        queryBuilder.filter(Expressions.exists(SnomedConceptDocument.Fields.REFSET_TYPE));
    }
    if (containsKey(OptionKey.REFERENCED_COMPONENT_TYPE)) {
        queryBuilder.filter(referencedComponentTypes(getCollection(OptionKey.REFERENCED_COMPONENT_TYPE, String.class)));
    }
    if (containsKey(OptionKey.MAP_TARGET_COMPONENT_TYPE)) {
        queryBuilder.filter(mapTargetComponentTypes(getCollection(OptionKey.MAP_TARGET_COMPONENT_TYPE, String.class)));
    }
    return queryBuilder.build();
}
Also used : Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 27 with ExpressionBuilder

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

the class RelationshipChangeSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(final RepositoryContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addClassificationIdClause(queryBuilder);
    addSourceIdClause(queryBuilder);
    addDestinationIdClause(queryBuilder);
    return queryBuilder.build();
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 28 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder 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)

Example 29 with ExpressionBuilder

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

the class EquivalentConceptSetSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(final RepositoryContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addClassificationIdClause(queryBuilder);
    addConceptIdClause(queryBuilder);
    return queryBuilder.build();
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 30 with ExpressionBuilder

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

the class Taxonomies method getStatements.

private static Collection<Object[]> getStatements(RevisionSearcher searcher, LongCollection conceptIds, String characteristicTypeId, boolean filterByConceptIds) throws IOException {
    // merge stated relationships and OWL axiom relationships into a single array
    ImmutableList.Builder<Object[]> isaStatementsBuilder = ImmutableList.builder();
    final Set<String> concepts = LongSets.toStringSet(conceptIds);
    ExpressionBuilder activeIsaRelationshipQuery = Expressions.builder().filter(active()).filter(typeId(Concepts.IS_A)).filter(characteristicTypeId(characteristicTypeId));
    if (filterByConceptIds) {
        activeIsaRelationshipQuery.filter(sourceIds(concepts)).filter(destinationIds(concepts));
    }
    final Query<String[]> activeStatedISARelationshipsQuery = Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(activeIsaRelationshipQuery.build()).limit(Integer.MAX_VALUE).build();
    Hits<String[]> activeIsaRelationships = searcher.search(activeStatedISARelationshipsQuery);
    activeIsaRelationships.forEach(activeIsaRelationship -> {
        isaStatementsBuilder.add(new Object[] { activeIsaRelationship[0], Long.parseLong(activeIsaRelationship[1]), new long[] { Long.parseLong(activeIsaRelationship[2]) } });
    });
    activeIsaRelationships = null;
    if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
        // search existing axioms defined for the given set of conceptIds
        ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active());
        if (filterByConceptIds) {
            activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(concepts)).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).filter(destinationIds(concepts)).build()));
        } else {
            activeOwlAxiomMemberQuery.filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).build()));
        }
        final Query<SnomedRefSetMemberIndexEntry> activeAxiomISARelationshipsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
        Hits<SnomedRefSetMemberIndexEntry> activeAxiomISARelationships = searcher.search(activeAxiomISARelationshipsQuery);
        activeAxiomISARelationships.forEach(owlMember -> {
            if (!CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())) {
                // XXX: breaks with a NumberFormatException if any of the IS A relationships has a value
                long[] destinationIds = owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> Concepts.IS_A.equals(classAxiom.getTypeId())).map(SnomedOWLRelationshipDocument::getDestinationId).mapToLong(Long::parseLong).toArray();
                isaStatementsBuilder.add(new Object[] { owlMember.getId(), Long.parseLong(owlMember.getReferencedComponentId()), destinationIds });
            }
        });
        activeAxiomISARelationships = null;
    }
    return isaStatementsBuilder.build();
}
Also used : Query(com.b2international.index.query.Query) Hits(com.b2international.index.Hits) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) LoggerFactory(org.slf4j.LoggerFactory) LongCollections(com.b2international.collections.longs.LongCollections) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult) Expressions.typeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.typeId) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ImmutableList(com.google.common.collect.ImmutableList) LongSets(com.b2international.commons.collect.LongSets) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) Logger(org.slf4j.Logger) LongIterator(com.b2international.collections.longs.LongIterator) LongCollection(com.b2international.collections.longs.LongCollection) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Expressions.characteristicTypeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.characteristicTypeId) Expressions.sourceIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.sourceIds) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) Expressions.destinationIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.destinationIds) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) StagingArea(com.b2international.index.revision.StagingArea) IComponent(com.b2international.snowowl.core.domain.IComponent) Collections(java.util.Collections) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) ImmutableList(com.google.common.collect.ImmutableList) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)

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