|
29 | 29 | import java.io.IOException; |
30 | 30 | import java.util.Locale; |
31 | 31 | import java.util.Map; |
| 32 | +import java.util.Objects; |
32 | 33 |
|
33 | 34 | /** |
34 | 35 | * Wrapper class for Lucene's SimpleQueryParser that allows us to redefine |
@@ -202,51 +203,102 @@ private Query newPossiblyAnalyzedQuery(String field, String termStr) { |
202 | 203 | return new PrefixQuery(new Term(field, termStr)); |
203 | 204 | } |
204 | 205 | } |
205 | | - |
206 | 206 | /** |
207 | 207 | * Class encapsulating the settings for the SimpleQueryString query, with |
208 | 208 | * their default values |
209 | 209 | */ |
210 | | - public static class Settings { |
211 | | - private Locale locale = Locale.ROOT; |
212 | | - private boolean lowercaseExpandedTerms = true; |
213 | | - private boolean lenient = false; |
214 | | - private boolean analyzeWildcard = false; |
| 210 | + static class Settings { |
| 211 | + /** Locale to use for parsing. */ |
| 212 | + private Locale locale = SimpleQueryStringBuilder.DEFAULT_LOCALE; |
| 213 | + /** Specifies whether parsed terms should be lowercased. */ |
| 214 | + private boolean lowercaseExpandedTerms = SimpleQueryStringBuilder.DEFAULT_LOWERCASE_EXPANDED_TERMS; |
| 215 | + /** Specifies whether lenient query parsing should be used. */ |
| 216 | + private boolean lenient = SimpleQueryStringBuilder.DEFAULT_LENIENT; |
| 217 | + /** Specifies whether wildcards should be analyzed. */ |
| 218 | + private boolean analyzeWildcard = SimpleQueryStringBuilder.DEFAULT_ANALYZE_WILDCARD; |
215 | 219 |
|
| 220 | + /** |
| 221 | + * Generates default {@link Settings} object (uses ROOT locale, does |
| 222 | + * lowercase terms, no lenient parsing, no wildcard analysis). |
| 223 | + * */ |
216 | 224 | public Settings() { |
| 225 | + } |
217 | 226 |
|
| 227 | + public Settings(Locale locale, Boolean lowercaseExpandedTerms, Boolean lenient, Boolean analyzeWildcard) { |
| 228 | + this.locale = locale; |
| 229 | + this.lowercaseExpandedTerms = lowercaseExpandedTerms; |
| 230 | + this.lenient = lenient; |
| 231 | + this.analyzeWildcard = analyzeWildcard; |
218 | 232 | } |
219 | 233 |
|
| 234 | + /** Specifies the locale to use for parsing, Locale.ROOT by default. */ |
220 | 235 | public void locale(Locale locale) { |
221 | | - this.locale = locale; |
| 236 | + this.locale = (locale != null) ? locale : SimpleQueryStringBuilder.DEFAULT_LOCALE; |
222 | 237 | } |
223 | 238 |
|
| 239 | + /** Returns the locale to use for parsing. */ |
224 | 240 | public Locale locale() { |
225 | 241 | return this.locale; |
226 | 242 | } |
227 | 243 |
|
| 244 | + /** |
| 245 | + * Specifies whether to lowercase parse terms, defaults to true if |
| 246 | + * unset. |
| 247 | + */ |
228 | 248 | public void lowercaseExpandedTerms(boolean lowercaseExpandedTerms) { |
229 | 249 | this.lowercaseExpandedTerms = lowercaseExpandedTerms; |
230 | 250 | } |
231 | 251 |
|
| 252 | + /** Returns whether to lowercase parse terms. */ |
232 | 253 | public boolean lowercaseExpandedTerms() { |
233 | 254 | return this.lowercaseExpandedTerms; |
234 | 255 | } |
235 | 256 |
|
| 257 | + /** Specifies whether to use lenient parsing, defaults to false. */ |
236 | 258 | public void lenient(boolean lenient) { |
237 | 259 | this.lenient = lenient; |
238 | 260 | } |
239 | 261 |
|
| 262 | + /** Returns whether to use lenient parsing. */ |
240 | 263 | public boolean lenient() { |
241 | 264 | return this.lenient; |
242 | 265 | } |
243 | 266 |
|
| 267 | + /** Specifies whether to analyze wildcards. Defaults to false if unset. */ |
244 | 268 | public void analyzeWildcard(boolean analyzeWildcard) { |
245 | 269 | this.analyzeWildcard = analyzeWildcard; |
246 | 270 | } |
247 | 271 |
|
| 272 | + /** Returns whether to analyze wildcards. */ |
248 | 273 | public boolean analyzeWildcard() { |
249 | 274 | return analyzeWildcard; |
250 | 275 | } |
| 276 | + |
| 277 | + @Override |
| 278 | + public int hashCode() { |
| 279 | + // checking the return value of toLanguageTag() for locales only. |
| 280 | + // For further reasoning see |
| 281 | + // https://issues.apache.org/jira/browse/LUCENE-4021 |
| 282 | + return Objects.hash(locale.toLanguageTag(), lowercaseExpandedTerms, lenient, analyzeWildcard); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public boolean equals(Object obj) { |
| 287 | + if (this == obj) { |
| 288 | + return true; |
| 289 | + } |
| 290 | + if (obj == null || getClass() != obj.getClass()) { |
| 291 | + return false; |
| 292 | + } |
| 293 | + Settings other = (Settings) obj; |
| 294 | + |
| 295 | + // checking the return value of toLanguageTag() for locales only. |
| 296 | + // For further reasoning see |
| 297 | + // https://issues.apache.org/jira/browse/LUCENE-4021 |
| 298 | + return (Objects.equals(locale.toLanguageTag(), other.locale.toLanguageTag()) |
| 299 | + && Objects.equals(lowercaseExpandedTerms, other.lowercaseExpandedTerms) |
| 300 | + && Objects.equals(lenient, other.lenient) |
| 301 | + && Objects.equals(analyzeWildcard, other.analyzeWildcard)); |
| 302 | + } |
251 | 303 | } |
252 | 304 | } |
0 commit comments