Search in sources :

Example 1 with CodeSystemSearchRequestBuilder

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

Aggregations

BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 Options (com.b2international.commons.options.Options)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)1 CodeSystemSearchRequestBuilder (com.b2international.snowowl.core.codesystem.CodeSystemSearchRequestBuilder)1 Concepts (com.b2international.snowowl.core.domain.Concepts)1 Iterables (com.google.common.collect.Iterables)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1