LUCENE-6249: StandardQueryParser doesn't support pure negative clauses.

LUCENE-6248: Remove unused odd constants from StandardSyntaxParser.jj 



git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1660315 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2015-02-17 08:20:15 +00:00
parent f89cab00d1
commit 274e24afed
4 changed files with 91 additions and 111 deletions

View File

@ -50,6 +50,9 @@ New Features
Bug Fixes
* LUCENE-6249: StandardQueryParser doesn't support pure negative clauses.
(Dawid Weiss)
* LUCENE-6190: Spatial pointsOnly flag on PrefixTreeStrategy shouldn't switch all predicates to
Intersects. (David Smiley)
@ -125,6 +128,9 @@ API Changes
Other
* LUCENE-6248: Remove unused odd constants from StandardSyntaxParser.jj
(Dawid Weiss)
* LUCENE-6193: Collapse identical catch branches in try-catch statements.
(shalin)

View File

@ -20,6 +20,7 @@ package org.apache.lucene.queryparser.flexible.standard.parser;
import java.io.StringReader;
import java.util.Vector;
import java.util.Arrays;
import org.apache.lucene.queryparser.flexible.messages.Message;
import org.apache.lucene.queryparser.flexible.messages.MessageImpl;
@ -45,10 +46,6 @@ import org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode;
*/
public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserConstants {
private static final int CONJ_NONE =0;
private static final int CONJ_AND =2;
private static final int CONJ_OR =2;
// syntax parser constructor
public StandardSyntaxParser() {
@ -78,36 +75,6 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
}
}
// * Query ::= ( Clause )*
// * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
final public int Conjunction() throws ParseException {
int ret = CONJ_NONE;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
case OR:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
jj_consume_token(AND);
ret = CONJ_AND;
break;
case OR:
jj_consume_token(OR);
ret = CONJ_OR;
break;
default:
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[1] = jj_gen;
;
}
{if (true) return ret;}
throw new Error("Missing return statement in function");
}
final public ModifierQueryNode.Modifier Modifiers() throws ParseException {
ModifierQueryNode.Modifier ret = ModifierQueryNode.Modifier.MOD_NONE;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@ -128,13 +95,13 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
ret = ModifierQueryNode.Modifier.MOD_NOT;
break;
default:
jj_la1[2] = jj_gen;
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[3] = jj_gen;
jj_la1[1] = jj_gen;
;
}
{if (true) return ret;}
@ -180,12 +147,12 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
;
break;
default:
jj_la1[4] = jj_gen;
jj_la1[2] = jj_gen;
break label_1;
}
c = DisjQuery(field);
if (clauses == null) {
clauses = new Vector<>();
clauses = new Vector<QueryNode>();
clauses.addElement(first);
}
clauses.addElement(c);
@ -193,6 +160,15 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
if (clauses != null) {
{if (true) return new BooleanQueryNode(clauses);}
} else {
// Handle the case of a "pure" negation query which
// needs to be wrapped as a boolean query, otherwise
// the returned result drops the negation.
if (first instanceof ModifierQueryNode) {
ModifierQueryNode m = (ModifierQueryNode) first;
if (m.getModifier() == ModifierQueryNode.Modifier.MOD_NOT) {
{if (true) return new BooleanQueryNode(Arrays.<QueryNode> asList(m));}
}
}
{if (true) return first;}
}
throw new Error("Missing return statement in function");
@ -209,13 +185,13 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
;
break;
default:
jj_la1[5] = jj_gen;
jj_la1[3] = jj_gen;
break label_2;
}
jj_consume_token(OR);
c = ConjQuery(field);
if (clauses == null) {
clauses = new Vector<>();
clauses = new Vector<QueryNode>();
clauses.addElement(first);
}
clauses.addElement(c);
@ -239,13 +215,13 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
;
break;
default:
jj_la1[6] = jj_gen;
jj_la1[4] = jj_gen;
break label_3;
}
jj_consume_token(AND);
c = ModClause(field);
if (clauses == null) {
clauses = new Vector<>();
clauses = new Vector<QueryNode>();
clauses.addElement(first);
}
clauses.addElement(c);
@ -328,7 +304,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_consume_token(OP_EQUAL);
break;
default:
jj_la1[7] = jj_gen;
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -353,7 +329,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
operator = jj_consume_token(OP_MORETHANEQ);
break;
default:
jj_la1[8] = jj_gen;
jj_la1[6] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -369,7 +345,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
term = jj_consume_token(NUMBER);
break;
default:
jj_la1[9] = jj_gen;
jj_la1[7] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -420,7 +396,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
q = new TermRangeQueryNode(qLower, qUpper, lowerInclusive, upperInclusive);
break;
default:
jj_la1[10] = jj_gen;
jj_la1[8] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -443,7 +419,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_consume_token(OP_EQUAL);
break;
default:
jj_la1[11] = jj_gen;
jj_la1[9] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -470,19 +446,19 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
boost = jj_consume_token(NUMBER);
break;
default:
jj_la1[12] = jj_gen;
jj_la1[10] = jj_gen;
;
}
group=true;
break;
default:
jj_la1[13] = jj_gen;
jj_la1[11] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[14] = jj_gen;
jj_la1[12] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -532,7 +508,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
term = jj_consume_token(NUMBER);
break;
default:
jj_la1[15] = jj_gen;
jj_la1[13] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -542,7 +518,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
fuzzy=true;
break;
default:
jj_la1[16] = jj_gen;
jj_la1[14] = jj_gen;
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@ -555,12 +531,12 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
fuzzy=true;
break;
default:
jj_la1[17] = jj_gen;
jj_la1[15] = jj_gen;
;
}
break;
default:
jj_la1[18] = jj_gen;
jj_la1[16] = jj_gen;
;
}
if (fuzzy) {
@ -590,7 +566,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_consume_token(RANGEEX_START);
break;
default:
jj_la1[19] = jj_gen;
jj_la1[17] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -602,7 +578,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
goop1 = jj_consume_token(RANGE_QUOTED);
break;
default:
jj_la1[20] = jj_gen;
jj_la1[18] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -611,7 +587,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_consume_token(RANGE_TO);
break;
default:
jj_la1[21] = jj_gen;
jj_la1[19] = jj_gen;
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@ -622,7 +598,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
goop2 = jj_consume_token(RANGE_QUOTED);
break;
default:
jj_la1[22] = jj_gen;
jj_la1[20] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -635,7 +611,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_consume_token(RANGEEX_END);
break;
default:
jj_la1[23] = jj_gen;
jj_la1[21] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -645,7 +621,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
boost = jj_consume_token(NUMBER);
break;
default:
jj_la1[24] = jj_gen;
jj_la1[22] = jj_gen;
;
}
if (goop1.kind == RANGE_QUOTED) {
@ -669,7 +645,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
fuzzySlop = jj_consume_token(FUZZY_SLOP);
break;
default:
jj_la1[25] = jj_gen;
jj_la1[23] = jj_gen;
;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@ -678,7 +654,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
boost = jj_consume_token(NUMBER);
break;
default:
jj_la1[26] = jj_gen;
jj_la1[24] = jj_gen;
;
}
int phraseSlop = 0;
@ -696,7 +672,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
}
break;
default:
jj_la1[27] = jj_gen;
jj_la1[25] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -732,27 +708,6 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
finally { jj_save(1, xla); }
}
private boolean jj_3_2() {
if (jj_scan_token(TERM)) return true;
Token xsp;
xsp = jj_scanpos;
if (jj_3R_4()) {
jj_scanpos = xsp;
if (jj_3R_5()) return true;
}
return false;
}
private boolean jj_3R_12() {
if (jj_scan_token(RANGEIN_START)) return true;
return false;
}
private boolean jj_3R_11() {
if (jj_scan_token(REGEXPTERM)) return true;
return false;
}
private boolean jj_3_1() {
if (jj_scan_token(TERM)) return true;
Token xsp;
@ -845,6 +800,27 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
return false;
}
private boolean jj_3_2() {
if (jj_scan_token(TERM)) return true;
Token xsp;
xsp = jj_scanpos;
if (jj_3R_4()) {
jj_scanpos = xsp;
if (jj_3R_5()) return true;
}
return false;
}
private boolean jj_3R_12() {
if (jj_scan_token(RANGEIN_START)) return true;
return false;
}
private boolean jj_3R_11() {
if (jj_scan_token(REGEXPTERM)) return true;
return false;
}
/** Generated Token Manager. */
public StandardSyntaxParserTokenManager token_source;
/** Current token. */
@ -855,7 +831,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
private Token jj_scanpos, jj_lastpos;
private int jj_la;
private int jj_gen;
final private int[] jj_la1 = new int[28];
final private int[] jj_la1 = new int[26];
static private int[] jj_la1_0;
static private int[] jj_la1_1;
static {
@ -863,10 +839,10 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
jj_la1_init_1();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x300,0x300,0x1c00,0x1c00,0x1ec03c00,0x200,0x100,0x18000,0x1e0000,0x10c00000,0x1f8000,0x18000,0x200000,0x1ec02000,0x1ec02000,0x12800000,0x1000000,0x1000000,0x200000,0xc000000,0x0,0x20000000,0x0,0xc0000000,0x200000,0x1000000,0x200000,0x1ec00000,};
jj_la1_0 = new int[] {0x1c00,0x1c00,0x1ec03c00,0x200,0x100,0x18000,0x1e0000,0x10c00000,0x1f8000,0x18000,0x200000,0x1ec02000,0x1ec02000,0x12800000,0x1000000,0x1000000,0x200000,0xc000000,0x0,0x20000000,0x0,0xc0000000,0x200000,0x1000000,0x200000,0x1ec00000,};
}
private static void jj_la1_init_1() {
jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x3,0x0,0x3,0x0,0x0,0x0,0x0,0x0,};
jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x3,0x0,0x3,0x0,0x0,0x0,0x0,0x0,};
}
final private JJCalls[] jj_2_rtns = new JJCalls[2];
private boolean jj_rescan = false;
@ -878,7 +854,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 28; i++) jj_la1[i] = -1;
for (int i = 0; i < 26; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
@ -888,7 +864,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 28; i++) jj_la1[i] = -1;
for (int i = 0; i < 26; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
@ -898,7 +874,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 28; i++) jj_la1[i] = -1;
for (int i = 0; i < 26; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
@ -908,7 +884,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 28; i++) jj_la1[i] = -1;
for (int i = 0; i < 26; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
@ -986,7 +962,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
return (jj_ntk = jj_nt.kind);
}
private java.util.List<int[]> jj_expentries = new java.util.ArrayList<>();
private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
private int[] jj_expentry;
private int jj_kind = -1;
private int[] jj_lasttokens = new int[100];
@ -1025,7 +1001,7 @@ public class StandardSyntaxParser implements SyntaxParser, StandardSyntaxParserC
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 28; i++) {
for (int i = 0; i < 26; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {

View File

@ -32,6 +32,7 @@ package org.apache.lucene.queryparser.flexible.standard.parser;
import java.io.StringReader;
import java.util.Vector;
import java.util.Arrays;
import org.apache.lucene.queryparser.flexible.messages.Message;
import org.apache.lucene.queryparser.flexible.messages.MessageImpl;
@ -57,10 +58,6 @@ import org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode;
*/
public class StandardSyntaxParser implements SyntaxParser {
private static final int CONJ_NONE =0;
private static final int CONJ_AND =2;
private static final int CONJ_OR =2;
// syntax parser constructor
public StandardSyntaxParser() {
@ -149,20 +146,6 @@ PARSER_END(StandardSyntaxParser)
| <RANGE_GOOP: (~[ " ", "]", "}" ])+ >
}
// * Query ::= ( Clause )*
// * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
int Conjunction() : {
int ret = CONJ_NONE;
}
{
[
<AND> { ret = CONJ_AND; }
| <OR> { ret = CONJ_OR; }
]
{ return ret; }
}
ModifierQueryNode.Modifier Modifiers() : {
ModifierQueryNode.Modifier ret = ModifierQueryNode.Modifier.MOD_NONE;
}
@ -220,6 +203,15 @@ QueryNode Query(CharSequence field) :
if (clauses != null) {
return new BooleanQueryNode(clauses);
} else {
// Handle the case of a "pure" negation query which
// needs to be wrapped as a boolean query, otherwise
// the returned result drops the negation.
if (first instanceof ModifierQueryNode) {
ModifierQueryNode m = (ModifierQueryNode) first;
if (m.getModifier() == ModifierQueryNode.Modifier.MOD_NOT) {
return new BooleanQueryNode(Arrays.<QueryNode> asList(m));
}
}
return first;
}
}

View File

@ -532,6 +532,12 @@ public class TestQPHelper extends LuceneTestCase {
assertQueryEquals("term term1 term2", a, "term term1 term2");
}
public void testLeadingNegation() throws Exception {
assertQueryEquals("-term", null, "-term");
assertQueryEquals("!term", null, "-term");
assertQueryEquals("NOT term", null, "-term");
}
public void testWildcard() throws Exception {
assertQueryEquals("term*", null, "term*");
assertQueryEquals("term*^2", null, "term*^2.0");