Search in sources :

Example 1 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequest method doExecute.

@Override
protected Concepts doExecute(ServiceProvider context) throws IOException {
    final int limit = limit();
    Options conceptSearchOptions = Options.builder().putAll(options()).put(ConceptSearchRequestEvaluator.OptionKey.ID, componentIds()).put(ConceptSearchRequestEvaluator.OptionKey.AFTER, searchAfter()).put(ConceptSearchRequestEvaluator.OptionKey.LIMIT, limit).put(ConceptSearchRequestEvaluator.OptionKey.LOCALES, locales()).put(ConceptSearchRequestEvaluator.OptionKey.FIELDS, fields()).put(ConceptSearchRequestEvaluator.OptionKey.EXPAND, expand()).put(SearchResourceRequest.OptionKey.SORT_BY, sortBy()).build();
    final CodeSystemSearchRequestBuilder codeSystemSearchReq = CodeSystemRequests.prepareSearchCodeSystem().all();
    final Map<ResourceURI, ResourceURI> codeSystemResourceFiltersByResource;
    if (containsKey(OptionKey.CODESYSTEM)) {
        // remove path so we can use the code resource URI as key
        codeSystemResourceFiltersByResource = Maps.uniqueIndex(getCollection(OptionKey.CODESYSTEM, ResourceURI.class), uri -> uri.withoutPath());
        // for filtering use the keys
        codeSystemSearchReq.filterByIds(codeSystemResourceFiltersByResource.keySet().stream().map(ResourceURI::getResourceId).collect(Collectors.toSet()));
    } else {
        codeSystemResourceFiltersByResource = Collections.emptyMap();
    }
    // .filterByToolingIds(toolingIds) TODO perform TOOLING filtering
    // .filterByUrls(urls) TODO perform URL filtering
    List<Concepts> concepts = codeSystemSearchReq.buildAsync().execute(context).stream().map(codeSystem -> {
        final ResourceURI uriToEvaluateOn = codeSystemResourceFiltersByResource.getOrDefault(codeSystem.getResourceURI(), codeSystem.getResourceURI());
        return context.service(RepositoryManager.class).get(codeSystem.getToolingId()).service(ConceptSearchRequestEvaluator.class).evaluate(uriToEvaluateOn, context, conceptSearchOptions);
    }).collect(Collectors.toList());
    // for single CodeSystem searches, sorting, paging works as it should
    if (concepts.size() == 1) {
        return Iterables.getOnlyElement(concepts);
    }
    // otherwise, check if searchAfter was used, as it would return bogus results; it can not be applied across code systems
    if (searchAfter() != null) {
        throw new BadRequestException("searchAfter is not supported in Concept Search API for multiple code systems.");
    }
    // calculate grand total
    int total = 0;
    for (Concepts conceptsToAdd : concepts) {
        total += conceptsToAdd.getTotal();
    }
    return new Concepts(// TODO add manual sorting here if multiple resources have been fetched
    concepts.stream().flatMap(Concepts::stream).limit(limit).collect(Collectors.toList()), null, /* not supported across codesystems */
    limit, total);
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) Iterables(com.google.common.collect.Iterables) RepositoryManager(com.b2international.snowowl.core.RepositoryManager) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) List(java.util.List) Options(com.b2international.commons.options.Options) Concepts(com.b2international.snowowl.core.domain.Concepts) CodeSystemSearchRequestBuilder(com.b2international.snowowl.core.codesystem.CodeSystemSearchRequestBuilder) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) Collections(java.util.Collections) ResourceURI(com.b2international.snowowl.core.ResourceURI) Options(com.b2international.commons.options.Options) ResourceURI(com.b2international.snowowl.core.ResourceURI) Concepts(com.b2international.snowowl.core.domain.Concepts) CodeSystemSearchRequestBuilder(com.b2international.snowowl.core.codesystem.CodeSystemSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 2 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequestSnomedTest method hitCount.

@Test
public void hitCount() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().setLimit(0).filterByCodeSystemUri(CODESYSTEM).buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1888);
}
Also used : Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 3 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequestSnomedTest method setPreferreDisplayToFsn.

@Test
public void setPreferreDisplayToFsn() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(CODESYSTEM).filterById(ID).setPreferredDisplay("FSN").setLocales("en").buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1);
    final Concept concept = matches.first().get();
    assertThat(concept.getTerm()).isEqualTo(FSN);
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 4 with Concepts

use of com.b2international.snowowl.core.domain.Concepts 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 5 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class CodeSystemResourceTypeConverter method expand.

@Override
public void expand(RepositoryContext context, Options expand, List<ExtendedLocale> locales, Collection<Resource> results) {
    if (expand.containsKey("content")) {
        // allow expanding content via content expansion, for now hit count only
        results.forEach(codeSystem -> {
            final Concepts concepts = CodeSystemRequests.prepareSearchConcepts().setLimit(0).filterByCodeSystemUri(codeSystem.getResourceURI()).buildAsync().execute(context);
            codeSystem.setProperties("content", concepts);
        });
    }
}
Also used : Concepts(com.b2international.snowowl.core.domain.Concepts)

Aggregations

Concepts (com.b2international.snowowl.core.domain.Concepts)10 Test (org.junit.Test)5 Concept (com.b2international.snowowl.core.domain.Concept)4 Options (com.b2international.commons.options.Options)2 ResourceURI (com.b2international.snowowl.core.ResourceURI)2 IOException (java.io.IOException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 TextConstants (com.b2international.index.compat.TextConstants)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)1 CodeSystemSearchRequestBuilder (com.b2international.snowowl.core.codesystem.CodeSystemSearchRequestBuilder)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 Suggestions (com.b2international.snowowl.core.domain.Suggestions)1 ConceptSearchRequestBuilder (com.b2international.snowowl.core.request.ConceptSearchRequestBuilder)1 OptionKey (com.b2international.snowowl.core.request.ConceptSearchRequestEvaluator.OptionKey)1 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)1 ResourceResponseEntry (com.b2international.snowowl.fhir.core.model.ResourceResponseEntry)1