skip trying to populate resources in search result bundle if no resources were found

This commit is contained in:
Ken Stevens 2019-11-13 15:48:48 -05:00 committed by James Agnew
parent 251df0849d
commit 25c094e38a
5 changed files with 59 additions and 10 deletions

View File

@ -165,6 +165,9 @@ public class PersistedJpaBundleProvider implements IBundleProvider {
}
protected List<IBaseResource> doSearchOrEverything(final int theFromIndex, final int theToIndex) {
if (mySearchEntity.getNumFound() <= 0) {
return Collections.emptyList();
}
final ISearchBuilder sb = myDao.newSearchBuilder();
String resourceName = mySearchEntity.getResourceType();

View File

@ -40,7 +40,6 @@ import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.api.CacheControlDirective;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.SearchTotalModeEnum;
import ca.uhn.fhir.rest.api.SummaryEnum;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.api.server.IPreResourceAccessDetails;
import ca.uhn.fhir.rest.api.server.RequestDetails;
@ -963,12 +962,8 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc {
*
* before doing anything else.
*/
boolean wantOnlyCount = SummaryEnum.COUNT.equals(myParams.getSummaryMode());
boolean wantCount =
wantOnlyCount ||
SearchTotalModeEnum.ACCURATE.equals(myParams.getSearchTotalMode()) ||
(myParams.getSearchTotalMode() == null && SearchTotalModeEnum.ACCURATE.equals(myDaoConfig.getDefaultTotalMode()));
if (wantCount) {
boolean wantOnlyCount = myParams.wantOnlyCount(myDaoConfig.getDefaultTotalMode());
if (wantOnlyCount) {
ourLog.trace("Performing count");
ISearchBuilder sb = newSearchBuilder();
Iterator<Long> countIterator = sb.createCountQuery(myParams, mySearch.getUuid(), myRequest);

View File

@ -19,6 +19,7 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException;
import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor;
import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;
import ca.uhn.fhir.test.utilities.JettyUtil;
import ca.uhn.fhir.util.BundleUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.ResultSeverityEnum;
@ -55,8 +56,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import ca.uhn.fhir.test.utilities.JettyUtil;
public class SystemProviderR4Test extends BaseJpaR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderR4Test.class);
@ -378,6 +377,19 @@ public class SystemProviderR4Test extends BaseJpaR4Test {
assertEquals(0, respSub.getEntry().size());
}
@Test
public void testCountCache() {
Patient patient = new Patient();
patient.addName().setFamily("Unique762");
myPatientDao.create(patient, mySrd);
Bundle resp1 = (Bundle) ourClient.search().byUrl("Patient?name=Unique762&_summary=count").execute();
ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(resp1));
assertEquals(1, resp1.getTotal());
Bundle resp2 = (Bundle) ourClient.search().byUrl("Patient?name=Unique762&_summary=count").execute();
assertEquals(1, resp2.getTotal());
ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(resp2));
}
@Test
public void testTransactionCreateWithPreferHeader() throws Exception {

View File

@ -0,0 +1,33 @@
package ca.uhn.fhir.jpa.search;
import ca.uhn.fhir.jpa.dao.IDao;
import ca.uhn.fhir.jpa.entity.Search;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
@RunWith(MockitoJUnitRunner.class)
public class PersistedJpaBundleProviderTest {
private PersistedJpaBundleProvider myPersistedJpaBundleProvider;
private IDao myDao;
@Before
public void init() {
RequestDetails request = mock(RequestDetails.class);
String searchUuid = "this is not a hat";
myDao = mock(IDao.class);
myPersistedJpaBundleProvider = new PersistedJpaBundleProvider(request, searchUuid, myDao);
}
@Test
public void zeroNumFoundDoesntCallCache() {
myPersistedJpaBundleProvider.setSearchEntity(new Search());
myPersistedJpaBundleProvider.doSearchOrEverything(0, 0);
verifyNoInteractions(myDao);
}
}

View File

@ -111,7 +111,7 @@ public class SearchParameterMap implements Serializable {
}
}
public void add(String theName, IQueryParameterOr<?> theOr) {
public void add(String theName, IQueryParameterOr<?> theOr) {
if (theOr == null) {
return;
}
@ -495,6 +495,12 @@ public class SearchParameterMap implements Serializable {
}
}
public boolean wantOnlyCount(SearchTotalModeEnum theDefaultTotalMode) {
return SummaryEnum.COUNT.equals(mySummaryMode) ||
SearchTotalModeEnum.ACCURATE.equals(mySearchTotalMode) ||
(mySearchTotalMode == null && SearchTotalModeEnum.ACCURATE.equals(theDefaultTotalMode));
}
public enum EverythingModeEnum {
/*
* Don't reorder! We rely on the ordinals