View Javadoc
1   /*
2    * Copyright 2012-2021 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.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           * @return the valueCountMap
69           */
70          public Map<String, Long> getValueCountMap() {
71              return valueCountMap;
72          }
73  
74          /**
75           * @return the name
76           */
77          public String getName() {
78              return name;
79          }
80  
81      }
82  
83      /**
84       * @return the queryCountMap
85       */
86      public Map<String, Long> getQueryCountMap() {
87          return queryCountMap;
88      }
89  
90      /**
91       * @return the fieldList
92       */
93      public List<Field> getFieldList() {
94          return fieldList;
95      }
96  
97  }