Skip to content

Commit

Permalink
TRUNK-4425 Preferred name matches should be sorted above synonym matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorytkowski committed Jul 15, 2014
1 parent ba5d463 commit 93d60c1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/src/main/java/org/openmrs/ConceptName.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class ConceptName extends BaseOpenmrsObject implements Auditable, Voidabl

private ConceptNameType conceptNameType;

@Field
private Boolean localePreferred = false;

// Constructors
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/org/openmrs/api/ConceptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ public interface ConceptService extends OpenmrsService {
* @should return a search result whose concept name contains all word tokens as first
* @should return a search result for phrase with stop words
* @should not return concepts with matching names that are voided
* @should return preferred names higher
* @since 1.8
*/
@Authorized(PrivilegeConstants.GET_CONCEPTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,14 @@ private String newConceptNameQuery(final String name, final boolean searchKeywor
final String escapedName = LuceneQuery.escapeQuery(name);
final List<String> tokenizedName = tokenizeConceptName(escapedName, locales);

final StringBuilder query = newNameQuery(tokenizedName, escapedName, searchKeywords);
final StringBuilder query = new StringBuilder();

query.append("((");
final StringBuilder nameQuery = newNameQuery(tokenizedName, escapedName, searchKeywords);
query.append(nameQuery);
query.append(" localePreferred:true)^0.8 OR (");
query.append(nameQuery);
query.append(")^0.2)");

List<String> localeQueries = new ArrayList<String>();
for (Locale locale : locales) {
Expand Down
27 changes: 27 additions & 0 deletions api/src/test/java/org/openmrs/api/ConceptServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.openmrs.api;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
Expand All @@ -25,6 +26,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.openmrs.test.OpenmrsMatchers.hasId;
import static org.openmrs.test.OpenmrsMatchers.hasConcept;
import static org.openmrs.test.TestUtil.containsId;

import java.util.ArrayList;
Expand Down Expand Up @@ -3217,4 +3219,29 @@ public void getOrderableConcepts_shouldGetOrderableConcepts() throws Exception {
Collections.singletonList(locale), true, null, null);
assertEquals(2, conceptSearchResultList.size());
}

/**
* @see ConceptService#getConcepts(String,List,boolean,List,List,List,List,Concept,Integer,Integer)
* @verifies return preferred names higher
*/
@Test
public void getConcepts_shouldReturnPreferredNamesHigher() throws Exception {
Concept hivProgram = conceptService.getConceptByName("hiv program");
ConceptName synonym = new ConceptName("synonym", Context.getLocale());
hivProgram.addName(synonym);
conceptService.saveConcept(hivProgram);

Concept mdrTbProgram = conceptService.getConceptByName("mdr-tb program");
synonym = new ConceptName("synonym", Context.getLocale());
synonym.setLocalePreferred(true);
mdrTbProgram.addName(synonym);
conceptService.saveConcept(mdrTbProgram);

updateSearchIndex();

List<ConceptSearchResult> concepts = conceptService.getConcepts("synonym", null, false, null, null, null, null,
null, null, null);

assertThat(concepts, contains(hasConcept(is(mdrTbProgram)), hasConcept(is(hivProgram))));
}
}
19 changes: 19 additions & 0 deletions api/src/test/java/org/openmrs/test/OpenmrsMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.openmrs.Concept;
import org.openmrs.ConceptSearchResult;
import org.openmrs.OpenmrsObject;

/**
Expand Down Expand Up @@ -59,4 +61,21 @@ protected String featureValueOf(final OpenmrsObject actual) {
}
};
}

/**
* Matches by concept.
*
* @param concept
* @return the concept
*/
public static Matcher<ConceptSearchResult> hasConcept(final Matcher<Concept> concept) {
return new FeatureMatcher<ConceptSearchResult, Concept>(
concept, "concept", "concept") {

@Override
protected Concept featureValueOf(ConceptSearchResult actual) {
return actual.getConcept();
}
};
}
}

0 comments on commit 93d60c1

Please sign in to comment.