use of org.snomed.snowstorm.core.data.domain.QueryConcept in project snowstorm by IHTSDO.
the class MRCMService method retrieveConceptModelAttributeHierarchy.
public ConceptMini retrieveConceptModelAttributeHierarchy(String branch, List<LanguageDialect> languageDialects) {
logger.info("Loading concept model attribute hierarchy.");
TimerUtil timer = new TimerUtil("attribute-tree", Level.INFO);
String topId = Concepts.CONCEPT_MODEL_ATTRIBUTE;
long topIdLong = parseLong(topId);
// Load all attributes including terms
List<ConceptMini> allAttributes = ecl("<<" + topId, branch, languageDialects);
timer.checkpoint("load all with terms");
Map<Long, ConceptMini> attributeMap = allAttributes.stream().collect(Collectors.toMap(ConceptMini::getConceptIdAsLong, Function.identity()));
if (!attributeMap.containsKey(topIdLong)) {
throw new IllegalStateException("Concept not found: " + topId + " | Concept model attribute (attribute) |.");
}
Set<Long> remainingAttributes = new HashSet<>(attributeMap.keySet());
remainingAttributes.remove(topIdLong);
BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(branch);
NativeSearchQueryBuilder queryConceptQuery = new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteria.getEntityBranchCriteria(QueryConcept.class)).must(termQuery(QueryConcept.Fields.STATED, false)).filter(termsQuery(QueryConcept.Fields.CONCEPT_ID, remainingAttributes))).withFields(QueryConcept.Fields.CONCEPT_ID, QueryConcept.Fields.PARENTS).withPageable(LARGE_PAGE);
try (SearchHitsIterator<QueryConcept> queryConcepts = elasticsearchTemplate.searchForStream(queryConceptQuery.build(), QueryConcept.class)) {
queryConcepts.forEachRemaining(hit -> {
for (Long parent : hit.getContent().getParents()) {
ConceptMini parentMini = attributeMap.get(parent);
if (parentMini.getExtraFields() == null || parentMini.getExtraFields().get(CHILDREN) == null) {
parentMini.addExtraField(CHILDREN, new ArrayList<>());
}
@SuppressWarnings("unchecked") List<ConceptMini> children = (List<ConceptMini>) parentMini.getExtraFields().get(CHILDREN);
children.add(attributeMap.get(hit.getContent().getConceptIdL()));
children.sort(Comparator.comparing(ConceptMini::getFsnTerm));
}
});
}
timer.finish();
return attributeMap.get(topIdLong);
}
use of org.snomed.snowstorm.core.data.domain.QueryConcept in project snowstorm by IHTSDO.
the class RefsetDescriptorUpdaterService method preCommitCompletion.
/**
* If the commit is creating a new Reference Set, update the 900000000000456007 |Reference set descriptor reference set (foundation metadata concept)|
* Reference Set by adding a new entry to the Reference Set for the new Reference Set being created.
*
* @param commit Commit to process.
* @throws IllegalStateException When commit should fail.
*/
@Override
public void preCommitCompletion(Commit commit) throws IllegalStateException {
if (BranchMetadataHelper.isImportingCodeSystemVersion(commit)) {
logger.info("RefSet Descriptor auto update is disabled on branch {}", commit.getBranch().getPath());
return;
}
if (Commit.CommitType.CONTENT != commit.getCommitType()) {
logger.debug("CommitType is not CONTENT. Nothing to do.");
return;
}
SearchHits<QueryConcept> searchHits = elasticsearchTemplate.search(new NativeSearchQueryBuilder().withQuery(boolQuery().must(termQuery(QueryConcept.Fields.START, commit.getTimepoint().getTime())).must(termQuery(QueryConcept.Fields.ANCESTORS, Concepts.REFSET)).must(termQuery(QueryConcept.Fields.STATED, true))).build(), QueryConcept.class);
boolean creatingRefSet = searchHits.hasSearchHits();
if (!creatingRefSet) {
return;
}
String branchPath = commit.getBranch().getPath();
for (SearchHit<QueryConcept> searchHit : searchHits.getSearchHits()) {
long conceptIdL = searchHit.getContent().getConceptIdL();
String conceptIdS = String.valueOf(conceptIdL);
List<ReferenceSetMember> members = referenceSetMemberService.findMembers(branchPath, new MemberSearchRequest().referencedComponentId(conceptIdS).referenceSet(Concepts.REFSET_DESCRIPTOR_REFSET), PAGE_REQUEST).getContent();
if (members.isEmpty()) {
doAddMemberToRefset(commit, conceptIdL, branchPath);
}
}
}
use of org.snomed.snowstorm.core.data.domain.QueryConcept in project snowstorm by IHTSDO.
the class ConceptSelectorHelper method fetchIds.
public static Page<Long> fetchIds(BoolQueryBuilder query, Collection<Long> filterByConceptIds, Function<QueryConcept, Boolean> inclusionFilter, PageRequest pageRequest, QueryService queryService) {
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(query).withFields(getRequiredFields(inclusionFilter));
if (filterByConceptIds != null) {
searchQueryBuilder.withFilter(termsQuery(QueryConcept.Fields.CONCEPT_ID, filterByConceptIds));
}
if (inclusionFilter == null) {
searchQueryBuilder.withFields(QueryConcept.Fields.CONCEPT_ID);
}
if (pageRequest != null && inclusionFilter == null) {
// Fetch a page of IDs
if (Sort.unsorted().equals(pageRequest.getSort()) || pageRequest.getSort().getOrderFor(QueryConcept.Fields.CONCEPT_ID) == null) {
// Check the page request isn't a SearchAfter because we explicitly use page number here
if (pageRequest instanceof SearchAfterPageRequest) {
throw new NotImplementedException("searchAfter in ConceptSelector must be accompanied with a sort order");
}
searchQueryBuilder.withSort(getDefaultSortForQueryConcept());
searchQueryBuilder.withPageable(PageRequest.of(pageRequest.getPageNumber(), pageRequest.getPageSize()));
} else {
searchQueryBuilder.withPageable(pageRequest);
}
Page<QueryConcept> queryConcepts = queryService.queryForPage(searchQueryBuilder.build());
List<Long> ids = queryConcepts.getContent().stream().map(QueryConcept::getConceptIdL).collect(toList());
return new PageImpl<>(ids, pageRequest, queryConcepts.getTotalElements());
} else {
// Fetch all IDs
searchQueryBuilder.withPageable(LARGE_PAGE);
List<Long> ids = new LongArrayList();
try (SearchHitsIterator<QueryConcept> stream = queryService.streamQueryResults(searchQueryBuilder.build())) {
stream.forEachRemaining(hit -> {
if (inclusionFilter == null || inclusionFilter.apply(hit.getContent())) {
ids.add(hit.getContent().getConceptIdL());
}
});
}
// Stream search doesn't sort for us
ids.sort(LongComparators.OPPOSITE_COMPARATOR);
return getPage(pageRequest, ids);
}
}
use of org.snomed.snowstorm.core.data.domain.QueryConcept in project snowstorm by IHTSDO.
the class SemanticIndexService method findConceptReferences.
public MapPage<Long, Set<Long>> findConceptReferences(String branch, Long conceptId, boolean stated, PageRequest pageRequest) {
Map<Long, Set<Long>> referenceTypeToConceptMap = new HashMap<>();
BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(branch);
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteria.getEntityBranchCriteria(QueryConcept.class)).must(termQuery(QueryConcept.Fields.STATED, stated)).must(// New bool query where at least one should must match
boolQuery().should(termQuery(QueryConcept.Fields.PARENTS, conceptId)).should(termQuery(QueryConcept.Fields.ATTR + "." + QueryConcept.ATTR_TYPE_WILDCARD, conceptId)))).withPageable(pageRequest);
String conceptIdString = conceptId.toString();
SearchHits<QueryConcept> queryConcepts = elasticsearchTemplate.search(queryBuilder.build(), QueryConcept.class);
for (SearchHit<QueryConcept> hit : queryConcepts.getSearchHits()) {
if (hit.getContent().getAncestors().contains(conceptId)) {
referenceTypeToConceptMap.computeIfAbsent(Concepts.IS_A_LONG, id -> new LongOpenHashSet()).add(hit.getContent().getConceptIdL());
} else {
Map<String, Set<Object>> attributes = hit.getContent().getAttr();
for (String attributeId : attributes.keySet()) {
if (attributeId.equals(QueryConcept.ATTR_TYPE_WILDCARD)) {
continue;
}
if (attributes.get(attributeId).contains(conceptIdString)) {
referenceTypeToConceptMap.computeIfAbsent(parseLong(attributeId), id -> new LongOpenHashSet()).add(hit.getContent().getConceptIdL());
}
}
}
}
return new MapPage<>(referenceTypeToConceptMap, pageRequest, queryConcepts.getTotalHits());
}
Aggregations