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