diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java index c209d5e5b2b..2fdc3b8f666 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java @@ -1400,7 +1400,7 @@ public class SearchBuilder implements ISearchBuilder { myResourceTableQuery = outerQuery; myResourceTableRoot = myResourceTableQuery.from(ResourceTable.class); if (theCount) { - outerQuery.multiselect(myBuilder.count(myResourceTableRoot.get("myId").as(Long.class))); + outerQuery.multiselect(myBuilder.countDistinct(myResourceTableRoot.get("myId").as(Long.class))); } else { outerQuery.multiselect(myResourceTableRoot.get("myId").as(Long.class)); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3ValueSetTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3ValueSetTest.java index 4a72e9331a2..a3c2e1dec11 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3ValueSetTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3ValueSetTest.java @@ -16,7 +16,6 @@ import org.springframework.transaction.annotation.Transactional; import java.io.IOException; -import static junit.framework.Assert.assertNotNull; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -62,7 +61,7 @@ public class FhirResourceDaoDstu3ValueSetTest extends BaseJpaDstu3Test { } @Test - public void testExpandById() throws IOException { + public void testExpandById() { String resp; ValueSet expanded = myValueSetDao.expand(myExtensionalVsId, null, mySrd); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchOptimizedTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchOptimizedTest.java index 1bddc899c2b..4e3ce096e9c 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchOptimizedTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchOptimizedTest.java @@ -6,6 +6,7 @@ import ca.uhn.fhir.jpa.entity.SearchStatusEnum; import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.SummaryEnum; import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.util.TestUtil; import com.google.common.collect.Sets; @@ -24,6 +25,7 @@ import java.util.concurrent.Future; import static org.apache.commons.lang3.StringUtils.leftPad; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -60,11 +62,50 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test { params.setSummaryMode(Sets.newHashSet(SummaryEnum.COUNT)); IBundleProvider results = myPatientDao.search(params); String uuid = results.getUuid(); + assertEquals(200, results.size().intValue()); List ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true); assertThat(ids, empty()); assertEquals(200, myDatabaseBackedPagingProvider.retrieveResultList(uuid).size().intValue()); } + @Test + public void testFetchCountWithMultipleIndexesOnOneResource() { + + // Already have 200, let's add number 201 with a bunch of similar names + Patient p = new Patient(); + p.setId("PT" + leftPad(Integer.toString(201), 5, '0')); + p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0')); + p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "A"); + p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "AA"); + p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "AAA"); + myPatientDao.update(p); + + myDaoConfig.setSearchPreFetchThresholds(Arrays.asList(20, 50, 190)); + + // Seach with count only + SearchParameterMap params = new SearchParameterMap(); + params.add(Patient.SP_NAME, new StringParam("FAM")); + params.setSummaryMode(Sets.newHashSet(SummaryEnum.COUNT)); + IBundleProvider results = myPatientDao.search(params); + String uuid = results.getUuid(); + assertEquals(201, results.size().intValue()); + List ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true); + assertThat(ids, empty()); + assertEquals(201, myDatabaseBackedPagingProvider.retrieveResultList(uuid).size().intValue()); + + // Seach with count and dat + params = new SearchParameterMap(); + params.add(Patient.SP_NAME, new StringParam("FAM")); + params.setSummaryMode(Sets.newHashSet(SummaryEnum.COUNT, SummaryEnum.DATA)); + results = myPatientDao.search(params); + uuid = results.getUuid(); + assertEquals(201, results.size().intValue()); + ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true); + assertThat(ids, hasSize(10)); + assertEquals(201, myDatabaseBackedPagingProvider.retrieveResultList(uuid).size().intValue()); + } + + @Test public void testFetchCountAndData() { @@ -75,6 +116,7 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test { params.setSummaryMode(Sets.newHashSet(SummaryEnum.COUNT, SummaryEnum.DATA)); IBundleProvider results = myPatientDao.search(params); String uuid = results.getUuid(); + assertEquals(200, results.size().intValue()); List ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true); assertEquals("Patient/PT00000", ids.get(0)); assertEquals("Patient/PT00009", ids.get(9));