You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2014/11/27 12:18:07 UTC

svn commit: r1642088 - in /jackrabbit/oak/branches/1.0: ./ oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/ oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/

Author: chetanm
Date: Thu Nov 27 11:18:07 2014
New Revision: 1642088

URL: http://svn.apache.org/r1642088
Log:
OAK-2190: Support for like query for string
Merging revisions 1638705

Modified:
    jackrabbit/oak/branches/1.0/   (props changed)
    jackrabbit/oak/branches/1.0/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
    jackrabbit/oak/branches/1.0/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java

Propchange: jackrabbit/oak/branches/1.0/
------------------------------------------------------------------------------
  Merged /jackrabbit/oak/trunk:r1638705

Modified: jackrabbit/oak/branches/1.0/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java?rev=1642088&r1=1642087&r2=1642088&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java (original)
+++ jackrabbit/oak/branches/1.0/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java Thu Nov 27 11:18:07 2014
@@ -647,28 +647,7 @@ public class LucenePropertyIndex impleme
             }
 
             if (isLike) {
-                first = first.replace('%', WildcardQuery.WILDCARD_STRING);
-                first = first.replace('_', WildcardQuery.WILDCARD_CHAR);
-
-                int indexOfWS = first.indexOf(WildcardQuery.WILDCARD_STRING);
-                int indexOfWC = first.indexOf(WildcardQuery.WILDCARD_CHAR);
-                int len = first.length();
-
-                if (indexOfWS == len || indexOfWC == len) {
-                    // remove trailing "*" for prefixquery
-                    first = first.substring(0, first.length() - 1);
-                    if (JCR_PATH.equals(name)) {
-                        qs.add(new PrefixQuery(newPathTerm(first)));
-                    } else {
-                        qs.add(new PrefixQuery(new Term(name, first)));
-                    }
-                } else {
-                    if (JCR_PATH.equals(name)) {
-                        qs.add(new WildcardQuery(newPathTerm(first)));
-                    } else {
-                        qs.add(new WildcardQuery(new Term(name, first)));
-                    }
-                }
+                qs.add(createLikeQuery(name, first));
                 continue;
             }
 
@@ -702,6 +681,31 @@ public class LucenePropertyIndex impleme
         return defaultVal;
     }
 
+    private static Query createLikeQuery(String name, String first) {
+        first = first.replace('%', WildcardQuery.WILDCARD_STRING);
+        first = first.replace('_', WildcardQuery.WILDCARD_CHAR);
+
+        int indexOfWS = first.indexOf(WildcardQuery.WILDCARD_STRING);
+        int indexOfWC = first.indexOf(WildcardQuery.WILDCARD_CHAR);
+        int len = first.length();
+
+        if (indexOfWS == len || indexOfWC == len) {
+            // remove trailing "*" for prefixquery
+            first = first.substring(0, first.length() - 1);
+            if (JCR_PATH.equals(name)) {
+                return new PrefixQuery(newPathTerm(first));
+            } else {
+                return new PrefixQuery(new Term(name, first));
+            }
+        } else {
+            if (JCR_PATH.equals(name)) {
+                return new WildcardQuery(newPathTerm(first));
+            } else {
+                return new WildcardQuery(new Term(name, first));
+            }
+        }
+    }
+
     @CheckForNull
     private static Query createQuery(PropertyRestriction pr,
                                      IndexDefinition defn) {
@@ -796,7 +800,9 @@ public class LucenePropertyIndex impleme
                 break;
             }
             default: {
-                //TODO Support for pr.like
+                if (pr.isLike) {
+                    return createLikeQuery(pr.propertyName, pr.first.getValue(STRING));
+                }
 
                 //TODO Confirm that all other types can be treated as string
                 String first = pr.first != null ? pr.first.getValue(STRING) : null;

Modified: jackrabbit/oak/branches/1.0/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java?rev=1642088&r1=1642087&r2=1642088&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java (original)
+++ jackrabbit/oak/branches/1.0/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java Thu Nov 27 11:18:07 2014
@@ -342,7 +342,6 @@ public class LucenePropertyIndexTest ext
         assertQuery("select [jcr:path] from [nt:base] where propa is not null", asList("/test/a", "/test/b", "/test/c"));
     }
 
-    @Ignore("OAK-2190")
     @Test
     public void likeQueriesWithString() throws Exception {
         Tree idx = createIndex("test1", of("propa", "propb"));
@@ -355,8 +354,12 @@ public class LucenePropertyIndexTest ext
         test.addChild("c").setProperty("propa", "humpy");
         root.commit();
 
-        assertQuery("select [jcr:path] from [nt:base] where propa like 'hum%'", asList("/test/a", "test/c"));
-        assertQuery("select [jcr:path] from [nt:base] where propa like '%ty'", asList("/test/a", "/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where propa like 'hum%'",
+            asList("/test/a", "/test/c"));
+        assertQuery("select [jcr:path] from [nt:base] where propa like '%ty'",
+            asList("/test/a", "/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where propa like '%ump%'",
+            asList("/test/a", "/test/b", "/test/c"));
     }
 
     @Test