You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2015/10/28 16:26:18 UTC

svn commit: r1711034 - in /lucene/dev/branches/lucene_solr_4_10: ./ lucene/ lucene/queryparser/ lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/ lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/stand...

Author: dweiss
Date: Wed Oct 28 15:26:18 2015
New Revision: 1711034

URL: http://svn.apache.org/viewvc?rev=1711034&view=rev
Log:
LUCENE-6249: StandardQueryParser doesn't support pure negative clauses. 
LUCENE-6248: Remove unused odd constants from StandardSyntaxParser.jj


Modified:
    lucene/dev/branches/lucene_solr_4_10/   (props changed)
    lucene/dev/branches/lucene_solr_4_10/lucene/   (props changed)
    lucene/dev/branches/lucene_solr_4_10/lucene/CHANGES.txt
    lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/   (props changed)
    lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java
    lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj
    lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java

Modified: lucene/dev/branches/lucene_solr_4_10/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_10/lucene/CHANGES.txt?rev=1711034&r1=1711033&r2=1711034&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_10/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/lucene_solr_4_10/lucene/CHANGES.txt Wed Oct 28 15:26:18 2015
@@ -10,6 +10,14 @@ Bug Fixes
 * LUCENE-6658: Fix IndexUpgrader to also upgrade indexes without any
   segments.  (Trejkaz, Uwe Schindler)
 
+* LUCENE-6249: StandardQueryParser doesn't support pure negative clauses. 
+  (Dawid Weiss)
+
+Other
+
+* LUCENE-6248: Remove unused odd constants from StandardSyntaxParser.jj
+  (Dawid Weiss)
+
 ======================= Lucene 4.10.4 ======================
 
 Bug fixes

Modified: lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java?rev=1711034&r1=1711033&r2=1711034&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java (original)
+++ lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java Wed Oct 28 15:26:18 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.queryparser.fl
 
 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.fle
  */
 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 implem
       }
     }
 
-// *   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 implem
                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 implem
         ;
         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 implem
         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 implem
         ;
         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 implem
         ;
         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 implem
           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 implem
           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 implem
           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 implem
         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 implem
             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 implem
             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 implem
         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 implem
                                 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 implem
                                                          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 implem
         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 implem
         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 implem
         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 implem
         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 implem
         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 implem
         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 implem
         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 implem
         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 implem
          }
       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 implem
     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 implem
     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 implem
   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 implem
       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 implem
     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 implem
     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 implem
     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 implem
     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 implem
       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 implem
       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) {

Modified: lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj?rev=1711034&r1=1711033&r2=1711034&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj (original)
+++ lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj Wed Oct 28 15:26:18 2015
@@ -32,6 +32,7 @@ package org.apache.lucene.queryparser.fl
 
 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.fle
  */
 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;
       }
     }

Modified: lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java?rev=1711034&r1=1711033&r2=1711034&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java (original)
+++ lucene/dev/branches/lucene_solr_4_10/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java Wed Oct 28 15:26:18 2015
@@ -533,6 +533,12 @@ public class TestQPHelper extends Lucene
     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");