use of com.b2international.index.revision.RevisionSearcher in project snow-owl by b2ihealthcare.
the class RevisionIndexReadRequest method execute.
@Override
public B execute(final BranchContext context) {
final String branchPath = context.path();
RevisionIndex index = context.service(RevisionIndex.class);
if (snapshot) {
return index.read(branchPath, searcher -> {
try {
return next(context.inject().bind(RevisionSearcher.class, searcher).build());
} catch (QueryParseException e) {
throw new IllegalQueryParameterException(e.getMessage());
}
});
} else {
return next(context.inject().bind(RevisionSearcher.class, new RevisionSearcher() {
@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
return index.read(branchPath, searcher -> searcher.aggregate(aggregation));
}
@Override
public Searcher searcher() {
return index.read(branchPath, searcher -> searcher.searcher());
}
@Override
public <T> Hits<T> search(Query<T> query) throws IOException {
return index.read(branchPath, searcher -> searcher.search(query));
}
@Override
public <T> Iterable<T> get(Class<T> type, Iterable<String> keys) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, keys));
}
@Override
public <T> T get(Class<T> type, String key) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, key));
}
@Override
public String branch() {
return branchPath;
}
}).build());
}
}
use of com.b2international.index.revision.RevisionSearcher in project snow-owl by b2ihealthcare.
the class Taxonomies method updateTaxonomy.
private static TaxonomyGraphStatus updateTaxonomy(RevisionSearcher searcher, SnomedOWLExpressionConverter expressionConverter, StagingArea staging, TaxonomyGraph graphToUpdate, String characteristicTypeId) throws IOException {
LOGGER.trace("Processing changes taxonomic information.");
staging.getNewObjects(SnomedRelationshipIndexEntry.class).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(newRelationship -> updateEdge(newRelationship, graphToUpdate));
final Set<String> relationshipsToExcludeFromReactivatedConcepts = newHashSet();
staging.getChangedRevisions(SnomedRelationshipIndexEntry.class).map(diff -> (SnomedRelationshipIndexEntry) diff.newRevision).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(dirtyRelationship -> {
relationshipsToExcludeFromReactivatedConcepts.add(dirtyRelationship.getId());
updateEdge(dirtyRelationship, graphToUpdate);
});
staging.getRemovedObjects(SnomedRelationshipIndexEntry.class).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(relationship -> {
relationshipsToExcludeFromReactivatedConcepts.add(relationship.getId());
graphToUpdate.removeEdge(relationship.getId());
});
if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
staging.getNewObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).forEach(member -> updateEdge(member, graphToUpdate, expressionConverter));
staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).map(diff -> (SnomedRefSetMemberIndexEntry) diff.newRevision).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).forEach(member -> updateEdge(member, graphToUpdate, expressionConverter));
staging.getRemovedObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).map(SnomedRefSetMemberIndexEntry::getId).forEach(graphToUpdate::removeEdge);
}
staging.getNewObjects(SnomedConceptDocument.class).forEach(newConcept -> updateConcept(newConcept, graphToUpdate));
staging.getRemovedObjects(SnomedConceptDocument.class).forEach(concept -> graphToUpdate.removeNode(concept.getId()));
final Set<String> conceptWithPossibleMissingRelationships = newHashSet();
staging.getChangedRevisions(SnomedConceptDocument.class, Collections.singleton(SnomedConceptDocument.Fields.ACTIVE)).forEach(diff -> {
final RevisionPropertyDiff propDiff = diff.getRevisionPropertyDiff(SnomedConceptDocument.Fields.ACTIVE);
final boolean oldValue = Boolean.parseBoolean(propDiff.getOldValue());
final boolean newValue = Boolean.parseBoolean(propDiff.getNewValue());
final String conceptId = diff.newRevision.getId();
if (!oldValue && newValue) {
// make sure the node is part of the new tree
graphToUpdate.addNode(conceptId);
conceptWithPossibleMissingRelationships.add(conceptId);
}
});
if (!conceptWithPossibleMissingRelationships.isEmpty()) {
Hits<String[]> possibleMissingRelationships = searcher.search(Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(Expressions.builder().filter(SnomedRelationshipIndexEntry.Expressions.active()).filter(SnomedRelationshipIndexEntry.Expressions.characteristicTypeId(characteristicTypeId)).filter(SnomedRelationshipIndexEntry.Expressions.typeId(Concepts.IS_A)).filter(SnomedRelationshipIndexEntry.Expressions.sourceIds(conceptWithPossibleMissingRelationships)).mustNot(SnomedRelationshipIndexEntry.Expressions.ids(relationshipsToExcludeFromReactivatedConcepts)).build()).limit(Integer.MAX_VALUE).build());
for (String[] relationship : possibleMissingRelationships) {
graphToUpdate.addNode(relationship[2]);
graphToUpdate.addEdge(relationship[0], Long.parseLong(relationship[1]), new long[] { Long.parseLong(relationship[2]) });
}
}
LOGGER.trace("Rebuilding taxonomic information based on the changes.");
return graphToUpdate.update();
}
use of com.b2international.index.revision.RevisionSearcher in project snow-owl by b2ihealthcare.
the class Taxonomies method getStatements.
private static Collection<Object[]> getStatements(RevisionSearcher searcher, LongCollection conceptIds, String characteristicTypeId, boolean filterByConceptIds) throws IOException {
// merge stated relationships and OWL axiom relationships into a single array
ImmutableList.Builder<Object[]> isaStatementsBuilder = ImmutableList.builder();
final Set<String> concepts = LongSets.toStringSet(conceptIds);
ExpressionBuilder activeIsaRelationshipQuery = Expressions.builder().filter(active()).filter(typeId(Concepts.IS_A)).filter(characteristicTypeId(characteristicTypeId));
if (filterByConceptIds) {
activeIsaRelationshipQuery.filter(sourceIds(concepts)).filter(destinationIds(concepts));
}
final Query<String[]> activeStatedISARelationshipsQuery = Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(activeIsaRelationshipQuery.build()).limit(Integer.MAX_VALUE).build();
Hits<String[]> activeIsaRelationships = searcher.search(activeStatedISARelationshipsQuery);
activeIsaRelationships.forEach(activeIsaRelationship -> {
isaStatementsBuilder.add(new Object[] { activeIsaRelationship[0], Long.parseLong(activeIsaRelationship[1]), new long[] { Long.parseLong(activeIsaRelationship[2]) } });
});
activeIsaRelationships = null;
if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
// search existing axioms defined for the given set of conceptIds
ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active());
if (filterByConceptIds) {
activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(concepts)).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).filter(destinationIds(concepts)).build()));
} else {
activeOwlAxiomMemberQuery.filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).build()));
}
final Query<SnomedRefSetMemberIndexEntry> activeAxiomISARelationshipsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
Hits<SnomedRefSetMemberIndexEntry> activeAxiomISARelationships = searcher.search(activeAxiomISARelationshipsQuery);
activeAxiomISARelationships.forEach(owlMember -> {
if (!CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())) {
// XXX: breaks with a NumberFormatException if any of the IS A relationships has a value
long[] destinationIds = owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> Concepts.IS_A.equals(classAxiom.getTypeId())).map(SnomedOWLRelationshipDocument::getDestinationId).mapToLong(Long::parseLong).toArray();
isaStatementsBuilder.add(new Object[] { owlMember.getId(), Long.parseLong(owlMember.getReferencedComponentId()), destinationIds });
}
});
activeAxiomISARelationships = null;
}
return isaStatementsBuilder.build();
}
use of com.b2international.index.revision.RevisionSearcher in project snow-owl by b2ihealthcare.
the class SnomedValidationIssueDetailExtension method extendIssueDetails.
private void extendIssueDetails(BranchContext context, Collection<ValidationIssue> issues) {
final RevisionSearcher searcher = context.service(RevisionSearcher.class);
final Multimap<String, ValidationIssue> issuesByComponentId = Multimaps.index(issues, issue -> issue.getAffectedComponent().getComponentId());
final Multimap<ComponentCategory, String> issueComponentIdsByComponentCategory = HashMultimap.create();
issues.stream().forEach(issue -> {
final ComponentCategory componentCategory = getComponentCategory(issue.getAffectedComponent().getComponentType());
issueComponentIdsByComponentCategory.put(componentCategory, issue.getAffectedComponent().getComponentId());
});
final Multimap<String, String> issueIdsByConceptIds = HashMultimap.create();
final Set<String> alreadyFetchedConceptIds = Sets.newHashSet();
for (ComponentCategory category : issueComponentIdsByComponentCategory.keySet()) {
final Query<String[]> query = buildQuery(category, issueComponentIdsByComponentCategory.get(category));
query.stream(searcher).forEachOrdered(hits -> {
for (String[] hit : hits) {
String id = hit[0];
String status = hit[1];
String moduleId = hit[2];
issuesByComponentId.get(id).forEach(validationIssue -> {
validationIssue.setDetails(COMPONENT_STATUS, status);
validationIssue.setDetails(COMPONENT_MODULE_ID, moduleId);
if (CONCEPT == category) {
validationIssue.setDetails(CONCEPT_STATUS, status);
validationIssue.setDetails(SnomedDocument.Fields.EFFECTIVE_TIME, Long.parseLong(hit[3]));
alreadyFetchedConceptIds.add(id);
} else if (DESCRIPTION == category || RELATIONSHIP == category || SET_MEMBER == category) {
validationIssue.setDetails(SnomedDocument.Fields.EFFECTIVE_TIME, Long.parseLong(hit[3]));
final String containerConceptId = hit[4];
if (!Strings.isNullOrEmpty(containerConceptId) && (!issueIdsByConceptIds.containsKey(containerConceptId) || !alreadyFetchedConceptIds.contains(containerConceptId))) {
issueIdsByConceptIds.put(containerConceptId, id);
}
// in case of description just add the already fetched term as label to the issue, concepts and relationship will get their
if (DESCRIPTION == category) {
validationIssue.setAffectedComponentLabels(Collections.singletonList(hit[5]));
}
}
});
}
});
}
if (!issueIdsByConceptIds.isEmpty()) {
final Query<String[]> conceptStatusQuery = Query.select(String[].class).from(SnomedConceptDocument.class).fields(SnomedConceptDocument.Fields.ID, SnomedConceptDocument.Fields.ACTIVE).where(SnomedConceptDocument.Expressions.ids(issueIdsByConceptIds.keySet())).limit(SCROLL_SIZE).build();
conceptStatusQuery.stream(searcher).flatMap(Hits::stream).forEachOrdered(hit -> {
Collection<String> issueIds = issueIdsByConceptIds.get(hit[0]);
issueIds.stream().forEach(id -> {
issuesByComponentId.get(id).forEach(validationIssue -> validationIssue.setDetails(CONCEPT_STATUS, hit[1]));
});
});
}
}
use of com.b2international.index.revision.RevisionSearcher in project snow-owl by b2ihealthcare.
the class SnomedValidationIssueDetailExtension method extendRelationshipIssueLabels.
private void extendRelationshipIssueLabels(BranchContext context, Collection<ValidationIssue> issues, Map<String, Object> ruleParameters) {
final RevisionSearcher searcher = context.service(RevisionSearcher.class);
final List<ValidationIssue> relationshipIssues = issues.stream().filter(issue -> SnomedRelationship.TYPE == issue.getAffectedComponent().getComponentType()).collect(Collectors.toList());
if (relationshipIssues.isEmpty()) {
return;
}
final Multimap<String, ValidationIssue> issuesByRelationshipId = Multimaps.index(relationshipIssues, issue -> issue.getAffectedComponent().getComponentId());
final Set<String> conceptsToFetch = newHashSet();
final Map<String, String> relationshipFragmentsByRelationshipId = Maps.newHashMap();
searcher.stream(Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.TYPE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID, SnomedRelationshipIndexEntry.Fields.VALUE_TYPE, SnomedRelationshipIndexEntry.Fields.NUMERIC_VALUE, SnomedRelationshipIndexEntry.Fields.STRING_VALUE).where(SnomedRelationshipIndexEntry.Expressions.ids(issuesByRelationshipId.keySet())).limit(SCROLL_SIZE).build()).forEach(hits -> {
for (String[] hit : hits) {
final String id = hit[0];
final String sourceId = hit[1];
final String typeId = hit[2];
final String destinationId = hit[3];
final String valueType = hit[4];
final String numericValue = hit[5];
final String stringValue = hit[6];
String destination = "";
conceptsToFetch.add(sourceId);
conceptsToFetch.add(typeId);
if (!Strings.isNullOrEmpty(destinationId)) {
conceptsToFetch.add(destinationId);
destination = destinationId;
} else {
if (RelationshipValueType.DECIMAL.name().equals(valueType) || RelationshipValueType.INTEGER.name().equals(valueType)) {
destination = DecimalUtils.decode(numericValue).toString();
} else if (RelationshipValueType.STRING.name().equals(valueType)) {
destination = stringValue;
}
}
relationshipFragmentsByRelationshipId.put(id, String.format("%s|%s|%s", sourceId, typeId, destination));
}
});
Map<String, String> affectedComponentLabelsByConcept = getAffectedComponentLabels(context, ruleParameters, conceptsToFetch);
if (!affectedComponentLabelsByConcept.isEmpty()) {
issuesByRelationshipId.values().forEach(issue -> {
final String[] relationshipFragments = relationshipFragmentsByRelationshipId.get(issue.getAffectedComponent().getComponentId()).split("[|]");
final String sourceId = relationshipFragments[0];
final String typeId = relationshipFragments[1];
final String destinationIdOrValue = relationshipFragments[2];
final String sourceTerm = affectedComponentLabelsByConcept.getOrDefault(sourceId, sourceId);
final String typeTerm = affectedComponentLabelsByConcept.getOrDefault(typeId, typeId);
final String destinationTerm = affectedComponentLabelsByConcept.getOrDefault(destinationIdOrValue, destinationIdOrValue);
issue.setAffectedComponentLabels(ImmutableList.of(String.format("%s - %s - %s", sourceTerm, typeTerm, destinationTerm)));
});
}
}
Aggregations