1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.search.solr.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.HashSet; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
import java.util.Set; |
30 |
|
import java.util.regex.Matcher; |
31 |
|
import java.util.regex.Pattern; |
32 |
|
|
33 |
|
import org.apache.solr.common.params.MapSolrParams; |
34 |
|
import org.apache.solr.common.params.SolrParams; |
35 |
|
import org.apache.solr.request.SolrQueryRequest; |
36 |
|
import org.apache.solr.search.ExtendedDismaxQParserPlugin; |
37 |
|
import org.apache.solr.search.QParser; |
38 |
|
import org.apache.solr.util.SolrPluginUtils; |
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@version |
61 |
|
@since |
62 |
|
|
|
|
| 91.8% |
Uncovered Elements: 8 (97) |
Complexity: 26 |
Complexity Density: 0.46 |
|
63 |
|
public class XWikiDismaxQParserPlugin extends ExtendedDismaxQParserPlugin |
64 |
|
{ |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private static final Pattern LIST_SEPARATOR = Pattern.compile("\\s*,\\s*"); |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@see |
78 |
|
|
79 |
|
private static final Pattern FIELD_PATTERN = Pattern.compile("(?:^|[+\\-(\\s])([a-z_][\\p{L}\\p{N}_\\-.$]*):"); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private static final String WILDCARD = "*"; |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
86 |
1020 |
@Override... |
87 |
|
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) |
88 |
|
{ |
89 |
1020 |
return super.createParser(qstr, localParams, withFieldAliases(qstr, params), req); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@param |
97 |
|
@return |
98 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
99 |
1024 |
public SolrParams withFieldAliases(String query, SolrParams parameters)... |
100 |
|
{ |
101 |
1024 |
Set<String> fieldNames = extractFieldNames(query); |
102 |
|
|
103 |
1024 |
String defaultQueryFields = parameters.get("qf"); |
104 |
1024 |
if (defaultQueryFields != null) { |
105 |
1024 |
fieldNames.addAll(SolrPluginUtils.parseFieldBoosts(defaultQueryFields).keySet()); |
106 |
|
} |
107 |
1024 |
if (fieldNames.isEmpty()) { |
108 |
0 |
return parameters; |
109 |
|
} |
110 |
|
|
111 |
1024 |
Map<String, String> aliasParameters = new HashMap<String, String>(); |
112 |
1024 |
addMultilingualFieldAliases(fieldNames, aliasParameters, parameters); |
113 |
1024 |
addTypedDynamicFieldAliases(fieldNames, aliasParameters, parameters); |
114 |
|
|
115 |
1024 |
return aliasParameters.isEmpty() ? parameters : SolrParams.wrapDefaults(new MapSolrParams(aliasParameters), |
116 |
|
parameters); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@param |
123 |
|
@param |
124 |
|
@param |
125 |
|
|
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
127 |
1024 |
private void addMultilingualFieldAliases(Set<String> fieldNames, Map<String, String> aliasParameters,... |
128 |
|
SolrParams parameters) |
129 |
|
{ |
130 |
1024 |
List<String> multilingualFields = getListParameter("xwiki.multilingualFields", parameters); |
131 |
1024 |
if (multilingualFields.isEmpty()) { |
132 |
1 |
return; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
1023 |
List<String> supportedLocales = getSupportedLocales(parameters); |
137 |
|
|
138 |
1023 |
for (String fieldName : fieldNames) { |
139 |
12980 |
if (matchesFieldName(fieldName, multilingualFields)) { |
140 |
7096 |
addAliases(fieldName, supportedLocales, aliasParameters); |
141 |
|
} |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@param |
153 |
|
@param |
154 |
|
@param |
155 |
|
|
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
157 |
1024 |
private void addTypedDynamicFieldAliases(Set<String> fieldNames, Map<String, String> aliasParameters,... |
158 |
|
SolrParams parameters) |
159 |
|
{ |
160 |
1024 |
List<String> typedDynamicFields = getListParameter("xwiki.typedDynamicFields", parameters); |
161 |
1024 |
List<String> dynamicFieldTypes = getListParameter("xwiki.dynamicFieldTypes", parameters); |
162 |
1024 |
if (typedDynamicFields.isEmpty() || dynamicFieldTypes.isEmpty()) { |
163 |
2 |
return; |
164 |
|
} |
165 |
|
|
166 |
1022 |
for (String fieldName : fieldNames) { |
167 |
12978 |
if (matchesFieldName(fieldName, typedDynamicFields)) { |
168 |
289 |
addAliases(fieldName, dynamicFieldTypes, aliasParameters); |
169 |
|
} |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@return |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
179 |
1035 |
public Set<String> extractFieldNames(String query)... |
180 |
|
{ |
181 |
1035 |
Set<String> fieldNames = new HashSet<String>(); |
182 |
1035 |
Matcher matcher = FIELD_PATTERN.matcher(query); |
183 |
1051 |
while (matcher.find()) { |
184 |
16 |
fieldNames.add(matcher.group(1)); |
185 |
|
} |
186 |
1035 |
return fieldNames; |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
@param |
193 |
|
@param |
194 |
|
@return |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
196 |
3072 |
private static List<String> getListParameter(String parameter, SolrParams parameters)... |
197 |
|
{ |
198 |
3072 |
String value = parameters.get(parameter); |
199 |
3072 |
if (value != null) { |
200 |
3067 |
return Arrays.asList(LIST_SEPARATOR.split(value)); |
201 |
|
} else { |
202 |
5 |
return Collections.emptyList(); |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
@param |
208 |
|
@return |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
210 |
1023 |
private static List<String> getSupportedLocales(SolrParams parameters)... |
211 |
|
{ |
212 |
1023 |
List<String> supportedLocalesList = new ArrayList<String>(); |
213 |
1023 |
supportedLocalesList.add("_"); |
214 |
1023 |
String supportedLocales = parameters.get("xwiki.supportedLocales"); |
215 |
1023 |
if (supportedLocales != null) { |
216 |
52 |
supportedLocalesList.addAll(Arrays.asList(LIST_SEPARATOR.split(supportedLocales))); |
217 |
|
} |
218 |
1023 |
return supportedLocalesList; |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
@param |
223 |
|
@param |
224 |
|
@link |
225 |
|
@return |
226 |
|
|
227 |
|
|
|
|
| 75% |
Uncovered Elements: 5 (20) |
Complexity: 6 |
Complexity Density: 0.6 |
|
228 |
25958 |
private boolean matchesFieldName(String fieldName, List<String> fieldNamePatterns)... |
229 |
|
{ |
230 |
25958 |
for (String fieldNamePattern : fieldNamePatterns) { |
231 |
95431 |
if (fieldNamePattern.equals(fieldName)) { |
232 |
6807 |
return true; |
233 |
88624 |
} else if (fieldNamePattern.endsWith(WILDCARD)) { |
234 |
25031 |
if (fieldName.startsWith(fieldNamePattern.substring(0, fieldNamePattern.length() - 1))) { |
235 |
578 |
return true; |
236 |
|
} |
237 |
63593 |
} else if (fieldNamePattern.startsWith(WILDCARD)) { |
238 |
0 |
if (fieldName.endsWith(fieldNamePattern.substring(1))) { |
239 |
0 |
return true; |
240 |
|
} |
241 |
|
} |
242 |
|
} |
243 |
18573 |
return false; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
@param |
250 |
|
@param |
251 |
|
@param |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
253 |
7385 |
private void addAliases(String fieldName, List<String> suffixes, Map<String, String> aliasParameters)... |
254 |
|
{ |
255 |
7385 |
String aliasParameterName = String.format("f.%s.qf", fieldName); |
256 |
7385 |
StringBuilder aliasParameterValue = new StringBuilder(); |
257 |
7385 |
for (String suffix : suffixes) { |
258 |
9425 |
aliasParameterValue.append(' ').append(fieldName).append('_').append(suffix); |
259 |
|
} |
260 |
7385 |
String previousValue = aliasParameters.get(aliasParameterName); |
261 |
7385 |
aliasParameters.put(aliasParameterName, previousValue == null ? aliasParameterValue.substring(1) |
262 |
|
: previousValue + aliasParameterValue.toString()); |
263 |
|
} |
264 |
|
} |