View Javadoc
1   /*
2    * Copyright 2012-2020 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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           * @return the valueCountMap
71           */
72          public Map<String, Long> getValueCountMap() {
73              return valueCountMap;
74          }
75  
76          /**
77           * @return the name
78           */
79          public String getName() {
80              return name;
81          }
82  
83      }
84  
85      /**
86       * @return the queryCountMap
87       */
88      public Map<String, Long> getQueryCountMap() {
89          return queryCountMap;
90      }
91  
92      /**
93       * @return the fieldList
94       */
95      public List<Field> getFieldList() {
96          return fieldList;
97      }
98  
99  }