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);
}
Aggregations