Commit 7056dae5 authored by Rob Tomsick's avatar Rob Tomsick

Add some sorting + dedup for drug search

parent c922bb91
......@@ -39,18 +39,19 @@ import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.jooq.DSLContext;
import org.jooq.Field;
......@@ -62,7 +63,6 @@ import edu.unc.cscc.crxrest.model.NDC;
import edu.unc.cscc.crxrest.model.TradeName;
import edu.unc.cscc.crxrest.model.UnitOfMeasure;
import edu.unc.cscc.crxrest.model.barcode.Barcode;
import edu.unc.cscc.crxrest.model.barcode.UPCA;
import edu.unc.cscc.crxrest.model.product.Product;
import edu.unc.cscc.crxrest.model.product.ProductComponent;
......@@ -154,12 +154,16 @@ implements DictionaryService
final Field<String> nf = field(name("name"), String.class);
return this.ctx
.select(nf).from(N_TABLE)
.where(nf.likeIgnoreCase(query + "%"))
.orderBy(nf.asc())
.limit(limit)
.fetch(r -> new Drug(r.get(nf)));
List<Drug> drugs = this.ctx
.selectDistinct(nf).from(N_TABLE)
.where(nf.likeIgnoreCase(query + "%"))
.orderBy(nf.asc())
.limit(limit)
.fetch(r -> new Drug(r.get(nf)));
drugs.sort(comparatorFor(query, d -> d.canonicalName()));
return drugs;
}
@Override
......@@ -317,5 +321,14 @@ implements DictionaryService
});
}
private static final <T> Comparator<T>
comparatorFor(String reference, Function<T, String> accessor)
{
return (a, b) ->
StringUtils.getFuzzyDistance(reference, accessor.apply(a), Locale.ENGLISH)
-
StringUtils.getFuzzyDistance(reference, accessor.apply(b), Locale.ENGLISH);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment