Skip to content

Commit

Permalink
Merge pull request #11274 from MaineC/feature/simple-query-string-ref…
Browse files Browse the repository at this point in the history
…actoring

Refactors SimpleQueryStringBuilder/Parser
  • Loading branch information
Isabel Drost-Fromm committed Jun 23, 2015
2 parents b2606d9 + e170c8e commit 7afa37c
Show file tree
Hide file tree
Showing 5 changed files with 718 additions and 148 deletions.
Original file line number Diff line number Diff line change
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));
}
}
}
Loading

0 comments on commit 7afa37c

Please sign in to comment.