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/25 12:56:18 UTC

svn commit: r1641597 - in /jackrabbit/oak/trunk/oak-lucene/src: main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java

Author: chetanm
Date: Tue Nov 25 11:56:17 2014
New Revision: 1641597

URL: http://svn.apache.org/r1641597
Log:
OAK-2278 - Make current testcases pass with new index implementation

Some test with query like _where [prop] = 1_ were failing because the default Lucene config created for running test did not explicitly define the property type due to which property is treated as string and comparison fails as the index value is long.

As a fix LucenePropertyIndex would try to determine the property type from values specified in PropertyRestriction

Modified:
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
    jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java?rev=1641597&r1=1641596&r2=1641597&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java Tue Nov 25 11:56:17 2014
@@ -651,6 +651,22 @@ public class LucenePropertyIndex impleme
         }
     }
 
+    private static int determinePropertyType(PropertyDefinition defn, PropertyRestriction pr) {
+        int typeFromRestriction = pr.propertyType;
+        if (typeFromRestriction == PropertyType.UNDEFINED) {
+            //If no explicit type defined then determine the type from restriction
+            //value
+            if (pr.first != null && pr.first.getType() != Type.UNDEFINED) {
+                typeFromRestriction = pr.first.getType().tag();
+            } else if (pr.last != null && pr.last.getType() != Type.UNDEFINED) {
+                typeFromRestriction = pr.last.getType().tag();
+            } else if (pr.list != null && !pr.list.isEmpty()){
+                typeFromRestriction = pr.list.get(0).getType().tag();
+            }
+        }
+        return getPropertyType(defn, pr.propertyName, typeFromRestriction);
+    }
+
     private static int getPropertyType(PropertyDefinition defn, String name, int defaultVal){
         if (defn.isTypeDefined()) {
             return defn.getType();
@@ -690,7 +706,7 @@ public class LucenePropertyIndex impleme
     @CheckForNull
     private static Query createQuery(PropertyRestriction pr,
                                      PropertyDefinition defn) {
-        int propType = getPropertyType(defn, pr.propertyName, pr.propertyType);
+        int propType = determinePropertyType(defn, pr);
         switch (propType) {
             case PropertyType.DATE: {
                 Long first = pr.first != null ? FieldFactory.dateToLong(pr.first.getValue(Type.DATE)) : null;

Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java?rev=1641597&r1=1641596&r2=1641597&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java Tue Nov 25 11:56:17 2014
@@ -39,7 +39,6 @@ import org.apache.jackrabbit.oak.api.Con
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
-import org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneInitializerHelper;
 import org.apache.jackrabbit.oak.plugins.index.nodetype.NodeTypeIndexProvider;
 import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
 import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
@@ -269,6 +268,34 @@ public class LucenePropertyIndexTest ext
     }
 
     @Test
+    public void determinePropTypeFromRestriction() throws Exception{
+        Tree idx = createIndex("test1", of("propa", "propb"));
+        //Do not provide type information
+        root.commit();
+
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("propa", 10);
+        test.addChild("b").setProperty("propa", 20);
+        test.addChild("c").setProperty("propa", 30);
+        test.addChild("c").setProperty("propb", "foo");
+        test.addChild("d").setProperty("propb", "foo");
+        root.commit();
+
+        assertThat(explain("select [jcr:path] from [nt:base] where [propa] >= 20"), containsString("lucene:test1"));
+
+        assertQuery("select [jcr:path] from [nt:base] where [propa] >= 20", asList("/test/b", "/test/c"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] >= 20", asList("/test/b", "/test/c"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] = 20", asList("/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] <= 20", asList("/test/b", "/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] < 20", asList("/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] = 20 or [propa] = 10 ", asList("/test/b", "/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] > 10 and [propa] < 30", asList("/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] in (10,20)", asList("/test/b", "/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where propa is not null", asList("/test/a", "/test/b", "/test/c"));
+
+    }
+
+    @Test
     public void rangeQueriesWithDouble() throws Exception {
         Tree idx = createIndex("test1", of("propa", "propb"));
         Tree propIdx = idx.addChild(PROP_NODE).addChild("propa");