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