Fix bad counts when multiple parameters match

This commit is contained in:
James Agnew 2018-10-05 13:02:59 -04:00
parent ca54409ce3
commit 34e943b49b
3 changed files with 44 additions and 3 deletions

View File

@ -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));
}

View File

@ -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);

View File

@ -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<String> 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<String> 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<String> ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true);
assertEquals("Patient/PT00000", ids.get(0));
assertEquals("Patient/PT00009", ids.get(9));