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