Author: catholicon
Date: Sat Sep 10 13:51:58 2016
New Revision: 1760168

URL: http://svn.apache.org/viewvc?rev=1760168&view=rev
Log:
OAK-4705: Fulltext parser doesn't allow stand-alone hyphen in search expression

Allow hyphen to be anywhere. If it comes before a word/quoted-word, then it 
acts as a not for the term that follows it.
Also, it seemed that hyphen followed by quoted term wasn't being parse 
correctly (the quotes didn't fulfil their purpose) - fixed that
Added a bunch of parsing tests

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextParser.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/ast/FullTextTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextParser.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextParser.java?rev=1760168&r1=1760167&r2=1760168&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextParser.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextParser.java
 Sat Sep 10 13:51:58 2016
@@ -85,10 +85,9 @@ public class FullTextParser {
         boolean not = false;
         StringBuilder buff = new StringBuilder();
         char c = text.charAt(parseIndex);
-        if (c == '-') {
-            if (++parseIndex >= text.length()) {
-                throw getSyntaxError("term");
-            }
+        if (c == '-' && parseIndex < text.length() - 1 &&
+                text.charAt(parseIndex + 1) != ' ') {
+            c = text.charAt(++parseIndex);
             not = true;
         }
         boolean escaped = false;
@@ -205,4 +204,4 @@ public class FullTextParser {
         return new ParseException("FullText expression: " + query, index);
     }
 
-}
\ No newline at end of file
+}

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/ast/FullTextTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/ast/FullTextTest.java?rev=1760168&r1=1760167&r2=1760168&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/ast/FullTextTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/ast/FullTextTest.java
 Sat Sep 10 13:51:58 2016
@@ -54,6 +54,7 @@ public class FullTextTest {
         assertFalse(test("hello world", "world"));
         assertTrue(test("hello world", "world hello"));
         assertTrue(test("hello world ", "hello world"));
+        assertEquals("\"hello\"", convertPattern("hello hello"));
     }
 
     @Test
@@ -65,7 +66,33 @@ public class FullTextTest {
     }
 
     @Test
+    public void minusLiteral() throws ParseException {
+        assertEquals("\"-\"", convertPattern("-"));
+        assertEquals("\"-\"", convertPattern("- "));
+        assertEquals("\"-\"", convertPattern("- -"));
+        assertEquals("\"-\" \"hello\"", convertPattern("- hello"));
+        assertEquals("\"-\" \"hello\" \"world\"", convertPattern("hello - 
world"));
+        assertEquals("\"-\" \"hello\" \"world\"", convertPattern("hello  -  
world"));
+        assertEquals("\"-\" \"hello\"", convertPattern("hello -"));
+
+        assertTrue(test("-", "hello - world"));
+        assertTrue(test("- ", "hello - world"));
+        assertTrue(test("- -", "hello - world"));
+        assertTrue(test("- hello", "hello - world"));
+        assertTrue(test("hello - world", "hello - world"));
+        assertTrue(test("hello - \"wonderful world\"", "hello - wonderful 
world"));
+        assertTrue(test("hello -", "hello -"));
+    }
+
+    @Test
     public void not() throws ParseException {
+        assertEquals("\"hello\" -\"wonderful world\"", convertPattern("hello 
-\"wonderful world\""));
+        assertTrue(test("hello -\"wonderful world\"", "hello"));
+        assertTrue(test("hello -\"wonderful world\"", "hello wonderful"));
+        assertTrue(test("hello -\"wonderful world\"", "hello world"));
+        assertFalse(test("hello -\"wonderful world\"", "hello wonderful 
world"));
+        assertFalse(test("hello -\"wonderful world\"", "wonderful world"));
+        assertTrue(test("hello \"-wonderful world\"", "hello this beautiful 
-wonderful world"));
         assertEquals("\"hello\" -\"world\"", convertPattern("hello -world"));
         assertTrue(test("hello -world", "hello"));
         assertFalse(test("hello -world", "hello world"));
@@ -107,8 +134,6 @@ public class FullTextTest {
         testInvalid("", "(*); expected: term");
         testInvalid("x OR ", "x OR(*); expected: term");
         testInvalid("\"", "(*)\"; expected: double quote");
-        testInvalid("-", "(*)-; expected: term");
-        testInvalid("- x", "- (*)x; expected: term");
         testInvalid("\\", "(*)\\; expected: escaped char");
         testInvalid("\"\\", "\"(*)\\; expected: escaped char");
         testInvalid("\"x\"y", "\"x\"(*)y; expected: space");


Reply via email to