Skip to content

Commit 7afa37c

Browse files
author
Isabel Drost-Fromm
committed
Merge pull request #11274 from MaineC/feature/simple-query-string-refactoring
Refactors SimpleQueryStringBuilder/Parser
2 parents b2606d9 + e170c8e commit 7afa37c

File tree

5 files changed

+718
-148
lines changed

5 files changed

+718
-148
lines changed

core/src/main/java/org/elasticsearch/index/query/SimpleQueryParser.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.util.Locale;
3131
import java.util.Map;
32+
import java.util.Objects;
3233

3334
/**
3435
* Wrapper class for Lucene's SimpleQueryParser that allows us to redefine
@@ -202,51 +203,102 @@ private Query newPossiblyAnalyzedQuery(String field, String termStr) {
202203
return new PrefixQuery(new Term(field, termStr));
203204
}
204205
}
205-
206206
/**
207207
* Class encapsulating the settings for the SimpleQueryString query, with
208208
* their default values
209209
*/
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;
215219

220+
/**
221+
* Generates default {@link Settings} object (uses ROOT locale, does
222+
* lowercase terms, no lenient parsing, no wildcard analysis).
223+
* */
216224
public Settings() {
225+
}
217226

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;
218232
}
219233

234+
/** Specifies the locale to use for parsing, Locale.ROOT by default. */
220235
public void locale(Locale locale) {
221-
this.locale = locale;
236+
this.locale = (locale != null) ? locale : SimpleQueryStringBuilder.DEFAULT_LOCALE;
222237
}
223238

239+
/** Returns the locale to use for parsing. */
224240
public Locale locale() {
225241
return this.locale;
226242
}
227243

244+
/**
245+
* Specifies whether to lowercase parse terms, defaults to true if
246+
* unset.
247+
*/
228248
public void lowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
229249
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
230250
}
231251

252+
/** Returns whether to lowercase parse terms. */
232253
public boolean lowercaseExpandedTerms() {
233254
return this.lowercaseExpandedTerms;
234255
}
235256

257+
/** Specifies whether to use lenient parsing, defaults to false. */
236258
public void lenient(boolean lenient) {
237259
this.lenient = lenient;
238260
}
239261

262+
/** Returns whether to use lenient parsing. */
240263
public boolean lenient() {
241264
return this.lenient;
242265
}
243266

267+
/** Specifies whether to analyze wildcards. Defaults to false if unset. */
244268
public void analyzeWildcard(boolean analyzeWildcard) {
245269
this.analyzeWildcard = analyzeWildcard;
246270
}
247271

272+
/** Returns whether to analyze wildcards. */
248273
public boolean analyzeWildcard() {
249274
return analyzeWildcard;
250275
}
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+
}
251303
}
252304
}

0 commit comments

Comments
 (0)