use of org.springframework.data.elasticsearch.core.SearchHitsIterator in project snowstorm by IHTSDO.
the class AdminOperationsService method reduceVersionsReplaced.
public Map<Class, AtomicLong> reduceVersionsReplaced(String branch) {
// For all entries in the versionsReplaced map check if the document is from a child branch. If so remove from the set.
Branch latest = branchService.findBranchOrThrow(branch);
Map<Class, AtomicLong> reducedByType = new HashMap<>();
Map<String, Set<String>> versionsReplaced = latest.getVersionsReplaced();
final Map<Class<? extends DomainEntity>, ElasticsearchRepository> componentTypeRepoMap = domainEntityConfiguration.getAllTypeRepositoryMap();
for (Class<? extends DomainEntity> type : componentTypeRepoMap.keySet()) {
Set<String> toRemove = new HashSet<>();
Set<String> versionsReplacedForType = versionsReplaced.getOrDefault(type.getSimpleName(), Collections.emptySet());
for (List<String> versionsReplacedSegment : Iterables.partition(versionsReplacedForType, 1_000)) {
try (final SearchHitsIterator<? extends DomainEntity> entitiesReplaced = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(boolQuery().must(prefixQuery("path", branch + "/")).must(termsQuery("_id", versionsReplacedSegment))).withPageable(ConceptService.LARGE_PAGE).build(), type)) {
entitiesReplaced.forEachRemaining(entity -> toRemove.add(entity.getId()));
}
}
if (!toRemove.isEmpty()) {
versionsReplacedForType.removeAll(toRemove);
reducedByType.computeIfAbsent(type, (t) -> new AtomicLong(0)).addAndGet(toRemove.size());
}
}
latest.setVersionsReplaced(versionsReplaced);
branchRepository.save(latest);
return reducedByType;
}
use of org.springframework.data.elasticsearch.core.SearchHitsIterator in project snowstorm by IHTSDO.
the class DescriptionService method findDescriptionsWithAggregations.
public PageWithBucketAggregations<Description> findDescriptionsWithAggregations(String path, DescriptionCriteria criteria, PageRequest pageRequest) throws TooCostlyException {
TimerUtil timer = new TimerUtil("Search", Level.INFO, 5, new TimerUtil("Search DEBUG", Level.DEBUG));
final BranchCriteria branchCriteria = versionControlHelper.getBranchCriteria(path);
timer.checkpoint("Build branch criteria");
// Fetch all matching description and concept ids
// ids of concepts where all descriptions and concept criteria are met
DescriptionMatches descriptionMatches = findDescriptionAndConceptIds(criteria, Collections.EMPTY_SET, branchCriteria, timer);
BoolQueryBuilder descriptionQuery = descriptionMatches.getDescriptionQuery();
// Apply concept and acceptability filtering for final search
BoolQueryBuilder descriptionFilter = boolQuery();
descriptionFilter.must(termsQuery(Description.Fields.DESCRIPTION_ID, descriptionMatches.getMatchedDescriptionIds()));
// Start fetching aggregations..
List<Aggregation> allAggregations = new ArrayList<>();
Set<Long> conceptIds = descriptionMatches.getMatchedConceptIds();
// Fetch FSN semantic tag aggregation
BoolQueryBuilder fsnClauses = boolQuery();
String semanticTag = criteria.getSemanticTag();
Set<String> semanticTags = criteria.getSemanticTags();
boolean semanticTagFiltering = !Strings.isNullOrEmpty(semanticTag) || !CollectionUtils.isEmpty(semanticTags);
Set<String> allSemanticTags = new HashSet<>();
if (semanticTagFiltering) {
if (!Strings.isNullOrEmpty(semanticTag)) {
allSemanticTags.add(semanticTag);
}
if (!CollectionUtils.isEmpty(semanticTags)) {
allSemanticTags.addAll(semanticTags);
}
fsnClauses.must(termsQuery(Description.Fields.TAG, allSemanticTags));
}
NativeSearchQueryBuilder fsnQueryBuilder = new NativeSearchQueryBuilder().withQuery(fsnClauses.must(branchCriteria.getEntityBranchCriteria(Description.class)).must(termsQuery(Description.Fields.ACTIVE, true)).must(termsQuery(Description.Fields.TYPE_ID, Concepts.FSN)).must(termsQuery(Description.Fields.CONCEPT_ID, conceptIds))).addAggregation(AggregationBuilders.terms("semanticTags").field(Description.Fields.TAG).size(AGGREGATION_SEARCH_SIZE));
if (!semanticTagFiltering) {
fsnQueryBuilder.withPageable(PAGE_OF_ONE);
SearchHits<Description> semanticTagResults = elasticsearchTemplate.search(fsnQueryBuilder.build(), Description.class);
allAggregations.add(semanticTagResults.getAggregations().get("semanticTags"));
timer.checkpoint("Semantic tag aggregation");
} else {
// Apply semantic tag filter
fsnQueryBuilder.withPageable(LARGE_PAGE).withFields(Description.Fields.CONCEPT_ID);
Set<Long> conceptSemanticTagMatches = new LongOpenHashSet();
if (allSemanticTags.size() == 1) {
try (SearchHitsIterator<Description> descriptionStream = elasticsearchTemplate.searchForStream(fsnQueryBuilder.build(), Description.class)) {
descriptionStream.forEachRemaining(hit -> conceptSemanticTagMatches.add(parseLong(hit.getContent().getConceptId())));
}
allAggregations.add(new SimpleAggregation("semanticTags", allSemanticTags.iterator().next(), conceptSemanticTagMatches.size()));
} else {
SearchHits<Description> semanticTagResults = elasticsearchTemplate.search(fsnQueryBuilder.build(), Description.class);
semanticTagResults.stream().forEach((hit -> conceptSemanticTagMatches.add(parseLong(hit.getContent().getConceptId()))));
allAggregations.add(semanticTagResults.getAggregations().get("semanticTags"));
}
conceptIds = conceptSemanticTagMatches;
}
// Fetch concept refset membership aggregation
SearchHits<ReferenceSetMember> membershipResults = elasticsearchTemplate.search(new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteria.getEntityBranchCriteria(ReferenceSetMember.class)).must(termsQuery(ReferenceSetMember.Fields.ACTIVE, true)).filter(termsQuery(ReferenceSetMember.Fields.REFERENCED_COMPONENT_ID, conceptIds))).withPageable(PAGE_OF_ONE).addAggregation(AggregationBuilders.terms("membership").field(ReferenceSetMember.Fields.REFSET_ID)).build(), ReferenceSetMember.class);
allAggregations.add(membershipResults.getAggregations().get("membership"));
timer.checkpoint("Concept refset membership aggregation");
// Perform final paged description search with description property aggregations
descriptionFilter.must(termsQuery(Description.Fields.CONCEPT_ID, conceptIds));
final NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withQuery(descriptionQuery.filter(descriptionFilter)).addAggregation(AggregationBuilders.terms("module").field(Description.Fields.MODULE_ID)).addAggregation(AggregationBuilders.terms("language").field(Description.Fields.LANGUAGE_CODE)).withPageable(pageRequest);
NativeSearchQuery aggregateQuery = addTermSort(queryBuilder.build());
aggregateQuery.setTrackTotalHits(true);
SearchHits<Description> descriptions = elasticsearchTemplate.search(aggregateQuery, Description.class);
allAggregations.addAll(descriptions.getAggregations().asList());
timer.checkpoint("Fetch descriptions including module and language aggregations");
timer.finish();
// Merge aggregations
return PageWithBucketAggregationsFactory.createPage(descriptions, new Aggregations(allAggregations), pageRequest);
}
use of org.springframework.data.elasticsearch.core.SearchHitsIterator in project snowstorm by IHTSDO.
the class TraceabilityLogService method filterRefsetMembersAndLookupComponentConceptIds.
private Map<Long, List<ReferenceSetMember>> filterRefsetMembersAndLookupComponentConceptIds(Iterable<ReferenceSetMember> persistedReferenceSetMembers, Commit commit, Map<Long, Long> componentToConceptIdMap) {
Map<Long, List<ReferenceSetMember>> conceptToMembersMap = new Long2ObjectArrayMap<>();
List<ReferenceSetMember> membersToLog = new ArrayList<>();
Set<Long> referencedDescriptions = new LongOpenHashSet();
Set<Long> referencedRelationships = new LongOpenHashSet();
for (ReferenceSetMember refsetMember : persistedReferenceSetMembers) {
String conceptId = refsetMember.getConceptId();
if (conceptId != null) {
conceptToMembersMap.computeIfAbsent(parseLong(conceptId), id -> new ArrayList<>()).add(refsetMember);
} else {
final String referencedComponentId = refsetMember.getReferencedComponentId();
if (IdentifierService.isConceptId(referencedComponentId)) {
conceptToMembersMap.computeIfAbsent(Long.parseLong(referencedComponentId), id -> new ArrayList<>()).add(refsetMember);
} else {
membersToLog.add(refsetMember);
if (IdentifierService.isDescriptionId(referencedComponentId)) {
referencedDescriptions.add(parseLong(referencedComponentId));
} else if (IdentifierService.isRelationshipId(referencedComponentId)) {
referencedRelationships.add(parseLong(referencedComponentId));
}
}
}
}
final Set<Long> descriptionIdsToLookup = referencedDescriptions.stream().filter(Predicate.not(componentToConceptIdMap::containsKey)).collect(Collectors.toSet());
final Set<Long> relationshipIdsToLookup = referencedRelationships.stream().filter(Predicate.not(componentToConceptIdMap::containsKey)).collect(Collectors.toSet());
BranchCriteria branchCriteria = null;
if (!descriptionIdsToLookup.isEmpty()) {
branchCriteria = versionControlHelper.getBranchCriteria(commit.getBranch());
for (List<Long> descriptionIdsSegment : Iterables.partition(descriptionIdsToLookup, CLAUSE_LIMIT)) {
try (final SearchHitsIterator<Description> stream = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(branchCriteria.getEntityBranchCriteria(Description.class).must(termsQuery(Description.Fields.DESCRIPTION_ID, descriptionIdsSegment))).withFields(Description.Fields.DESCRIPTION_ID, Description.Fields.CONCEPT_ID).withPageable(LARGE_PAGE).build(), Description.class)) {
stream.forEachRemaining(hit -> {
final Description description = hit.getContent();
componentToConceptIdMap.put(parseLong(description.getDescriptionId()), parseLong(description.getConceptId()));
});
}
}
}
if (!relationshipIdsToLookup.isEmpty()) {
if (branchCriteria == null) {
branchCriteria = versionControlHelper.getBranchCriteria(commit.getBranch());
}
for (List<Long> relationshipsIdsSegment : Iterables.partition(relationshipIdsToLookup, CLAUSE_LIMIT)) {
try (final SearchHitsIterator<Relationship> stream = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(branchCriteria.getEntityBranchCriteria(Relationship.class).must(termsQuery(Relationship.Fields.RELATIONSHIP_ID, relationshipsIdsSegment))).withSourceFilter(new FetchSourceFilter(new String[] { Relationship.Fields.RELATIONSHIP_ID, Relationship.Fields.SOURCE_ID }, new String[] {})).withPageable(LARGE_PAGE).build(), Relationship.class)) {
stream.forEachRemaining(hit -> {
final Relationship relationship = hit.getContent();
componentToConceptIdMap.put(parseLong(relationship.getRelationshipId()), parseLong(relationship.getSourceId()));
});
}
}
}
membersToLog.forEach(refsetMember -> {
final String referencedComponentId = refsetMember.getReferencedComponentId();
final Long conceptId = componentToConceptIdMap.get(parseLong(referencedComponentId));
if (conceptId != null) {
conceptToMembersMap.computeIfAbsent(conceptId, id -> new ArrayList<>()).add(refsetMember);
} else {
logger.error("Refset member {} with referenced component {} can not be mapped to a concept id for traceability on branch {}", refsetMember.getId(), refsetMember.getReferencedComponentId(), commit.getBranch().getPath());
}
});
return conceptToMembersMap;
}
use of org.springframework.data.elasticsearch.core.SearchHitsIterator in project snowstorm by IHTSDO.
the class ImportComponentFactoryImpl method processEntities.
/*
- Mark as changed for version control.
- Remove if earlier or equal effectiveTime to existing.
- Copy release fields from existing.
*/
private <T extends SnomedComponent> void processEntities(Collection<T> components, Integer patchReleaseVersion, ElasticsearchOperations elasticsearchTemplate, Class<T> componentClass, boolean copyReleaseFields, boolean clearEffectiveTimes) {
Map<Integer, List<T>> effectiveDateMap = new HashMap<>();
components.forEach(component -> {
component.setChanged(true);
if (clearEffectiveTimes) {
component.setEffectiveTimeI(null);
component.setReleased(false);
component.setReleaseHash(null);
component.setReleasedEffectiveTime(null);
}
Integer effectiveTimeI = component.getEffectiveTimeI();
if (effectiveTimeI != null) {
effectiveDateMap.computeIfAbsent(effectiveTimeI, i -> new ArrayList<>()).add(component);
maxEffectiveTimeCollector.add(effectiveTimeI);
}
});
// patchReleaseVersion=-1 is a special case which allows replacing any effectiveTime
if (patchReleaseVersion == null || !patchReleaseVersion.equals(-1)) {
for (Integer effectiveTime : new TreeSet<>(effectiveDateMap.keySet())) {
// Find component states with an equal or greater effective time
boolean replacementOfThisEffectiveTimeAllowed = patchReleaseVersion != null && patchReleaseVersion.equals(effectiveTime);
List<T> componentsAtDate = effectiveDateMap.get(effectiveTime);
String idField = componentsAtDate.get(0).getIdField();
AtomicInteger alreadyExistingComponentCount = new AtomicInteger();
try (SearchHitsIterator<T> componentsWithSameOrLaterEffectiveTime = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteriaBeforeOpenCommit.getEntityBranchCriteria(componentClass)).must(termsQuery(idField, componentsAtDate.stream().map(T::getId).collect(Collectors.toList()))).must(replacementOfThisEffectiveTimeAllowed ? rangeQuery(SnomedComponent.Fields.EFFECTIVE_TIME).gt(effectiveTime) : rangeQuery(SnomedComponent.Fields.EFFECTIVE_TIME).gte(effectiveTime))).withFields(// Only fetch the id
idField).withPageable(LARGE_PAGE).build(), componentClass)) {
componentsWithSameOrLaterEffectiveTime.forEachRemaining(hit -> {
// Skip component import
// Compared by id only
components.remove(hit.getContent());
alreadyExistingComponentCount.incrementAndGet();
});
}
componentTypeSkippedMap.computeIfAbsent(componentClass.getSimpleName(), key -> new AtomicLong()).addAndGet(alreadyExistingComponentCount.get());
}
}
if (copyReleaseFields) {
Map<String, T> idToUnreleasedComponentMap = components.stream().filter(component -> component.getEffectiveTime() == null).collect(Collectors.toMap(T::getId, Function.identity()));
if (!idToUnreleasedComponentMap.isEmpty()) {
String idField = idToUnreleasedComponentMap.values().iterator().next().getIdField();
try (SearchHitsIterator<T> stream = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(boolQuery().must(branchCriteriaBeforeOpenCommit.getEntityBranchCriteria(componentClass)).must(termQuery(SnomedComponent.Fields.RELEASED, true)).filter(termsQuery(idField, idToUnreleasedComponentMap.keySet()))).withPageable(LARGE_PAGE).build(), componentClass)) {
stream.forEachRemaining(hit -> {
T t = idToUnreleasedComponentMap.get(hit.getContent().getId());
// noinspection unchecked
t.copyReleaseDetails(hit.getContent());
t.updateEffectiveTime();
});
}
}
}
}
use of org.springframework.data.elasticsearch.core.SearchHitsIterator in project snowstorm by IHTSDO.
the class BranchReviewService method createConceptChangeReportOnBranchForTimeRange.
Set<Long> createConceptChangeReportOnBranchForTimeRange(String path, Date start, Date end, boolean sourceIsParent) {
logger.info("Creating change report: branch {} time range {} ({}) to {} ({})", path, start.getTime(), start, end.getTime(), end);
List<Branch> startTimeSlice;
List<Branch> endTimeSlice;
if (sourceIsParent) {
// The source branch is the parent, so we are counting content which could be rebased down.
// This content can come from any ancestor branch.
startTimeSlice = versionControlHelper.getTimeSlice(path, start);
endTimeSlice = versionControlHelper.getTimeSlice(path, end);
} else {
// The source branch is the child, so we are counting content which could be promoted up.
// This content will exist on this path only.
startTimeSlice = Lists.newArrayList(branchService.findAtTimepointOrThrow(path, start));
endTimeSlice = Lists.newArrayList(branchService.findAtTimepointOrThrow(path, end));
}
if (startTimeSlice.equals(endTimeSlice)) {
return Collections.emptySet();
}
// Find components of each type that are on the target branch and have been ended on the source branch
final Set<Long> changedConcepts = new LongOpenHashSet();
final Map<Long, Long> referenceComponentIdToConceptMap = new Long2ObjectOpenHashMap<>();
final Set<Long> preferredDescriptionIds = new LongOpenHashSet();
Branch branch = branchService.findBranchOrThrow(path);
logger.debug("Collecting versions replaced for change report: branch {} time range {} to {}", path, start, end);
Map<String, Set<String>> changedVersionsReplaced = new HashMap<>();
// Technique: Search for replaced versions
// Work out changes in versions replaced between time slices
Map<String, Set<String>> startVersionsReplaced = versionControlHelper.getAllVersionsReplaced(startTimeSlice);
Map<String, Set<String>> endVersionsReplaced = versionControlHelper.getAllVersionsReplaced(endTimeSlice);
for (String type : Sets.union(startVersionsReplaced.keySet(), endVersionsReplaced.keySet())) {
changedVersionsReplaced.put(type, Sets.difference(endVersionsReplaced.getOrDefault(type, Collections.emptySet()), startVersionsReplaced.getOrDefault(type, Collections.emptySet())));
}
if (!changedVersionsReplaced.getOrDefault(Concept.class.getSimpleName(), Collections.emptySet()).isEmpty()) {
try (final SearchHitsIterator<Concept> stream = elasticsearchTemplate.searchForStream(componentsReplacedCriteria(changedVersionsReplaced.get(Concept.class.getSimpleName()), Concept.Fields.CONCEPT_ID).build(), Concept.class)) {
stream.forEachRemaining(hit -> changedConcepts.add(parseLong(hit.getContent().getConceptId())));
}
}
if (!changedVersionsReplaced.getOrDefault(Description.class.getSimpleName(), Collections.emptySet()).isEmpty()) {
NativeSearchQueryBuilder fsnQuery = componentsReplacedCriteria(changedVersionsReplaced.get(Description.class.getSimpleName()), Description.Fields.CONCEPT_ID).withFilter(termQuery(Description.Fields.TYPE_ID, Concepts.FSN));
try (final SearchHitsIterator<Description> stream = elasticsearchTemplate.searchForStream(fsnQuery.build(), Description.class)) {
stream.forEachRemaining(hit -> changedConcepts.add(parseLong(hit.getContent().getConceptId())));
}
}
if (!changedVersionsReplaced.getOrDefault(Relationship.class.getSimpleName(), Collections.emptySet()).isEmpty()) {
try (final SearchHitsIterator<Relationship> stream = elasticsearchTemplate.searchForStream(componentsReplacedCriteria(changedVersionsReplaced.get(Relationship.class.getSimpleName()), Relationship.Fields.SOURCE_ID).build(), Relationship.class)) {
stream.forEachRemaining(hit -> changedConcepts.add(parseLong(hit.getContent().getSourceId())));
}
}
if (!changedVersionsReplaced.getOrDefault(ReferenceSetMember.class.getSimpleName(), Collections.emptySet()).isEmpty()) {
// Refsets with the internal "conceptId" field are related to a concept in terms of authoring
NativeSearchQueryBuilder refsetQuery = componentsReplacedCriteria(changedVersionsReplaced.get(ReferenceSetMember.class.getSimpleName()), ReferenceSetMember.Fields.REFERENCED_COMPONENT_ID, ReferenceSetMember.Fields.CONCEPT_ID).withFilter(boolQuery().must(existsQuery(ReferenceSetMember.Fields.CONCEPT_ID)));
try (final SearchHitsIterator<ReferenceSetMember> stream = elasticsearchTemplate.searchForStream(refsetQuery.build(), ReferenceSetMember.class)) {
stream.forEachRemaining(hit -> referenceComponentIdToConceptMap.put(parseLong(hit.getContent().getReferencedComponentId()), parseLong(hit.getContent().getConceptId())));
}
}
// Technique: Search for ended versions
BoolQueryBuilder updatesDuringRange;
if (sourceIsParent) {
updatesDuringRange = versionControlHelper.getUpdatesOnBranchOrAncestorsDuringRangeQuery(path, start, end);
} else {
updatesDuringRange = versionControlHelper.getUpdatesOnBranchDuringRangeCriteria(path, start, end);
}
// Find new or ended versions of each component type and collect the conceptId they relate to
logger.debug("Collecting concept changes for change report: branch {} time range {} to {}", path, start, end);
TimerUtil timerUtil = new TimerUtil("Collecting changes");
NativeSearchQuery conceptsWithNewVersionsQuery = new NativeSearchQueryBuilder().withQuery(updatesDuringRange).withPageable(LARGE_PAGE).withSort(SortBuilders.fieldSort("start")).withFields(Concept.Fields.CONCEPT_ID).build();
try (final SearchHitsIterator<Concept> stream = elasticsearchTemplate.searchForStream(conceptsWithNewVersionsQuery, Concept.class)) {
stream.forEachRemaining(hit -> changedConcepts.add(parseLong(hit.getContent().getConceptId())));
}
logger.debug("Collecting description changes for change report: branch {} time range {} to {}", path, start, end);
AtomicLong descriptions = new AtomicLong();
NativeSearchQuery descQuery = newSearchQuery(updatesDuringRange).withFilter(termQuery(Description.Fields.TYPE_ID, Concepts.FSN)).withFields(Description.Fields.CONCEPT_ID).build();
try (final SearchHitsIterator<Description> stream = elasticsearchTemplate.searchForStream(descQuery, Description.class)) {
stream.forEachRemaining(hit -> {
changedConcepts.add(parseLong(hit.getContent().getConceptId()));
descriptions.incrementAndGet();
});
}
timerUtil.checkpoint("descriptions " + descriptions.get());
logger.debug("Collecting relationship changes for change report: branch {} time range {} to {}", path, start, end);
AtomicLong relationships = new AtomicLong();
NativeSearchQuery relQuery = newSearchQuery(updatesDuringRange).withFields(Relationship.Fields.SOURCE_ID).build();
try (final SearchHitsIterator<Relationship> stream = elasticsearchTemplate.searchForStream(relQuery, Relationship.class)) {
stream.forEachRemaining(hit -> {
changedConcepts.add(parseLong(hit.getContent().getSourceId()));
relationships.incrementAndGet();
});
}
timerUtil.checkpoint("relationships " + relationships.get());
logger.debug("Collecting refset member changes for change report: branch {} time range {} to {}", path, start, end);
NativeSearchQuery memberQuery = newSearchQuery(updatesDuringRange).withFilter(boolQuery().must(existsQuery(ReferenceSetMember.Fields.CONCEPT_ID))).withFields(ReferenceSetMember.Fields.REFERENCED_COMPONENT_ID, ReferenceSetMember.Fields.CONCEPT_ID).build();
try (final SearchHitsIterator<ReferenceSetMember> stream = elasticsearchTemplate.searchForStream(memberQuery, ReferenceSetMember.class)) {
stream.forEachRemaining(hit -> referenceComponentIdToConceptMap.put(parseLong(hit.getContent().getReferencedComponentId()), parseLong(hit.getContent().getConceptId())));
}
// Filter out changes for active Synonyms
// Inactive synonym changes should be included to avoid inactivation indicator / association clashes
List<Long> synonymAndTextDefIds = new LongArrayList();
NativeSearchQueryBuilder synonymQuery = new NativeSearchQueryBuilder().withQuery(versionControlHelper.getBranchCriteria(branch).getEntityBranchCriteria(Description.class)).withFilter(boolQuery().mustNot(termQuery(Description.Fields.TYPE_ID, Concepts.FSN)).must(termsQuery(Description.Fields.DESCRIPTION_ID, referenceComponentIdToConceptMap.keySet())).must(termQuery(Description.Fields.ACTIVE, true)));
try (final SearchHitsIterator<Description> stream = elasticsearchTemplate.searchForStream(synonymQuery.build(), Description.class)) {
stream.forEachRemaining(hit -> synonymAndTextDefIds.add(parseLong(hit.getContent().getDescriptionId())));
}
// Keep preferred terms if any
NativeSearchQuery languageMemberQuery = newSearchQuery(updatesDuringRange).withFilter(boolQuery().must(existsQuery(ReferenceSetMember.Fields.CONCEPT_ID)).must(termsQuery(ReferenceSetMember.Fields.REFERENCED_COMPONENT_ID, synonymAndTextDefIds)).must(termsQuery(ReferenceSetMember.LanguageFields.ACCEPTABILITY_ID_FIELD_PATH, Concepts.PREFERRED))).withFields(ReferenceSetMember.Fields.REFERENCED_COMPONENT_ID).build();
try (final SearchHitsIterator<ReferenceSetMember> stream = elasticsearchTemplate.searchForStream(languageMemberQuery, ReferenceSetMember.class)) {
stream.forEachRemaining(hit -> preferredDescriptionIds.add(parseLong(hit.getContent().getReferencedComponentId())));
}
Set<Long> changedComponents = referenceComponentIdToConceptMap.keySet().stream().filter(r -> preferredDescriptionIds.contains(r) || !synonymAndTextDefIds.contains(r)).collect(Collectors.toSet());
for (Long componentId : changedComponents) {
changedConcepts.add(referenceComponentIdToConceptMap.get(componentId));
}
logger.info("Change report complete for branch {} time range {} to {}", path, start, end);
return changedConcepts;
}
Aggregations