Search in sources :

Example 6 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class FhirValueSetExpandRequest method computeFhirValueSetUsingUrl.

private ValueSet computeFhirValueSetUsingUrl(ServiceProvider context, String urlValue) {
    // only URLs with query parts are supported, every other case is rejected for now
    if (urlValue.contains("#")) {
        return null;
    }
    // extract the non-query part from the URL value
    String baseUrl = urlValue.split("\\?")[0];
    String query = "";
    if (urlValue.contains("?")) {
        query = urlValue.split("\\?")[1];
    }
    // if this is the base URI string, then always append the core module to represent the International Edition properly
    if (Uri.SNOMED_BASE_URI_STRING.equals(baseUrl)) {
        baseUrl = baseUrl.concat("/900000000000207008");
    }
    // try to lookup the CodeSystem using the baseUrl
    CodeSystem codeSystem = FhirRequests.codeSystems().prepareSearch().one().filterByUrl(baseUrl).buildAsync().execute(context).first().map(ResourceResponseEntry.class::cast).map(ResourceResponseEntry::getResponseResource).map(CodeSystem.class::cast).orElse(null);
    // if no CodeSystem stored to use as Value Set source, return NotFound response
    if (codeSystem == null) {
        return null;
    }
    // return the content of the CodeSystem as Value Set
    String id = Hashing.goodFastHash(8).hashString(urlValue, Charsets.UTF_8).toString();
    Builder valueSet = ValueSet.builder(id).url(urlValue).status(PublicationStatus.ACTIVE);
    final Expansion.Builder expansion = Expansion.builder().identifier(id).timestamp(new Date());
    ConceptSearchRequestBuilder req = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(codeSystem.getResourceURI()).filterByActive(request.getActiveOnly()).filterByTerm(request.getFilter()).setLimit(request.getCount() == null ? 10 : request.getCount()).setSearchAfter(request.getAfter()).setPreferredDisplay("FSN").sortBy(!CompareUtils.isEmpty(request.getFilter()) ? SearchIndexResourceRequest.SCORE : SearchResourceRequest.Sort.fieldAsc("id"));
    Compose compose = null;
    // configure query based on fhir_vs query parameter and also build the compose declaration for this implicit Value Set
    if (Strings.isNullOrEmpty(query) || "fhir_vs".equals(query)) {
    // do nothing, search all concepts
    } else if (query.startsWith("fhir_vs=")) {
        String fhirVsValue = query.replace("fhir_vs=", "");
        if (fhirVsValue.startsWith("ecl/")) {
            String ecl = fhirVsValue.replace("ecl/", "");
            req.filterByQuery(ecl);
            // configure Value Set for ECL
            valueSet.name(String.format("%s Concepts matching %s", codeSystem.getName(), ecl)).description(String.format("All SNOMED CT concepts that match the expression constraint %s", ecl));
            // configure compose for ECL
            compose = Compose.builder().addInclude(Include.builder().addFilters(ValueSetFilter.builder().property("constraint").operator(FilterOperator.EQUALS).value(ecl).build()).build()).build();
        } else if (fhirVsValue.startsWith("isa/")) {
            String parent = fhirVsValue.replace("isa/", "");
            req.filterByAncestor(parent);
            // configure Value Set for IS A
            valueSet.name(String.format("%s Concept %s and descendants", codeSystem.getName(), parent)).description(String.format("All SNOMED CT concepts for %s", parent));
            // configure compose for IS A
            compose = Compose.builder().addInclude(Include.builder().system(baseUrl).addFilters(ValueSetFilter.builder().property("concept").operator(FilterOperator.IS_A).value(parent).build()).build()).build();
        } else if (fhirVsValue.startsWith("refset/")) {
            String refsetId = fhirVsValue.replace("refset/", "");
            if (Strings.isNullOrEmpty(refsetId)) {
                // TODO support refset identifier concept search
                return null;
            } else {
                req.filterByQuery("^" + refsetId);
                // configure Value Set for REFSET
                valueSet.name(String.format("%s Reference Set %s", codeSystem.getName(), refsetId)).description(String.format("All SNOMED CT concepts in the reference set %s", refsetId));
                // configure compose for REFSET
                compose = Compose.builder().addInclude(Include.builder().addFilters(ValueSetFilter.builder().property("concept").operator(FilterOperator.IN).value(refsetId).build()).build()).build();
            }
        } else {
            // TODO check against declared filter values in CodeSystem
            return null;
        }
    }
    Concepts concepts = req.buildAsync().execute(context);
    expansion.total(concepts.getTotal()).after(concepts.getSearchAfter());
    for (Concept concept : concepts) {
        expansion.addContains(Contains.builder().code(concept.getId()).system(baseUrl).display(concept.getTerm()).build());
    }
    return valueSet.compose(compose).expansion(expansion.build()).build();
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) ResourceResponseEntry(com.b2international.snowowl.fhir.core.model.ResourceResponseEntry) ConceptSearchRequestBuilder(com.b2international.snowowl.core.request.ConceptSearchRequestBuilder) Builder(com.b2international.snowowl.fhir.core.model.valueset.ValueSet.Builder) ConceptSearchRequestBuilder(com.b2international.snowowl.core.request.ConceptSearchRequestBuilder) CodeSystem(com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem) Expansion(com.b2international.snowowl.fhir.core.model.valueset.expansion.Expansion) Date(java.util.Date)

