Skip to content

Commit

Permalink
FRI-637 Use filter instead of query for branch path look up
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMChu committed Apr 19, 2023
1 parent f9adc98 commit 98648f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -300,11 +298,7 @@ private Integer getVersionEffectiveDateFromBranchName(String branchName) {
}
return null;
}

private String getHyphenatedEffectiveTimeFromVersionBranch(String branchPath) {
return branchPath.substring(branchPath.lastIndexOf('/') + 1).trim();
}


private String getReleaseBranchPath(String branchPath, Integer effectiveDate) {
return branchPath + "/" + getHyphenatedVersionString(effectiveDate);
}
Expand Down Expand Up @@ -674,8 +668,6 @@ public Set<Branch> findVersionsByCodeSystemAndBaseTimepointRange(CodeSystem code
SearchHits<CodeSystemVersion> queryCodeSystemVersions = elasticsearchOperations.search(
new NativeSearchQueryBuilder()
.withQuery(boolQuery().must(termQuery(CodeSystemVersion.Fields.SHORT_NAME, codeSystem.getShortName())))
.withPageable(PageRequest.of(0, 50))
.withSort(SortBuilders.fieldSort(CodeSystemVersion.Fields.IMPORT_DATE).order(SortOrder.DESC))
.build(), CodeSystemVersion.class
);

Expand All @@ -687,12 +679,10 @@ public Set<Branch> findVersionsByCodeSystemAndBaseTimepointRange(CodeSystem code
Set<String> branchPaths = codeSystemVersions.stream().map(CodeSystemVersion::getBranchPath).collect(Collectors.toSet());
SearchHits<Branch> queryBranches = elasticsearchOperations.search(
new NativeSearchQueryBuilder()
.withQuery(
boolQuery()
.must(termsQuery(Branch.Fields.PATH, branchPaths))
.withQuery(boolQuery()
.must(rangeQuery("base").gt(lowerBound).lte(upperBound))
.mustNot(existsQuery(Branch.Fields.END))
)
).withFilter(termsQuery(Branch.Fields.PATH, branchPaths))
.build(), Branch.class
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.snomed.snowstorm.core.data.services;

import io.kaicode.elasticvc.domain.Branch;
import org.junit.jupiter.api.Test;
import org.snomed.snowstorm.AbstractTest;
import org.snomed.snowstorm.core.data.domain.CodeSystem;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Instant;
import java.util.Set;

import static java.time.Instant.now;
import static org.junit.jupiter.api.Assertions.*;

class CodeSystemServiceTest extends AbstractTest {
Expand Down Expand Up @@ -87,4 +92,20 @@ void testFindLatestEffectiveVersion() {
codeSystemService.setLatestVersionCanBeFuture(false);
}

@Test
void testFindVersionsByCodeSystemAndBaseTimepointRange() {
CodeSystem codeSystem = codeSystemService.createCodeSystem(new CodeSystem("SNOMEDCT", "MAIN"));
codeSystemService.createVersion(codeSystem, 20230101, "20230101 release");
// Within time range
Set<Branch> results = codeSystemService.findVersionsByCodeSystemAndBaseTimepointRange(codeSystem, now().minusMillis(1000l).toEpochMilli(), now().plusMillis(1000L).toEpochMilli());
assertEquals(1, results.size());

// Out of range
results = codeSystemService.findVersionsByCodeSystemAndBaseTimepointRange(codeSystem, now().plusMillis(1000l).toEpochMilli(), now().plusMillis(2000L).toEpochMilli());
assertEquals(0, results.size());

// Time points out of order
assertThrows(IllegalArgumentException.class, () -> codeSystemService.findVersionsByCodeSystemAndBaseTimepointRange(codeSystem, now().toEpochMilli(), now().minusMillis(2000L).toEpochMilli()));
}

}

0 comments on commit 98648f5

Please sign in to comment.