diff --git a/api/src/main/java/org/openmrs/Patient.java b/api/src/main/java/org/openmrs/Patient.java index 4a4bb3219200..5b3ea8496a52 100644 --- a/api/src/main/java/org/openmrs/Patient.java +++ b/api/src/main/java/org/openmrs/Patient.java @@ -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 @@ -40,6 +44,7 @@ @Entity @Table(name = "patient") @PrimaryKeyJoinColumn(name = "patient_id") +@Indexed public class Patient extends Person { public static final long serialVersionUID = 93123L; diff --git a/api/src/main/java/org/openmrs/Person.java b/api/src/main/java/org/openmrs/Person.java index e7270ac8ec39..617ae87ad64c 100644 --- a/api/src/main/java/org/openmrs/Person.java +++ b/api/src/main/java/org/openmrs/Person.java @@ -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; @@ -65,6 +70,7 @@ @Entity @Table(name = "person") @Inheritance(strategy = InheritanceType.JOINED) +@Indexed public class Person extends BaseChangeableOpenmrsData { public static final long serialVersionUID = 2L; @@ -102,10 +108,12 @@ public class Person extends BaseChangeableOpenmrsData { @ContainedIn private Set 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; diff --git a/api/src/main/java/org/openmrs/util/OpenmrsConstants.java b/api/src/main/java/org/openmrs/util/OpenmrsConstants.java index b2b919dc4729..c315a077658a 100644 --- a/api/src/main/java/org/openmrs/util/OpenmrsConstants.java +++ b/api/src/main/java/org/openmrs/util/OpenmrsConstants.java @@ -599,7 +599,7 @@ public static final Collection AUTO_ROLES() { /** * Indicates the version of the search index. The index will be rebuilt, if the version changes. * - * @since 1.11 + * @since 1.11 */ public static final Integer SEARCH_INDEX_VERSION = 7; diff --git a/api/src/test/java/org/openmrs/api/db/PatientDAOTest.java b/api/src/test/java/org/openmrs/api/db/PatientDAOTest.java index b579efa612c7..21493b6c2366 100644 --- a/api/src/test/java/org/openmrs/api/db/PatientDAOTest.java +++ b/api/src/test/java/org/openmrs/api/db/PatientDAOTest.java @@ -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; @@ -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 fields=new ArrayList<>(); + fields.add("gender"); + + LuceneQuery 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 updatedQuery = LuceneQuery + .newQuery(Person.class, sessionFactory.getCurrentSession(), "M", fields); + updatedQuery.include("isPatient", true); + updatedQuery.include("voided", false); + Assert.assertEquals(43,luceneQuery.resultSize()); + } } diff --git a/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java b/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java index 23046f71bc40..aab19d1a4028 100644 --- a/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java +++ b/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java @@ -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; @@ -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}; } /**