1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.util;
17
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.codelibs.fess.Constants;
25 import org.elasticsearch.search.aggregations.Aggregations;
26 import org.elasticsearch.search.aggregations.bucket.filter.Filter;
27 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
28
29 import com.google.common.io.BaseEncoding;
30
31 public class FacetResponse {
32 protected Map<String, Long> queryCountMap = new LinkedHashMap<>();
33
34 protected List<Field> fieldList = new ArrayList<>();
35
36 public FacetResponse(final Aggregations aggregations) {
37 aggregations
38 .forEach(aggregation -> {
39 if (aggregation.getName().startsWith(Constants.FACET_FIELD_PREFIX)) {
40 final Terms termFacet = (Terms) aggregation;
41 fieldList.add(new Field(termFacet));
42 } else if (aggregation.getName().startsWith(Constants.FACET_QUERY_PREFIX)) {
43 final Filter queryFacet = (Filter) aggregation;
44 final String encodedQuery = queryFacet.getName().substring(Constants.FACET_QUERY_PREFIX.length());
45 queryCountMap.put(new String(BaseEncoding.base64().decode(encodedQuery), StandardCharsets.UTF_8),
46 queryFacet.getDocCount());
47 }
48
49 });
50 }
51
52 public boolean hasFacetResponse() {
53 return queryCountMap != null || fieldList != null;
54 }
55
56 public static class Field {
57 protected Map<String, Long> valueCountMap = new LinkedHashMap<>();
58
59 protected String name;
60
61 public Field(final Terms termFacet) {
62 final String encodedField = termFacet.getName().substring(Constants.FACET_FIELD_PREFIX.length());
63 name = new String(BaseEncoding.base64().decode(encodedField), StandardCharsets.UTF_8);
64 for (final Terms.Bucket tfEntry : termFacet.getBuckets()) {
65 valueCountMap.put(tfEntry.getKeyAsString(), tfEntry.getDocCount());
66 }
67 }
68
69
70
71
72 public Map<String, Long> getValueCountMap() {
73 return valueCountMap;
74 }
75
76
77
78
79 public String getName() {
80 return name;
81 }
82
83 }
84
85
86
87
88 public Map<String, Long> getQueryCountMap() {
89 return queryCountMap;
90 }
91
92
93
94
95 public List<Field> getFieldList() {
96 return fieldList;
97 }
98
99 }