Skip to content

Commit

Permalink
GSoC 2019: Patient search criteria added in openmrs core
Browse files Browse the repository at this point in the history
  • Loading branch information
Reyano132 committed Aug 12, 2019
1 parent 4a0a626 commit ae486d1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hibernate.annotations.SortNatural;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

/**
* Defines a Patient in the system. A patient is simply an extension of a person and all that that
Expand All @@ -41,6 +42,7 @@
@Entity
@Table(name = "patient")
@PrimaryKeyJoinColumn(name = "patient_id")
@Indexed
public class Patient extends Person {

public static final long serialVersionUID = 93123L;
Expand Down
10 changes: 9 additions & 1 deletion api/src/main/java/org/openmrs/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.SortNatural;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;
import org.openmrs.util.OpenmrsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -65,6 +70,7 @@
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
@Indexed
public class Person extends BaseChangeableOpenmrsData {

public static final long serialVersionUID = 2L;
Expand Down Expand Up @@ -102,10 +108,12 @@ public class Person extends BaseChangeableOpenmrsData {
@ContainedIn
private Set<PersonAttribute> attributes = null;

@Field
@Field(name="gender",index=Index.YES, analyze=Analyze.YES)
@Column(length = 50)
private String gender;

@Field(name="birthdate",index=Index.YES, analyze=Analyze.YES)
@DateBridge(resolution = Resolution.MILLISECOND)
@Column(name = "birthdate", length = 10)
private Date birthdate;

Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/openmrs/util/OpenmrsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ public static final Collection<String> AUTO_ROLES() {
/**
* Indicates the version of the search index. The index will be rebuilt, if the version changes.
*
* @since 1.11
* @since 2.3
*/
public static final Integer SEARCH_INDEX_VERSION = 7;
public static final Integer SEARCH_INDEX_VERSION = 8;

/**
* @since 1.12
Expand Down
24 changes: 24 additions & 0 deletions api/src/test/java/org/openmrs/api/db/PatientDAOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.openmrs.api.db.hibernate.HibernatePatientDAO;
import org.openmrs.api.db.hibernate.HibernatePersonDAO;
import org.openmrs.api.db.hibernate.PersonAttributeHelper;
import org.openmrs.api.db.hibernate.search.LuceneQuery;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.util.GlobalPropertiesTestHelper;
import org.openmrs.util.OpenmrsConstants;
Expand Down Expand Up @@ -2468,4 +2469,27 @@ public void getPatients_shouldFindPatientsEfficiently() throws IOException, URIS
time = System.currentTimeMillis() - time;
System.out.println("Anywhere search for 'uric' attribute limited to 15 results returned in " + time + " ms");
}

@Test
public void savePatients_shouldChangeLuceneIndexOfPersonClass() {
List<String> fields=new ArrayList<>();
fields.add("gender");

LuceneQuery<Person> luceneQuery = LuceneQuery
.newQuery(Person.class, sessionFactory.getCurrentSession(), "M", fields);
luceneQuery.include("isPatient", true);
luceneQuery.include("voided", false);
Assert.assertEquals(45,luceneQuery.resultSize());
Patient patient=patientService.getPatient(6);
patient.setGender("F");
patientService.savePatient(patient);

updateSearchIndex();
//As the gender of patient(with ID=6) is change to female, count of male patients reduce by 1
LuceneQuery<Person> updatedQuery = LuceneQuery
.newQuery(Person.class, sessionFactory.getCurrentSession(), "M", fields);
updatedQuery.include("isPatient", true);
updatedQuery.include("voided", false);
Assert.assertEquals(44,luceneQuery.resultSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.openmrs.ConceptName;
import org.openmrs.Drug;
import org.openmrs.PatientIdentifier;
import org.openmrs.Person;
import org.openmrs.PersonAttribute;
import org.openmrs.PersonName;
import org.openmrs.User;
Expand Down Expand Up @@ -945,7 +946,7 @@ public void baseSetupWithStandardDataAndAuthentication() throws SQLException {

public Class<?>[] getIndexedTypes() {
return new Class<?>[] { ConceptName.class, Drug.class, PersonName.class, PersonAttribute.class,
PatientIdentifier.class};
PatientIdentifier.class,Person.class};
}

/**
Expand Down

0 comments on commit ae486d1

Please sign in to comment.