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