Search in sources :

Example 1 with QueryConcept

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);
}
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 2 with QueryConcept

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);
        }
    }
}
Also used : QueryConcept(org.snomed.snowstorm.core.data.domain.QueryConcept) ReferenceSetMember(org.snomed.snowstorm.core.data.domain.ReferenceSetMember) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) MemberSearchRequest(org.snomed.snowstorm.core.data.services.pojo.MemberSearchRequest)

Example 3 with QueryConcept

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);
    }
}
Also used : QueryConcept(org.snomed.snowstorm.core.data.domain.QueryConcept) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) NotImplementedException(org.apache.commons.lang3.NotImplementedException) SearchAfterPageRequest(org.springframework.data.elasticsearch.core.SearchAfterPageRequest) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)

Example 4 with QueryConcept

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());
}
Also used : VersionControlHelper(io.kaicode.elasticvc.api.VersionControlHelper) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) HashMap(java.util.HashMap) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) MapPage(org.snomed.snowstorm.core.data.services.pojo.MapPage) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) QueryConcept(org.snomed.snowstorm.core.data.domain.QueryConcept) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) Service(org.springframework.stereotype.Service) ElasticsearchOperations(org.springframework.data.elasticsearch.core.ElasticsearchOperations) Map(java.util.Map) Concepts(org.snomed.snowstorm.core.data.domain.Concepts) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) Long.parseLong(java.lang.Long.parseLong) SearchHit(org.springframework.data.elasticsearch.core.SearchHit) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) Set(java.util.Set) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) QueryConcept(org.snomed.snowstorm.core.data.domain.QueryConcept) HashMap(java.util.HashMap) Long.parseLong(java.lang.Long.parseLong) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) MapPage(org.snomed.snowstorm.core.data.services.pojo.MapPage)

Aggregations

QueryConcept (org.snomed.snowstorm.core.data.domain.QueryConcept)4 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)4 BranchCriteria (io.kaicode.elasticvc.api.BranchCriteria)2 Long.parseLong (java.lang.Long.parseLong)2 VersionControlHelper (io.kaicode.elasticvc.api.VersionControlHelper)1 LongArrayList (it.unimi.dsi.fastutil.longs.LongArrayList)1 LongOpenHashSet (it.unimi.dsi.fastutil.longs.LongOpenHashSet)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)1 ConceptMini (org.snomed.snowstorm.core.data.domain.ConceptMini)1 Concepts (org.snomed.snowstorm.core.data.domain.Concepts)1 ReferenceSetMember (org.snomed.snowstorm.core.data.domain.ReferenceSetMember)1 MapPage (org.snomed.snowstorm.core.data.services.pojo.MapPage)1 MemberSearchRequest (org.snomed.snowstorm.core.data.services.pojo.MemberSearchRequest)1 TimerUtil (org.snomed.snowstorm.core.util.TimerUtil)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 PageRequest (org.springframework.data.domain.PageRequest)1