diff --git a/api/src/main/java/org/openmrs/Patient.java b/api/src/main/java/org/openmrs/Patient.java index a21b2028b471..6b8584075ccc 100644 --- a/api/src/main/java/org/openmrs/Patient.java +++ b/api/src/main/java/org/openmrs/Patient.java @@ -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 @@ -41,6 +42,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..ca78e69e69a5 100644 --- a/api/src/main/java/org/openmrs/util/OpenmrsConstants.java +++ b/api/src/main/java/org/openmrs/util/OpenmrsConstants.java @@ -599,9 +599,9 @@ 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 2.3 */ - public static final Integer SEARCH_INDEX_VERSION = 7; + public static final Integer SEARCH_INDEX_VERSION = 8; /** * @since 1.12 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..788459e9ec72 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(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 updatedQuery = LuceneQuery + .newQuery(Person.class, sessionFactory.getCurrentSession(), "M", fields); + updatedQuery.include("isPatient", true); + updatedQuery.include("voided", false); + Assert.assertEquals(44,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 1381b148fcc4..0f2322cb8128 100644 --- a/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java +++ b/api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java @@ -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; @@ -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}; } /**