doTest(browser, req, 7, answer, null);
}
public void testCustomFacetSort() throws Exception {
BrowseRequest req = new BrowseRequest();
FacetSpec numberSpec = new FacetSpec();
numberSpec.setCustomComparatorFactory(new ComparatorFactory() {
@Override
public IntComparator newComparator(final FieldValueAccessor fieldValueAccessor,
final BigSegmentedArray counts) {
return new IntComparator() {
@Override
public int compare(Integer v1, Integer v2) {
Integer size1 = (Integer) fieldValueAccessor.getRawValue(v1);
Integer size2 = (Integer) fieldValueAccessor.getRawValue(v2);
int val = size1 - size2;
if (val == 0) {
val = counts.get(v1) - counts.get(v2);
}
return val;
}
@Override
public int compare(int v1, int v2) {
int size1 = (Integer) fieldValueAccessor.getRawValue(v1);
int size2 = (Integer) fieldValueAccessor.getRawValue(v2);
int val = size1 - size2;
if (val == 0) {
val = counts.get(v1) - counts.get(v2);
}
return val;
}
};
}
@Override
public Comparator<BrowseFacet> newComparator() {
return new Comparator<BrowseFacet>() {
@Override
public int compare(BrowseFacet o1, BrowseFacet o2) {
int v1 = Integer.parseInt(o1.getValue());
int v2 = Integer.parseInt(o2.getValue());
int val = v2 - v1;
if (val == 0) {
val = o2.getFacetValueHitCount() - o1.getFacetValueHitCount();
}
return val;
}
};
}
});
numberSpec.setOrderBy(FacetSortSpec.OrderByCustom);
numberSpec.setMaxCount(3);
req.setFacetSpec("number", numberSpec);
HashMap<String, List<BrowseFacet>> answer = new HashMap<String, List<BrowseFacet>>();
answer.put(
"number",
Arrays.asList(new BrowseFacet[] { new BrowseFacet("2130", 1), new BrowseFacet("1013", 1),
new BrowseFacet("0913", 1) }));
doTest(req, 7, answer, null);
numberSpec.setOrderBy(FacetSortSpec.OrderValueAsc);
answer.put(
"number",
Arrays.asList(new BrowseFacet[] { new BrowseFacet("0005", 1), new BrowseFacet("0010", 1),
new BrowseFacet("0011", 1) }));