Search in sources :

Example 16 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class SnomedConceptSearchRequestEvaluator method evaluate.

@Override
public Concepts evaluate(ResourceURI uri, ServiceProvider context, Options search) {
    final String preferredDisplay = search.getString(OptionKey.DISPLAY);
    SnomedDisplayTermType displayTermType;
    if (preferredDisplay != null) {
        displayTermType = SnomedDisplayTermType.getEnum(preferredDisplay);
    } else {
        displayTermType = SnomedDisplayTermType.PT;
    }
    final SnomedConceptSearchRequestBuilder req = SnomedRequests.prepareSearchConcept();
    evaluateTermFilterOptions(req, search);
    if (search.containsKey(OptionKey.ID)) {
        req.filterByIds(search.getCollection(OptionKey.ID, String.class));
    }
    if (search.containsKey(OptionKey.ACTIVE)) {
        req.filterByActive(search.getBoolean(OptionKey.ACTIVE));
    }
    if (search.containsKey(OptionKey.PARENT)) {
        req.filterByParents(search.getCollection(OptionKey.PARENT, String.class));
    }
    if (search.containsKey(OptionKey.ANCESTOR)) {
        req.filterByAncestors(search.getCollection(OptionKey.ANCESTOR, String.class));
    }
    if (search.containsKey(OptionKey.TERM_TYPE)) {
        req.filterByDescriptionType(search.getString(OptionKey.TERM_TYPE));
    }
    if (search.containsKey(OptionKey.QUERY) || search.containsKey(OptionKey.MUST_NOT_QUERY)) {
        StringBuilder query = new StringBuilder();
        if (search.containsKey(OptionKey.QUERY)) {
            query.append("(").append(Ecl.or(search.getCollection(OptionKey.QUERY, String.class))).append(")");
        } else {
            query.append(Ecl.ANY);
        }
        if (search.containsKey(OptionKey.MUST_NOT_QUERY)) {
            query.append(" MINUS (").append(Ecl.or(search.getCollection(OptionKey.MUST_NOT_QUERY, String.class))).append(")");
        }
        req.filterByEcl(query.toString());
    }
    boolean requestedExpand = search.containsKey(OptionKey.EXPAND);
    // make sure preferredDescriptions() and displayTermType expansion data are always loaded
    Options expand = ExpandParser.parse("preferredDescriptions()").merge(requestedExpand ? search.getOptions(OptionKey.EXPAND) : Options.empty());
    if (!Strings.isNullOrEmpty(displayTermType.getExpand())) {
        expand = ExpandParser.parse(displayTermType.getExpand()).merge(expand);
    }
    SnomedConcepts matches = req.setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setSearchAfter(search.getString(OptionKey.AFTER)).setLimit(search.get(OptionKey.LIMIT, Integer.class)).setFields(search.getList(OptionKey.FIELDS, String.class)).setExpand(expand).sortBy(search.containsKey(SearchResourceRequest.OptionKey.SORT_BY) ? search.getList(SearchResourceRequest.OptionKey.SORT_BY, SearchResourceRequest.Sort.class) : null).build(uri).execute(context);
    return new Concepts(matches.stream().map(concept -> toConcept(uri, concept, displayTermType.getLabel(concept), requestedExpand)).collect(Collectors.toList()), matches.getSearchAfter(), matches.getLimit(), matches.getTotal());
}
Also used : Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Concepts(com.b2international.snowowl.core.domain.Concepts) SnomedDisplayTermType(com.b2international.snowowl.snomed.core.SnomedDisplayTermType) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest)

Example 17 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class SnomedDescendantsExpander method expand.

@Override
protected void expand(List<SnomedConcept> results, final Set<String> conceptIds, Options descendantExpandOptions, boolean direct) {
    try {
        final int limit = getLimit(descendantExpandOptions);
        final ExpressionBuilder expression = Expressions.builder();
        expression.filter(active());
        final ExpressionBuilder descendantFilter = Expressions.builder();
        if (stated) {
            descendantFilter.should(statedParents(conceptIds));
            if (!direct) {
                descendantFilter.should(statedAncestors(conceptIds));
            }
        } else {
            descendantFilter.should(parents(conceptIds));
            if (!direct) {
                descendantFilter.should(ancestors(conceptIds));
            }
        }
        expression.filter(descendantFilter.build());
        final Query<SnomedConceptDocument> query = Query.select(SnomedConceptDocument.class).where(expression.build()).limit((conceptIds.size() == 1 && limit == 0) ? limit : Integer.MAX_VALUE).build();
        final RevisionSearcher searcher = context().service(RevisionSearcher.class);
        final Hits<SnomedConceptDocument> hits = searcher.search(query);
        if (hits.getTotal() < 1) {
            final SnomedConcepts descendants = new SnomedConcepts(0, 0);
            for (SnomedConcept concept : results) {
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        // XXX won't work if number of results is greater than one, either use custom ConceptSearch or figure out how to expand descendants effectively
        if (conceptIds.size() == 1 && limit == 0) {
            for (SnomedConcept concept : results) {
                final SnomedConcepts descendants = new SnomedConcepts(0, hits.getTotal());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        final Multimap<String, String> descendantsByAncestor = TreeMultimap.create();
        for (SnomedConceptDocument hit : hits) {
            final Set<String> parentsAndAncestors = newHashSet();
            if (stated) {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedAncestors()));
                }
            } else {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getAncestors()));
                }
            }
            parentsAndAncestors.retainAll(conceptIds);
            for (String ancestor : parentsAndAncestors) {
                descendantsByAncestor.put(ancestor, hit.getId());
            }
        }
        final Collection<String> componentIds = newHashSet(descendantsByAncestor.values());
        if (limit > 0 && !componentIds.isEmpty()) {
            // query descendants again
            final SnomedConcepts descendants = SnomedRequests.prepareSearchConcept().all().filterByIds(componentIds).setLocales(locales()).setExpand(descendantExpandOptions.get("expand", Options.class)).build().execute(context());
            final Map<String, SnomedConcept> descendantsById = newHashMap();
            descendantsById.putAll(Maps.uniqueIndex(descendants, SnomedConcept::getId));
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final List<SnomedConcept> currentDescendants = FluentIterable.from(descendantIds).limit(limit).transform(Functions.forMap(descendantsById)).toList();
                final SnomedConcepts descendantConcepts = new SnomedConcepts(currentDescendants, null, limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendantConcepts);
                } else {
                    concept.setDescendants(descendantConcepts);
                }
            }
        } else {
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final SnomedConcepts descendants = new SnomedConcepts(limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
        }
    } catch (IOException e) {
        throw SnowowlRuntimeException.wrap(e);
    }
}
Also used : SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) IOException(java.io.IOException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Example 18 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetConverter method expandMembers.

private void expandMembers(List<SnomedReferenceSet> results) {
    if (expand().containsKey("members")) {
        Options expandOptions = expand().get("members", Options.class);
        for (SnomedReferenceSet refSet : results) {
            SnomedRefSetMemberSearchRequestBuilder req = SnomedRequests.prepareSearchMember().filterByRefSet(refSet.getId()).setLocales(locales()).setExpand(expandOptions.get("expand", Options.class));
            if (expandOptions.containsKey("limit")) {
                req.setLimit(expandOptions.get("limit", Integer.class));
            }
            refSet.setMembers(req.build().execute(context()));
        }
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder)

Example 19 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class SnomedRelationshipConverter method expandModifier.

private void expandModifier(List<SnomedRelationship> results) {
    if (expand().containsKey(SnomedRelationship.Expand.MODIFIER)) {
        final Options modifierOptions = expand().get(SnomedRelationship.Expand.MODIFIER, Options.class);
        final Iterable<String> modifierIds = FluentIterable.from(results).transform(SnomedRelationship::getModifierId);
        context().service(SnomedConceptRequestCache.class).request(modifierIds, modifierOptions.getOptions("expand"), locales(), modifiersById -> {
            for (SnomedRelationship relationship : results) {
                ((SnomedRelationship) relationship).setModifier(modifiersById.get(relationship.getModifierId()));
            }
        });
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) SnomedConceptRequestCache(com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache)

Example 20 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class SnomedRelationshipConverter method expandDestination.

private void expandDestination(List<SnomedRelationship> results) {
    if (expand().containsKey(SnomedRelationship.Expand.DESTINATION)) {
        final Options destinationOptions = expand().get(SnomedRelationship.Expand.DESTINATION, Options.class);
        final Iterable<String> destinationConceptIds = FluentIterable.from(results).filter(// skip expand on relationships with value
        r -> !r.hasValue()).transform(SnomedRelationship::getDestinationId);
        context().service(SnomedConceptRequestCache.class).request(destinationConceptIds, destinationOptions.getOptions("expand"), locales(), destinationConceptsById -> {
            for (SnomedRelationship relationship : results) {
                final String destinationId = relationship.getDestinationId();
                // containsKey handles any null values here
                if (destinationConceptsById.containsKey(destinationId)) {
                    final SnomedConcept destinationConcept = destinationConceptsById.get(destinationId);
                    ((SnomedRelationship) relationship).setDestination(destinationConcept);
                }
            }
        });
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) Set(java.util.Set) SnomedRelationships(com.b2international.snowowl.snomed.core.domain.SnomedRelationships) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) BaseRevisionResourceConverter(com.b2international.snowowl.core.request.BaseRevisionResourceConverter) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) List(java.util.List) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) FluentIterable(com.google.common.collect.FluentIterable) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedConceptRequestCache(com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache) Options(com.b2international.commons.options.Options) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) SnomedConceptRequestCache(com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Aggregations

Options (com.b2international.commons.options.Options)57 List (java.util.List)15 Collectors (java.util.stream.Collectors)14 Set (java.util.Set)13 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)11 BranchContext (com.b2international.snowowl.core.domain.BranchContext)11 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)11 SnomedConceptRequestCache (com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache)11 Test (org.junit.Test)9 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)8 Map (java.util.Map)8 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)7 BadRequestException (com.b2international.commons.exceptions.BadRequestException)6 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)6 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)6 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)6 Multimaps (com.google.common.collect.Multimaps)6 Collection (java.util.Collection)6