Skip to content

Commit e64b945

Browse files
committed
TRUNK-6683: Skip full-text search with drugs when a concept is provided (#6346)
1 parent 2bd4455 commit e64b945

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

api/src/main/java/org/openmrs/api/db/hibernate/HibernateConceptDAO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,8 @@ private SearchQuery<Drug> newDrugQuery(String drugName, boolean searchKeywords,
15811581
final Collection<Locale> locales = Collections.singletonList(locale == null ? Context.getLocale() : locale);
15821582

15831583
final List<Object> conceptIds;
1584-
if (searchDrugConceptNames) {
1584+
// If a concept is supplied, skip the relatively expensive concept-name full-text search
1585+
if (searchDrugConceptNames && concept == null) {
15851586
SearchSession searchSession = searchSessionFactory.getSearchSession();
15861587
SearchScope<ConceptName> scope = searchSession.scope(ConceptName.class);
15871588

api/src/test/java/org/openmrs/api/db/hibernate/HibernateConceptDAOTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ public void getDrugs_shouldReturnDrugIfPhaseMatchConceptNameNoNeedToMatchBothCon
123123

124124
}
125125

126+
/**
127+
* When a concept is supplied, the concept-name full-text lookup is skipped (TRUNK-6683), so drugs
128+
* belonging to other concepts whose names match the phrase are not returned - only the supplied
129+
* concept's drugs and drugs whose own name matches.
130+
*
131+
* @see HibernateConceptDAO#getDrugs(String,Concept,boolean,boolean,boolean,Integer,Integer)
132+
*/
133+
@Test
134+
public void getDrugs_shouldNotSearchConceptNamesWhenConceptIsSupplied() {
135+
// concept 3 is "COUGH SYRUP"; "ZEBRACONCEPT" only matches the name of concept 20 (drug "Placebo-X"),
136+
// never a drug name. With a concept supplied the concept-name lookup must not run, so Placebo-X is absent.
137+
Concept concept = dao.getConcept(3);
138+
139+
List<Drug> drugList = dao.getDrugs("ZEBRACONCEPT", concept, true, true, false, 0, 10);
140+
141+
assertEquals(1, drugList.size());
142+
assertTrue(drugList.stream().noneMatch(drug -> "Placebo-X".equals(drug.getName())));
143+
}
144+
145+
/**
146+
* The concept-name full-text lookup still runs when no concept is supplied, so a phrase that only
147+
* matches a concept name (not any drug name) still returns that concept's drugs.
148+
*
149+
* @see HibernateConceptDAO#getDrugs(String,Concept,boolean,boolean,boolean,Integer,Integer)
150+
*/
151+
@Test
152+
public void getDrugs_shouldSearchConceptNamesWhenNoConceptIsSupplied() {
153+
List<Drug> drugList = dao.getDrugs("ZEBRACONCEPT", null, true, true, false, 0, 10);
154+
155+
assertEquals(1, drugList.size());
156+
assertEquals("Placebo-X", drugList.get(0).getName());
157+
}
158+
126159
/**
127160
* @see HibernateConceptDAO#getDrugs(String,Concept,boolean,boolean,boolean,Integer,Integer)
128161
*/

api/src/test/resources/org/openmrs/api/db/hibernate/include/HibernateConceptTestDataSet.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@
4343
<drug drug_id="14" concept_id="14" name="TEST_DRUG_NAME" combination="true" creator="1" date_created="2008-08-15 15:34:03.0" retired="false" uuid="8e2323fa-0fa0-461f-9b59-9765997d849e"/>
4444
<drug drug_id="15" concept_id="15" name="TEST_DRUG_NAME_RETIRED" combination="true" creator="1" date_created="2014-05-27 15:48:00.0" retired="true" uuid="8e2323fa-0fa0-461f-9b59-9765997d849f"/>
4545
<drug drug_id="1584" concept_id="15" name="DRUG_NAME_WITH_SPECIAL_CHARACTERS (" combination="true" creator="1" date_created="2015-11-24 15:48:00.0" retired="false" uuid="8e2574fa-0fa0-461f-9b59-9765997d846"/>
46-
</dataset>
46+
<!-- concept 20 has a distinctive name that no drug_name matches; used to verify the concept-name full-text lookup is skipped when a concept is supplied (TRUNK-6683) -->
47+
<concept concept_id="20" retired="false" datatype_id="4" class_id="16" is_set="false" creator="1" date_created="2008-08-15 13:58:36.0" version="" uuid="0abca361-f6bf-49cc-97de-b2f37f099020"/>
48+
<concept_name concept_id="20" name="ZEBRACONCEPT" locale="en" creator="1" date_created="2008-08-15 13:58:36.0" concept_name_id="2500" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" voided="false" uuid="b8159118-c97b-4d5a-a63e-d4aa4be0c520"/>
49+
<drug drug_id="20" concept_id="20" name="Placebo-X" combination="false" creator="1" date_created="2008-08-15 15:34:03.0" retired="false" uuid="8e2323fa-0fa0-461f-9b59-9765997d8420"/>
50+
</dataset>

0 commit comments

Comments
 (0)