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 static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.Locale;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.codelibs.core.lang.StringUtil;
26  
27  public abstract class SearchRequestParams {
28  
29      public static final String AS_NQ = "nq";
30  
31      public static final String AS_OQ = "oq";
32  
33      public static final String AS_EPQ = "epq";
34  
35      public static final String AS_Q = "q";
36  
37      public static final String AS_FILETYPE = "filetype";
38  
39      public static final String AS_SITESEARCH = "sitesearch";
40  
41      public static final String AS_OCCURRENCE = "occt";
42  
43      public static final String AS_TIMESTAMP = "timestamp";
44  
45      public abstract String getQuery();
46  
47      public abstract Map<String, String[]> getFields();
48  
49      public abstract Map<String, String[]> getConditions();
50  
51      public abstract String[] getLanguages();
52  
53      public abstract GeoInfo getGeoInfo();
54  
55      public abstract FacetInfo getFacetInfo();
56  
57      public abstract HighlightInfo getHighlightInfo();
58  
59      public abstract String getSort();
60  
61      public abstract int getStartPosition();
62  
63      public abstract int getPageSize();
64  
65      public abstract String[] getExtraQueries();
66  
67      public abstract Object getAttribute(String name);
68  
69      public abstract Locale getLocale();
70  
71      public abstract SearchRequestType getType();
72  
73      public abstract String getSimilarDocHash();
74  
75      public String getTrackTotalHits() {
76          return null;
77      }
78  
79      public boolean hasConditionQuery() {
80          final Map<String, String[]> conditions = getConditions();
81          return !isEmptyArray(conditions.get(AS_Q))//
82                  || !isEmptyArray(conditions.get(AS_EPQ))//
83                  || !isEmptyArray(conditions.get(AS_OQ))//
84                  || !isEmptyArray(conditions.get(AS_NQ))//
85                  || !isEmptyArray(conditions.get(AS_TIMESTAMP))//
86                  || !isEmptyArray(conditions.get(AS_SITESEARCH))//
87                  || !isEmptyArray(conditions.get(AS_FILETYPE));
88      }
89  
90      protected boolean isEmptyArray(final String[] values) {
91          if (values == null || values.length == 0) {
92              return true;
93          }
94          return stream(values).get(stream -> stream.allMatch(StringUtil::isBlank));
95      }
96  
97      protected String[] simplifyArray(final String[] values) {
98          return stream(values).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
99      }
100 
101     protected String[] getParamValueArray(final HttpServletRequest request, final String param) {
102         return simplifyArray(request.getParameterValues(param));
103     }
104 
105     protected FacetInfo createFacetInfo(final HttpServletRequest request) {
106         final String[] fields = getParamValueArray(request, "facet.field");
107         final String[] queries = getParamValueArray(request, "facet.query");
108         if (fields.length == 0 && queries.length == 0) {
109             return null;
110         }
111         final FacetInfo facetInfo = new FacetInfo();
112         facetInfo.field = fields;
113         facetInfo.query = queries;
114         final String sizeStr = request.getParameter("facet.size");
115         if (StringUtil.isNotBlank(sizeStr)) {
116             facetInfo.size = Integer.parseInt(sizeStr);
117         }
118         final String minDocCountStr = request.getParameter("facet.minDocCount");
119         if (StringUtil.isNotBlank(minDocCountStr)) {
120             facetInfo.minDocCount = Long.parseLong(minDocCountStr);
121         }
122         final String sort = request.getParameter("facet.sort");
123         if (StringUtil.isNotBlank(sort)) {
124             facetInfo.sort = sort;
125         }
126         final String missing = request.getParameter("facet.missing");
127         if (StringUtil.isNotBlank(missing)) {
128             facetInfo.missing = missing;
129         }
130         return facetInfo;
131     }
132 
133     protected GeoInfo createGeoInfo(final HttpServletRequest request) {
134         return new GeoInfo(request);
135     }
136 
137     public enum SearchRequestType {
138         SEARCH, ADMIN_SEARCH, JSON, GSA, SUGGEST;
139     }
140 }