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.entity;
17  
18  import java.util.Arrays;
19  
20  import javax.annotation.PostConstruct;
21  
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.Logger;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.core.stream.StreamUtil;
26  import org.codelibs.fesen.search.aggregations.BucketOrder;
27  import org.codelibs.fess.mylasta.direction.FessConfig;
28  import org.codelibs.fess.util.ComponentUtil;
29  
30  public class FacetInfo {
31      private static final Logger logger = LogManager.getLogger(FacetInfo.class);
32  
33      public String[] field;
34  
35      public String[] query;
36  
37      public Integer size;
38  
39      public Long minDocCount;
40  
41      public String sort;
42  
43      public String missing;
44  
45      @PostConstruct
46      public void init() {
47          final FessConfig fessConfig = ComponentUtil.getFessConfig();
48          if (StringUtil.isNotBlank(fessConfig.getQueryFacetFields())) {
49              field = StreamUtil.split(fessConfig.getQueryFacetFields(), ",")
50                      .get(stream -> stream.map(String::trim).filter(StringUtil::isNotEmpty).distinct().toArray(n -> new String[n]));
51          }
52          if (StringUtil.isNotBlank(fessConfig.getQueryFacetFieldsSize())) {
53              size = fessConfig.getQueryFacetFieldsSizeAsInteger();
54          }
55          if (StringUtil.isNotBlank(fessConfig.getQueryFacetFieldsMinDocCount())) {
56              minDocCount = Long.parseLong(fessConfig.getQueryFacetFieldsMinDocCount());
57          }
58          if (StringUtil.isNotBlank(fessConfig.getQueryFacetFieldsSort())) {
59              sort = fessConfig.getQueryFacetFieldsSort();
60          }
61          if (StringUtil.isNotBlank(fessConfig.getQueryFacetFieldsMissing())) {
62              missing = fessConfig.getQueryFacetFieldsMissing();
63          }
64      }
65  
66      public BucketOrder getBucketOrder() {
67          if (StringUtil.isNotBlank(sort)) {
68              final String[] values = sort.split("\\.");
69              final boolean asc;
70              if (values.length > 1) {
71                  asc = !"desc".equalsIgnoreCase(values[1]);
72              } else {
73                  asc = true;
74              }
75              if (values.length > 0) {
76                  if ("term".equals(values[0]) || "key".equals(values[0])) {
77                      return BucketOrder.key(asc);
78                  }
79                  if ("count".equals(values[0])) {
80                      return BucketOrder.count(asc);
81                  }
82              }
83          }
84          return BucketOrder.count(false);
85      }
86  
87      public void addQuery(final String s) {
88          if (query == null) {
89              query = new String[] { s };
90          } else {
91              final String[] newQuery = Arrays.copyOf(query, query.length + 1);
92              newQuery[query.length] = s;
93              query = newQuery;
94          }
95          if (logger.isDebugEnabled()) {
96              logger.debug("loaded facet query: {}", s);
97          }
98      }
99  
100     @Override
101     public String toString() {
102         return "FacetInfo [field=" + Arrays.toString(field) + ", query=" + Arrays.toString(query) + ", size=" + size + ", minDocCount="
103                 + minDocCount + ", sort=" + sort + ", missing=" + missing + "]";
104     }
105 }