Example 7 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequestSnomedTest method filterById.

@Test
public void filterById() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().one().filterByCodeSystemUri(CODESYSTEM).filterById(SnomedConstants.Concepts.ROOT_CONCEPT).buildAsync().execute(Services.bus()).getSync();
    assertThat(matches).hasSize(1);
}
Also used : Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 8 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequestSnomedTest method useDefaultDisplay.

@Test
public void useDefaultDisplay() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(CODESYSTEM).filterById(ID).setLocales("en").buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1);
    final Concept concept = matches.first().get();
    assertThat(concept.getTerm()).isEqualTo(PT);
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 9 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSearchRequestSnomedTest method filterByQuery.

@Test
public void filterByQuery() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().setLimit(0).filterByCodeSystemUri(CODESYSTEM).filterByQuery("*").filterByExclusion(SnomedConstants.Concepts.ROOT_CONCEPT).buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1887);
}
Also used : Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 10 with Concepts

use of com.b2international.snowowl.core.domain.Concepts in project snow-owl by b2ihealthcare.

the class ConceptSuggestionRequest method doExecute.

@Override
protected Suggestions doExecute(BranchContext context) throws IOException {
    TermFilter termFilter;
    if (containsKey(TERM)) {
        if (containsKey(MIN_OCCURENCE_COUNT)) {
            termFilter = TermFilter.minTermMatch(getString(TERM), (Integer) get(MIN_OCCURENCE_COUNT)).withIgnoreStopwords();
        } else {
            termFilter = TermFilter.defaultTermMatch(getString(TERM)).withIgnoreStopwords();
        }
    } else {
        // Gather tokens
        final Multiset<String> tokenOccurrences = HashMultiset.create();
        final EnglishStemmer stemmer = new EnglishStemmer();
        // Get the suggestion base set of concepts
        final ConceptSearchRequestBuilder baseRequestBuilder = new ConceptSearchRequestBuilder().filterByCodeSystemUri(context.service(ResourceURI.class)).setLimit(SCROLL_LIMIT).setLocales(locales());
        if (containsKey(QUERY)) {
            baseRequestBuilder.filterByInclusions(getCollection(QUERY, String.class));
        }
        if (containsKey(MUST_NOT_QUERY)) {
            baseRequestBuilder.filterByExclusions(getCollection(MUST_NOT_QUERY, String.class));
        }
        baseRequestBuilder.stream(context).flatMap(Concepts::stream).flatMap(concept -> getAllTerms(concept).stream()).map(term -> term.toLowerCase(Locale.US)).flatMap(lowerCaseTerm -> TOKEN_SPLITTER.splitToList(lowerCaseTerm).stream()).map(token -> stemToken(stemmer, token)).forEach(tokenOccurrences::add);
        topTokens = Multisets.copyHighestCountFirst(tokenOccurrences).elementSet().stream().filter(// skip short tokens
        token -> token.length() > 2).limit(topTokenCount).collect(Collectors.toList());
        int minShouldMatch = containsKey(MIN_OCCURENCE_COUNT) ? (Integer) get(MIN_OCCURENCE_COUNT) : DEFAULT_MIN_OCCURENCE_COUNT;
        termFilter = TermFilter.minTermMatch(topTokens.stream().collect(Collectors.joining(" ")), minShouldMatch);
    }
    /* 
		 * Run a search with the top tokens and minimum number of matches, excluding everything
		 * that was included previously.
		 */
    final Set<String> exclusions = newHashSet();
    exclusions.addAll(getCollection(QUERY, String.class));
    exclusions.addAll(getCollection(MUST_NOT_QUERY, String.class));
    final ConceptSearchRequestBuilder resultRequestBuilder = new ConceptSearchRequestBuilder().filterByCodeSystemUri(context.service(ResourceURI.class)).filterByActive(true).filterByTerm(termFilter).setPreferredDisplay(getString(DISPLAY)).setLimit(limit()).setLocales(locales()).setSearchAfter(searchAfter()).sortBy(sortBy());
    if (!exclusions.isEmpty()) {
        resultRequestBuilder.filterByExclusions(exclusions);
    }
    final Concepts conceptSuggestions = resultRequestBuilder.build().execute(context);
    return new Suggestions(topTokens, conceptSuggestions.getItems(), conceptSuggestions.getSearchAfter(), limit(), conceptSuggestions.getTotal());
}
Also used : Multiset(com.google.common.collect.Multiset) OptionKey(com.b2international.snowowl.core.request.ConceptSearchRequestEvaluator.OptionKey) Set(java.util.Set) IOException(java.io.IOException) Multisets(com.google.common.collect.Multisets) Min(javax.validation.constraints.Min) Collectors(java.util.stream.Collectors) TextConstants(com.b2international.index.compat.TextConstants) List(java.util.List) Concepts(com.b2international.snowowl.core.domain.Concepts) Concept(com.b2international.snowowl.core.domain.Concept) Suggestions(com.b2international.snowowl.core.domain.Suggestions) ImmutableList(com.google.common.collect.ImmutableList) HashMultiset(com.google.common.collect.HashMultiset) Locale(java.util.Locale) EnglishStemmer(org.tartarus.snowball.ext.EnglishStemmer) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Splitter(com.google.common.base.Splitter) ResourceURI(com.b2international.snowowl.core.ResourceURI) Suggestions(com.b2international.snowowl.core.domain.Suggestions) ResourceURI(com.b2international.snowowl.core.ResourceURI) Concepts(com.b2international.snowowl.core.domain.Concepts) EnglishStemmer(org.tartarus.snowball.ext.EnglishStemmer)

Aggregations

Concepts (com.b2international.snowowl.core.domain.Concepts)10 Test (org.junit.Test)5 Concept (com.b2international.snowowl.core.domain.Concept)4 Options (com.b2international.commons.options.Options)2 ResourceURI (com.b2international.snowowl.core.ResourceURI)2 IOException (java.io.IOException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 TextConstants (com.b2international.index.compat.TextConstants)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)1 CodeSystemSearchRequestBuilder (com.b2international.snowowl.core.codesystem.CodeSystemSearchRequestBuilder)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 Suggestions (com.b2international.snowowl.core.domain.Suggestions)1 ConceptSearchRequestBuilder (com.b2international.snowowl.core.request.ConceptSearchRequestBuilder)1 OptionKey (com.b2international.snowowl.core.request.ConceptSearchRequestEvaluator.OptionKey)1 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)1 ResourceResponseEntry (com.b2international.snowowl.fhir.core.model.ResourceResponseEntry)1