fix #1801 add facet order

This commit is contained in:
Shinsuke Sugaya 2018-08-08 21:42:51 +09:00
parent 547fb2384a
commit c43d58690c
4 changed files with 26 additions and 6 deletions

View file

@ -17,6 +17,9 @@ package org.codelibs.fess.entity;
import java.util.Arrays;
import org.codelibs.core.lang.StringUtil;
import org.elasticsearch.search.aggregations.BucketOrder;
public class FacetInfo {
public String[] field;
@ -30,6 +33,26 @@ public class FacetInfo {
public String missing;
public BucketOrder getBucketOrder() {
if (StringUtil.isNotBlank(sort)) {
final String[] values = sort.split("\\.");
final boolean asc;
if (values.length > 1) {
asc = !values[1].equalsIgnoreCase("desc");
} else {
asc = true;
}
if (values.length > 0) {
if ("term".equals(values[0]) || "key".equals(values[0])) {
return BucketOrder.key(asc);
} else if ("count".equals(values[0])) {
return BucketOrder.count(asc);
}
}
}
return BucketOrder.count(false);
}
@Override
public String toString() {
return "FacetInfo [field=" + Arrays.toString(field) + ", query=" + Arrays.toString(query) + ", size=" + size + ", minDocCount="

View file

@ -1114,11 +1114,7 @@ public class FessEsClient implements Client {
final String encodedField = BaseEncoding.base64().encode(f.getBytes(StandardCharsets.UTF_8));
final TermsAggregationBuilder termsBuilder =
AggregationBuilders.terms(Constants.FACET_FIELD_PREFIX + encodedField).field(f);
if ("term".equals(facetInfo.sort) || "key".equals(facetInfo.sort)) {
termsBuilder.order(BucketOrder.key(true));
} else if ("count".equals(facetInfo.sort)) {
termsBuilder.order(BucketOrder.count(true));
}
termsBuilder.order(facetInfo.getBucketOrder());
if (facetInfo.size != null) {
termsBuilder.size(facetInfo.size);
}

View file

@ -55,7 +55,7 @@ public class FacetResponse {
}
public static class Field {
protected Map<String, Long> valueCountMap = new HashMap<>();
protected Map<String, Long> valueCountMap = new LinkedHashMap<>();
protected String name;

View file

@ -43,6 +43,7 @@
<property name="defaultOperator">org.apache.lucene.queryparser.classic.QueryParser$Operator.AND</property>
</component>
<component name="facetInfo" class="org.codelibs.fess.entity.FacetInfo">
<property name="sort">"count.desc"</property>
<property name="size">100</property>
<property name="minDocCount">1</property>
<property name="field">["label"]</property>