use of io.kaicode.elasticvc.api.BranchCriteria in project snowstorm by IHTSDO.
the class DupeLangRefsetFixService method fixConcepts.
public void fixConcepts(String path, Set<Long> conceptIds) {
try (final Commit commit = branchService.openCommit(path)) {
final BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(path);
final Collection<Concept> concepts = conceptService.find(branchCriteria, path, conceptIds, Config.DEFAULT_LANGUAGE_DIALECTS);
List<ReferenceSetMember> membersToSave = new ArrayList<>();
for (Concept concept : concepts) {
for (Description description : concept.getDescriptions()) {
for (Map.Entry<String, Set<ReferenceSetMember>> langRefsetEntry : description.getLangRefsetMembersMap().entrySet()) {
if (langRefsetEntry.getValue().size() > 1) {
List<ReferenceSetMember> langRefsets = new ArrayList<>(langRefsetEntry.getValue());
langRefsets.sort(Comparator.comparing(ReferenceSetMember::isReleased).thenComparing(ReferenceSetMember::isActive).reversed());
for (int i = 0; i < langRefsets.size(); i++) {
boolean active = i == 0;
final ReferenceSetMember member = langRefsets.get(i);
if (member.isActive() != active) {
member.setActive(active);
member.markChanged();
membersToSave.add(member);
}
}
}
}
}
}
if (!membersToSave.isEmpty()) {
logger.info("Saving {} corrected language refset members.", membersToSave.size());
referenceSetMemberService.doSaveBatchMembers(membersToSave, commit);
commit.markSuccessful();
} else {
logger.info("No language refset members found to correct.");
}
}
}
use of io.kaicode.elasticvc.api.BranchCriteria in project snowstorm by IHTSDO.
the class MRCMLoader method loadActiveMRCMFromCache.
/**
* Retrieve the latest MRCM for the given branch. If the MRCM has been read
* for the given branch, then the data is read from an internal cache. The cache will
* subsequently be cleared whenever the MRCM is updated.
*
* @param branchPath The branch to read MRCM data from.
* @return The MRCM for the given branch.
* @throws ServiceException When there is an issue reading MRCM.
*/
public MRCM loadActiveMRCMFromCache(String branchPath) throws ServiceException {
LOGGER.debug("Checking cache for MRCM.");
final MRCM cachedMRCM = cache.get(branchPath);
if (cachedMRCM != null) {
LOGGER.debug("MRCM present in cache.");
return cachedMRCM;
}
LOGGER.debug("MRCM not present in cache; loading MRCM.");
final BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(branchPath);
final TimerUtil timer = new TimerUtil("MRCM");
final List<Domain> domains = getDomains(branchPath, branchCriteria, timer);
final List<AttributeDomain> attributeDomains = getAttributeDomains(branchPath, branchCriteria, timer);
final List<AttributeRange> attributeRanges = getAttributeRanges(branchPath, branchCriteria, timer);
final MRCM mrcm = new MRCM(domains, attributeDomains, attributeRanges);
cache.putIfAbsent(branchPath, mrcm);
return mrcm;
}
use of io.kaicode.elasticvc.api.BranchCriteria 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 io.kaicode.elasticvc.api.BranchCriteria in project snowstorm by IHTSDO.
the class MRCMService method retrieveAttributeValues.
public Collection<ConceptMini> retrieveAttributeValues(ContentType contentType, String attributeId, String termPrefix, String branchPath, List<LanguageDialect> languageDialects) throws ServiceException {
BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(branchPath);
MRCM branchMRCM = mrcmLoader.loadActiveMRCM(branchPath, branchCriteria);
return retrieveAttributeValues(contentType, attributeId, termPrefix, branchPath, languageDialects, branchMRCM);
}
use of io.kaicode.elasticvc.api.BranchCriteria in project snowstorm by IHTSDO.
the class ExtensionAdditionalLanguageRefsetUpgradeService method addOrUpdateLanguageRefsetComponents.
private List<ReferenceSetMember> addOrUpdateLanguageRefsetComponents(String branchPath, AdditionalRefsetExecutionConfig config, Map<Long, ReferenceSetMember> existing) {
BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(branchPath);
List<ReferenceSetMember> result = new ArrayList<>();
// batch here as the max allowed in terms query is 65536
for (List<Long> batch : partition(existing.keySet(), 10_000)) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteria.getEntityBranchCriteria(ReferenceSetMember.class)).must(termQuery(REFSET_ID, config.getDefaultEnglishLanguageRefsetId()))).withFilter(termsQuery(REFERENCED_COMPONENT_ID, batch)).withPageable(LARGE_PAGE);
try (final SearchHitsIterator<ReferenceSetMember> componentsToUpdate = elasticsearchTemplate.searchForStream(queryBuilder.build(), ReferenceSetMember.class)) {
componentsToUpdate.forEachRemaining(hit -> update(hit.getContent(), existing, result));
}
}
Set<String> updated = result.stream().map(ReferenceSetMember::getReferencedComponentId).collect(Collectors.toSet());
// add new ones
for (Long referencedComponentId : existing.keySet()) {
if (!updated.contains(referencedComponentId.toString())) {
ReferenceSetMember toAdd = new ReferenceSetMember(UUID.randomUUID().toString(), null, existing.get(referencedComponentId).isActive(), config.getDefaultModuleId(), config.getDefaultEnglishLanguageRefsetId(), existing.get(referencedComponentId).getReferencedComponentId());
toAdd.setAdditionalField(ACCEPTABILITY_ID, existing.get(referencedComponentId).getAdditionalField(ACCEPTABILITY_ID));
result.add(toAdd);
}
}
return result;
}
Aggregations