Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors SimpleQueryStringBuilder/Parser #11274

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
* Wrapper class for Lucene's SimpleQueryParser that allows us to redefine
Expand Down Expand Up @@ -202,51 +203,102 @@ private Query newPossiblyAnalyzedQuery(String field, String termStr) {
return new PrefixQuery(new Term(field, termStr));
}
}

/**
* Class encapsulating the settings for the SimpleQueryString query, with
* their default values
*/
public static class Settings {
private Locale locale = Locale.ROOT;
private boolean lowercaseExpandedTerms = true;
private boolean lenient = false;
private boolean analyzeWildcard = false;
static class Settings {
/** Locale to use for parsing. */
private Locale locale = SimpleQueryStringBuilder.DEFAULT_LOCALE;
/** Specifies whether parsed terms should be lowercased. */
private boolean lowercaseExpandedTerms = SimpleQueryStringBuilder.DEFAULT_LOWERCASE_EXPANDED_TERMS;
/** Specifies whether lenient query parsing should be used. */
private boolean lenient = SimpleQueryStringBuilder.DEFAULT_LENIENT;
/** Specifies whether wildcards should be analyzed. */
private boolean analyzeWildcard = SimpleQueryStringBuilder.DEFAULT_ANALYZE_WILDCARD;

/**
* Generates default {@link Settings} object (uses ROOT locale, does
* lowercase terms, no lenient parsing, no wildcard analysis).
* */
public Settings() {
}

public Settings(Locale locale, Boolean lowercaseExpandedTerms, Boolean lenient, Boolean analyzeWildcard) {
this.locale = locale;
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
this.lenient = lenient;
this.analyzeWildcard = analyzeWildcard;
}

/** Specifies the locale to use for parsing, Locale.ROOT by default. */
public void locale(Locale locale) {
this.locale = locale;
this.locale = (locale != null) ? locale : SimpleQueryStringBuilder.DEFAULT_LOCALE;
}

/** Returns the locale to use for parsing. */
public Locale locale() {
return this.locale;
}

/**
* Specifies whether to lowercase parse terms, defaults to true if
* unset.
*/
public void lowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
this.lowercaseExpandedTerms = lowercaseExpandedTerms;
}

/** Returns whether to lowercase parse terms. */
public boolean lowercaseExpandedTerms() {
return this.lowercaseExpandedTerms;
}

/** Specifies whether to use lenient parsing, defaults to false. */
public void lenient(boolean lenient) {
this.lenient = lenient;
}

/** Returns whether to use lenient parsing. */
public boolean lenient() {
return this.lenient;
}

/** Specifies whether to analyze wildcards. Defaults to false if unset. */
public void analyzeWildcard(boolean analyzeWildcard) {
this.analyzeWildcard = analyzeWildcard;
}

/** Returns whether to analyze wildcards. */
public boolean analyzeWildcard() {
return analyzeWildcard;
}

@Override
public int hashCode() {
// checking the return value of toLanguageTag() for locales only.
// For further reasoning see
// https://issues.apache.org/jira/browse/LUCENE-4021
return Objects.hash(locale.toLanguageTag(), lowercaseExpandedTerms, lenient, analyzeWildcard);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Settings other = (Settings) obj;

// checking the return value of toLanguageTag() for locales only.
// For further reasoning see
// https://issues.apache.org/jira/browse/LUCENE-4021
return (Objects.equals(locale.toLanguageTag(), other.locale.toLanguageTag())
&& Objects.equals(lowercaseExpandedTerms, other.lowercaseExpandedTerms)
&& Objects.equals(lenient, other.lenient)
&& Objects.equals(analyzeWildcard, other.analyzeWildcard));
}
}
}