Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSoC 2019: Patient search criteria added in openmrs core #2980

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
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 @@ -40,6 +44,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
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/util/OpenmrsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we build the openmrs core, it will generate a person's Lucene index

* @since 1.11
*/
public static final Integer SEARCH_INDEX_VERSION = 7;

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(44,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(43,luceneQuery.resultSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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 @@ -946,7 +947,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