Search in sources :

Example 1 with BranchCriteria

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.");
        }
    }
}
Also used : Concept(org.snomed.snowstorm.core.data.domain.Concept) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) Description(org.snomed.snowstorm.core.data.domain.Description) Commit(io.kaicode.elasticvc.domain.Commit) ReferenceSetMember(org.snomed.snowstorm.core.data.domain.ReferenceSetMember)

Example 2 with BranchCriteria

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;
}
Also used : BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) TimerUtil(org.snomed.snowstorm.core.util.TimerUtil)

Example 3 with BranchCriteria

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);
}
Also used : BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) QueryConcept(org.snomed.snowstorm.core.data.domain.QueryConcept) ConceptMini(org.snomed.snowstorm.core.data.domain.ConceptMini) TimerUtil(org.snomed.snowstorm.core.util.TimerUtil) Long.parseLong(java.lang.Long.parseLong) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)

Example 4 with BranchCriteria

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);
}
Also used : BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria)

Example 5 with BranchCriteria

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;
}
Also used : BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) ReferenceSetMember(org.snomed.snowstorm.core.data.domain.ReferenceSetMember) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)

Aggregations

BranchCriteria (io.kaicode.elasticvc.api.BranchCriteria)59 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)34 Long.parseLong (java.lang.Long.parseLong)22 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)17 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)16 Commit (io.kaicode.elasticvc.domain.Commit)15 LongArrayList (it.unimi.dsi.fastutil.longs.LongArrayList)15 VersionControlHelper (io.kaicode.elasticvc.api.VersionControlHelper)12 Autowired (org.springframework.beans.factory.annotation.Autowired)12 Service (org.springframework.stereotype.Service)12 Collectors (java.util.stream.Collectors)11 org.snomed.snowstorm.core.data.domain (org.snomed.snowstorm.core.data.domain)11 Branch (io.kaicode.elasticvc.domain.Branch)10 java.util (java.util)10 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 PageImpl (org.springframework.data.domain.PageImpl)10 PageRequest (org.springframework.data.domain.PageRequest)10 LongOpenHashSet (it.unimi.dsi.fastutil.longs.LongOpenHashSet)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9