Index: QueryParser.jj =================================================================== RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v retrieving revision 1.55 diff -u -r1.55 QueryParser.jj --- QueryParser.jj 13 Nov 2004 15:11:26 -0000 1.55 +++ QueryParser.jj 14 Nov 2004 14:00:29 -0000 @@ -32,6 +32,7 @@ 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 @@ -85,8 +86,13 @@ public static final int DEFAULT_OPERATOR_OR = 0; 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 +103,14 @@ 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 +217,48 @@ * capital OR of OR Hungary.
* In DEFAULT_OPERATOR_AND terms are considered to be in conjuction: the * above mentioned query is parsed as capital AND of AND Hungary + * @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 (OR_OPERATOR) terms without any modifiers + * are considered optional: for example capital of Hungary is equal to + * capital OR of OR Hungary.
+ * In AND_OPERATOR terms are considered to be in conjuction: the + * above mentioned query is parsed as capital AND of AND Hungary */ - 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 +302,7 @@ 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 +317,7 @@ 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);