diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java index de89c6605a..1d9ad77213 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java @@ -128,18 +128,15 @@ private List getPatientsByNameAndGender(String name, String gender, Int HibernatePatientDAO patientDAO = new HibernatePatientDAO(); patientDAO.setSessionFactory(sessionFactory); - List patients = new ArrayList(); String query = LuceneQuery.escapeQuery(name); PersonLuceneQuery personLuceneQuery = new PersonLuceneQuery(sessionFactory); LuceneQuery nameQuery = personLuceneQuery.getPatientNameQueryWithOrParser(query, false); - List persons = nameQuery.list().stream() - .filter( - personName -> - personName.getPreferred() - && checkGender(personName.getPerson(), gender) - ).collect(toList()); - persons = persons.subList(0, Math.min(length, persons.size())); - persons.forEach(person -> patients.add(new Patient(person.getPerson()))); + List patients = nameQuery.list().stream() + .filter(personName -> + personName.getPreferred() && checkGender(personName.getPerson(), gender)) + .limit(length) + .map(personName -> new Patient(personName.getPerson())) + .collect(toList()); return patients; }