diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java index e7bee19e6b2..1edae5bab32 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java @@ -889,7 +889,7 @@ public class FhirResourceDao extends BaseFhirDao implements throw new NotImplementedException("This server does not support _sort specifications of type " + param.getParamType() + " - Can't serve _sort=" + theSort.getParamName()); } - From stringJoin = theFrom.join(joinAttrName, JoinType.LEFT); + From stringJoin = theFrom.join(joinAttrName, JoinType.INNER); // Predicate p = theBuilder.equal(stringJoin.get("myParamName"), theSort.getParamName()); // Predicate pn = theBuilder.isNull(stringJoin.get("myParamName")); // thePredicates.add(theBuilder.or(p, pn)); @@ -1247,7 +1247,7 @@ public class FhirResourceDao extends BaseFhirDao implements } } - final List pids = new ArrayList(loadPids); + final List pids; // Handle sorting if any was provided if (theParams.getSort() != null && isNotBlank(theParams.getSort().getParamName())) { @@ -1256,9 +1256,10 @@ public class FhirResourceDao extends BaseFhirDao implements CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery cq = builder.createTupleQuery(); Root from = cq.from(ResourceTable.class); - predicates.add(from.get("myId").in(pids)); + predicates.add(from.get("myId").in(loadPids)); createSort(builder, from, theParams.getSort(), orders, predicates); if (orders.size() > 0) { + Set originalPids = loadPids; loadPids = new LinkedHashSet(); cq.multiselect(from.get("myId").as(Long.class)); cq.where(predicates.toArray(new Predicate[0])); @@ -1272,9 +1273,20 @@ public class FhirResourceDao extends BaseFhirDao implements ourLog.info("Sort PID order is now: {}", loadPids); - pids.clear(); - pids.addAll(loadPids); + pids = new ArrayList(loadPids); + + // Any ressources which weren't matched by the sort get added to the bottom + for (Long next : originalPids) { + if (loadPids.contains(next)==false) { + pids.add(next); + } + } + + }else { + pids = new ArrayList(loadPids); } + } else { + pids = new ArrayList(loadPids); } IBundleProvider retVal = new IBundleProvider() { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java index 9ce77377f01..0c7f2293737 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java @@ -1568,7 +1568,7 @@ public class FhirResourceDaoTest { pm.setSort(new SortSpec(Patient.SP_BIRTHDATE).setOrder(SortOrderEnum.DESC)); actual = toUnqualifiedVersionlessIds(ourPatientDao.search(pm)); assertEquals(4, actual.size()); - assertThat(actual, contains(id4, id3, id2, id1)); + assertThat(actual, contains(id3, id2, id1, id4)); }