Skip to content

Apache Lucene open-source search software

get/setOperator becomes get/setDefaultOperator and its parameter is now typsafe

Daniel Naber197ad763d1a6
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.java b/src/java/org/apache/lucene/queryParser/QueryParser.java
index 2c2ac5ec77c..29672a5a0af 100644
--- a/src/java/org/apache/lucene/queryParser/QueryParser.java
+++ b/src/java/org/apache/lucene/queryParser/QueryParser.java
@@ -9,6 +9,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
+import org.apache.lucene.util.Parameter;
/**
* This class is generated by JavaCC. The only method that clients should need
@@ -59,11 +60,18 @@ public class QueryParser implements QueryParserConstants {
private static final int MOD_NOT = 10;
private static final int MOD_REQ = 11;
+ /** @deprecated use {@link #OR_OPERATOR} instead */
public static final int DEFAULT_OPERATOR_OR = 0;
+ /** @deprecated use {@link #AND_OPERATOR} instead */
public static final int DEFAULT_OPERATOR_AND = 1;
+ // make it possible to call setDefaultOperator() without accessing
+ // the nested class:
+ public static final Operator AND_OPERATOR = Operator.AND;
+ public static final Operator OR_OPERATOR = Operator.OR;
+
/** The actual operator that parser uses to combine query terms */
- private int operator = DEFAULT_OPERATOR_OR;
+ private Operator operator = OR_OPERATOR;
boolean lowercaseWildcardTerms = true;
@@ -74,6 +82,14 @@ public class QueryParser implements QueryParserConstants {
int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
Locale locale = Locale.getDefault();
+ static final class Operator extends Parameter {
+ private Operator(String name) {
+ super(name);
+ }
+ static final Operator OR = new Operator("OR");
+ static final Operator AND = new Operator("AND");
+ }
+
/** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
* @param query the query string to be parsed.
* @param field the default field for query terms.
@@ -180,16 +196,48 @@ public class QueryParser implements QueryParserConstants {
* <code>capital OR of OR Hungary</code>.<br/>
* In <code>DEFAULT_OPERATOR_AND</code> terms are considered to be in conjuction: the
* above mentioned query is parsed as <code>capital AND of AND Hungary</code>
+ * @deprecated use {@link #setDefaultOperator(QueryParser.Operator)} instead
+ */
+ public void setOperator(int op) {
+ if (op == DEFAULT_OPERATOR_AND)
+ this.operator = AND_OPERATOR;
+ else if (op == DEFAULT_OPERATOR_OR)
+ this.operator = OR_OPERATOR;
+ else
+ throw new IllegalArgumentException("Unknown operator " + op);
+ }
+
+ /**
+ * Sets the boolean operator of the QueryParser.
+ * In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
+ * are considered optional: for example <code>capital of Hungary</code> is equal to
+ * <code>capital OR of OR Hungary</code>.<br/>
+ * In <code>AND_OPERATOR</code> mode terms are considered to be in conjuction: the
+ * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
*/
- public void setOperator(int operator) {
- this.operator = operator;
+ public void setDefaultOperator(Operator op) {
+ this.operator = op;
}
/**
* Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
* or DEFAULT_OPERATOR_OR.
+ * @deprecated use {@link #getDefaultOperator()} instead
*/
public int getOperator() {
+ if(operator == AND_OPERATOR)
+ return DEFAULT_OPERATOR_AND;
+ else if(operator == OR_OPERATOR)
+ return DEFAULT_OPERATOR_OR;
+ else
+ throw new IllegalStateException("Unknown operator " + operator);
+ }
+
+ /**
+ * Gets implicit operator setting, which will be either AND_OPERATOR
+ * or OR_OPERATOR.
+ */
+ public Operator getDefaultOperator() {
return operator;
}
@@ -233,7 +281,7 @@ public class QueryParser implements QueryParserConstants {
c.setOccur(BooleanClause.Occur.MUST);
}
- if (clauses.size() > 0 && operator == DEFAULT_OPERATOR_AND && conj == CONJ_OR) {
+ if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {
// If this term is introduced by OR, make the preceding term optional,
// unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
// notice if the input is a OR b, first term is parsed as required; without
@@ -248,7 +296,7 @@ public class QueryParser implements QueryParserConstants {
if (q == null)
return;
- if (operator == DEFAULT_OPERATOR_OR) {
+ if (operator == OR_OPERATOR) {
// We set REQUIRED if we're introduced by AND or +; PROHIBITED if
// introduced by NOT or -; make sure not to set both.
prohibited = (mods == MOD_NOT);
diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jj
index d77de37644e..2adb7396adc 100644
--- a/src/java/org/apache/lucene/queryParser/QueryParser.jj
+++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj
@@ -32,6 +32,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
+import org.apache.lucene.util.Parameter;
/**
* This class is generated by JavaCC. The only method that clients should need
@@ -82,11 +83,18 @@ public class QueryParser {
private static final int MOD_NOT = 10;
private static final int MOD_REQ = 11;
+ /** @deprecated use {@link #OR_OPERATOR} instead */
public static final int DEFAULT_OPERATOR_OR = 0;
+ /** @deprecated use {@link #AND_OPERATOR} instead */
public static final int DEFAULT_OPERATOR_AND = 1;
+ // make it possible to call setDefaultOperator() without accessing
+ // the nested class:
+ public static final Operator AND_OPERATOR = Operator.AND;
+ public static final Operator OR_OPERATOR = Operator.OR;
+
/** The actual operator that parser uses to combine query terms */
- private int operator = DEFAULT_OPERATOR_OR;
+ private Operator operator = OR_OPERATOR;
boolean lowercaseWildcardTerms = true;
@@ -97,6 +105,14 @@ public class QueryParser {
int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
Locale locale = Locale.getDefault();
+ static final class Operator extends Parameter {
+ private Operator(String name) {
+ super(name);
+ }
+ static final Operator OR = new Operator("OR");
+ static final Operator AND = new Operator("AND");
+ }
+
/** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
* @param query the query string to be parsed.
* @param field the default field for query terms.
@@ -203,16 +219,48 @@ public class QueryParser {
* <code>capital OR of OR Hungary</code>.<br/>
* In <code>DEFAULT_OPERATOR_AND</code> terms are considered to be in conjuction: the
* above mentioned query is parsed as <code>capital AND of AND Hungary</code>
+ * @deprecated use {@link #setDefaultOperator(QueryParser.Operator)} instead
+ */
+ public void setOperator(int op) {
+ if (op == DEFAULT_OPERATOR_AND)
+ this.operator = AND_OPERATOR;
+ else if (op == DEFAULT_OPERATOR_OR)
+ this.operator = OR_OPERATOR;
+ else
+ throw new IllegalArgumentException("Unknown operator " + op);
+ }
+
+ /**
+ * Sets the boolean operator of the QueryParser.
+ * In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
+ * are considered optional: for example <code>capital of Hungary</code> is equal to
+ * <code>capital OR of OR Hungary</code>.<br/>
+ * In <code>AND_OPERATOR</code> mode terms are considered to be in conjuction: the
+ * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
*/
- public void setOperator(int operator) {
- this.operator = operator;
+ public void setDefaultOperator(Operator op) {
+ this.operator = op;
}
/**
* Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
* or DEFAULT_OPERATOR_OR.
+ * @deprecated use {@link #getDefaultOperator()} instead
*/
public int getOperator() {
+ if(operator == AND_OPERATOR)
+ return DEFAULT_OPERATOR_AND;
+ else if(operator == OR_OPERATOR)
+ return DEFAULT_OPERATOR_OR;
+ else
+ throw new IllegalStateException("Unknown operator " + operator);
+ }
+
+ /**
+ * Gets implicit operator setting, which will be either AND_OPERATOR
+ * or OR_OPERATOR.
+ */
+ public Operator getDefaultOperator() {
return operator;
}
@@ -256,7 +304,7 @@ public class QueryParser {
c.setOccur(BooleanClause.Occur.MUST);
}
- if (clauses.size() > 0 && operator == DEFAULT_OPERATOR_AND && conj == CONJ_OR) {
+ if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {
// If this term is introduced by OR, make the preceding term optional,
// unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
// notice if the input is a OR b, first term is parsed as required; without
@@ -271,7 +319,7 @@ public class QueryParser {
if (q == null)
return;
- if (operator == DEFAULT_OPERATOR_OR) {
+ if (operator == OR_OPERATOR) {
// We set REQUIRED if we're introduced by AND or +; PROHIBITED if
// introduced by NOT or -; make sure not to set both.
prohibited = (mods == MOD_NOT);