Skip to content

Commit

Permalink
Back porting: Concept search should display the name containing all word
Browse files Browse the repository at this point in the history
tokens when multiple words are entered - TRUNK-3232
  • Loading branch information
dkayiwa committed May 28, 2013
1 parent d3bf689 commit 775cc22
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions api/src/main/java/org/openmrs/api/ConceptService.java
Expand Up @@ -1486,6 +1486,7 @@ public List<ConceptWord> findConceptAnswers(String phrase, Locale locale, Concep
* @throws APIException
* @should return the best matched name as the first item in the searchResultsList
* @should return concept search results that match unique concepts
* @should return a search result whose concept name contains all word tokens
* @since 1.8
*/
@Transactional(readOnly = true)
Expand Down
Expand Up @@ -1369,9 +1369,10 @@ private Criteria createConceptWordSearchCriteria(String phrase, List<Locale> loc
if (log.isDebugEnabled())
log.debug("Current word: " + w);

DetachedCriteria crit = DetachedCriteria.forClass(ConceptWord.class).setProjection(
Property.forName("concept")).add(Expression.eqProperty("concept", "cw1.concept")).add(
Restrictions.like("word", w, MatchMode.START)).add(Expression.in("locale", locales));
DetachedCriteria crit = DetachedCriteria.forClass(ConceptWord.class, "cw2").setProjection(
Property.forName("concept")).add(Restrictions.eqProperty("cw2.concept", "cw1.concept")).add(
Restrictions.eqProperty("cw2.conceptName", "cw1.conceptName")).add(
Restrictions.like("word", w, MatchMode.START)).add(Restrictions.in("locale", locales));
junction.add(Subqueries.exists(crit));
}
searchCriteria.add(junction);
Expand Down Expand Up @@ -1466,6 +1467,9 @@ public List<ConceptSearchResult> getConcepts(String phrase, List<Locale> locales
List<ConceptDatatype> requireDatatypes,
List<ConceptDatatype> excludeDatatypes, Concept answersToConcept,
Integer start, Integer size) throws DAOException {
if (StringUtils.isBlank(phrase)) {
phrase = "%"; // match all
}

Criteria searchCriteria = createConceptWordSearchCriteria(phrase, locales, includeRetired, requireClasses,
excludeClasses, requireDatatypes, excludeDatatypes, answersToConcept);
Expand All @@ -1475,13 +1479,16 @@ public List<ConceptSearchResult> getConcepts(String phrase, List<Locale> locales
if (searchCriteria != null) {
ProjectionList pl = Projections.projectionList();
pl.add(Projections.distinct(Projections.groupProperty("cw1.concept")));
pl.add(Projections.property("cw1.word"));
pl.add(Projections.groupProperty("cw1.word"));
//if we have multiple words for the same concept, get the one with a highest weight
pl.add(Projections.max("cw1.weight"));
pl.add(Projections.property("cw1.conceptName"));

pl.add(Projections.max("cw1.weight"), "maxWeight");
//TODO In case a concept has multiple names that contains words that match the search phrase,
//setting this to min or max will select the concept name that was added first or last,
//but it should actually be the one that contains the word with the highest weight.
//see ConceptServiceTest.getConcepts_shouldReturnASearchResultWhoseConceptNameContainsAWordWithMoreWeight()
pl.add(Projections.min("cw1.conceptName"));
searchCriteria.setProjection(pl);
searchCriteria.addOrder(Order.desc("cw1.weight"));
searchCriteria.addOrder(Order.desc("maxWeight"));

if (start != null)
searchCriteria.setFirstResult(start);
Expand Down
24 changes: 24 additions & 0 deletions api/src/test/java/org/openmrs/api/ConceptServiceTest.java
Expand Up @@ -1766,4 +1766,28 @@ public void mapConceptProposalToConcept_shouldFailWhenAddingADuplicateSyonymn()

cs.mapConceptProposalToConcept(cp, mappedConcept, locale);
}

/**
* @see {@link ConceptService#getConcepts(String, List, boolean, List, List, List, List, Concept, Integer, Integer)}
*/
@Test
@Verifies(value = "should return a search result whose concept name contains all word tokens", method = "getConcepts(String,List<QLocale;>,null,List<QConceptClass;>,List<QConceptClass;>,List<QConceptDatatype;>,List<QConceptDatatype;>,Concept,Integer,Integer)")
public void getConcepts_shouldReturnASearchResultWhoseConceptNameContainsAllWordTokens() throws Exception {
executeDataSet("org/openmrs/api/include/ConceptServiceTest-words.xml");
Concept conceptWithMultipleMatchingNames = conceptService.getConcept(798);

//recalculate the weights just in case the logic for calculating the weights is changed
conceptService.updateConceptIndex(conceptWithMultipleMatchingNames);
conceptService.updateConceptIndex(conceptService.getConcept(240));
conceptService.updateConceptIndex(conceptService.getConcept(357));
conceptService.updateConceptIndex(conceptService.getConcept(328));
conceptService.updateConceptIndex(conceptService.getConcept(1240));

List<ConceptSearchResult> searchResults = conceptService.getConcepts("SALBUTAMOL INHALER", Collections
.singletonList(Locale.ENGLISH), false, null, null, null, null, null, null, null);

Assert.assertEquals(1, searchResults.size());
Assert.assertEquals(conceptWithMultipleMatchingNames, searchResults.get(0).getConcept());
Assert.assertEquals("SALBUTAMOL INHALER", searchResults.get(0).getConceptName().getName());
}
}
Expand Up @@ -17,4 +17,37 @@
<concept_name_tag_map concept_name_id="2458" concept_name_tag_id="2"/>
<concept_name_tag_map concept_name_id="2704" concept_name_tag_id="3"/>
<concept_name_tag_map concept_name_id="2707" concept_name_tag_id="3"/>

<concept concept_id="240" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5e1"/>
<concept concept_id="357" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5e2"/>
<concept concept_id="798" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5e3"/>
<concept concept_id="328" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5e4"/>
<concept concept_id="1240" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5e5"/>

<concept_name concept_id="240" name="SALBUTAMOL TABS" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="240" voided="false" concept_name_type="FULLY_SPECIFIED" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e1"/>
<concept_name concept_id="357" name="SALBUTAMOL SYRUP" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="357" voided="false" concept_name_type="FULLY_SPECIFIED" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e2"/>
<concept_name concept_id="798" name="SALBUTAMOL" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="798" voided="false" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" uuid="67ce91b2-e879-11e0-950d-00248140a5e3"/>
<concept_name concept_id="798" name="SALBUTAMOL INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="3256" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e4"/>
<concept_name concept_id="798" name="SALBUTAMOL SYRUP" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="3257" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e5"/>
<concept_name concept_id="798" name="SALBUTAMOL TABS" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="3258" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e6"/>

<concept_name concept_id="328" name="VENTOLIN INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="328" voided="false" concept_name_type="FULLY_SPECIFIED" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e7"/>
<concept_name concept_id="798" name="VENTOLIN INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="3403" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e8"/>
<concept_name concept_id="798" name="SALBUTAMOL INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="3256" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a5e9"/>
<concept_name concept_id="1240" name="BECLOMETHASONE INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="2662" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a510"/>
<concept_name concept_id="1240" name="BECLOMETASONE INHALER" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="2661" voided="false" locale_preferred="0" uuid="67ce91b2-e879-11e0-950d-00248140a511"/>

<concept_word concept_word_id="7394" concept_id="240" word="SALBUTAMOL" locale="en" concept_name_id="240" weight="2.19"/>
<concept_word concept_word_id="7706" concept_id="357" word="SALBUTAMOL" locale="en" concept_name_id="357" weight="2.178"/>
<concept_word concept_word_id="14009" concept_id="798" word="SALBUTAMOL" locale="en" concept_name_id="3256" weight="2.126666666666667"/>
<concept_word concept_word_id="14011" concept_id="798" word="SALBUTAMOL" locale="en" concept_name_id="3257" weight="2.1475"/>
<concept_word concept_word_id="14012" concept_id="798" word="SALBUTAMOL" locale="en" concept_name_id="798" weight="8.475"/>
<concept_word concept_word_id="14014" concept_id="798" word="SALBUTAMOL" locale="en" concept_name_id="3258" weight="2.16"/>

<concept_word concept_word_id="7637" concept_id="328" word="INHALER" locale="en" concept_name_id="328" weight="1.349"/>
<concept_word concept_word_id="10574" concept_id="1240" word="INHALER" locale="en" concept_name_id="2662" weight="1.308"/>
<concept_word concept_word_id="10575" concept_id="1240" word="INHALER" locale="en" concept_name_id="2661" weight="1.312"/>
<concept_word concept_word_id="14010" concept_id="798" word="INHALER" locale="en" concept_name_id="3403" weight="1.3387499999999999"/>
<concept_word concept_word_id="14015" concept_id="798" word="INHALER" locale="en" concept_name_id="3256" weight="1.3264814814814816"/>

</dataset>

0 comments on commit 775cc22

Please sign in to comment.