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 da...@apache.org on 2015/10/01 11:59:35 UTC

svn commit: r1706212 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/ oak-l...

Author: davide
Date: Thu Oct  1 09:59:34 2015
New Revision: 1706212

URL: http://svn.apache.org/viewvc?rev=1706212&view=rev
Log:
OAK-3371 - Wrong evaluation of NOT clause

- package version
- test coverage
- fix to the issue

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextSearchImpl.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElementFactory.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextContains.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextExpression.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/package-info.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndexQueryTest.java
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndex.java
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/package-info.java
    jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexAggregationTest.java
    jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElementFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElementFactory.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElementFactory.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AstElementFactory.java Thu Oct  1 09:59:34 2015
@@ -104,6 +104,9 @@ public class AstElementFactory {
     }
 
     public NotImpl not(ConstraintImpl constraint) {
+        if (constraint instanceof FullTextSearchImpl) {
+            return new NotFullTextImpl((FullTextSearchImpl) constraint);
+        }
         return new NotImpl(constraint);
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/FullTextSearchImpl.java Thu Oct  1 09:59:34 2015
@@ -50,10 +50,10 @@ public class FullTextSearchImpl extends
      */
     public static final boolean JACKRABBIT_2_SINGLE_QUOTED_PHRASE = true;
 
-    private final String selectorName;
+    final String selectorName;
     private final String relativePath;
-    private final String propertyName;
-    private final StaticOperandImpl fullTextSearchExpression;
+    final String propertyName;
+    final StaticOperandImpl fullTextSearchExpression;
     private SelectorImpl selector;
 
     public FullTextSearchImpl(
@@ -139,7 +139,7 @@ public class FullTextSearchImpl extends
                 p = PathUtils.concat(relativePath, p);
             }
             String p2 = normalizePropertyName(p);
-            String rawText = v.getValue(Type.STRING);
+            String rawText = getRawText(v);
             FullTextExpression e = FullTextParser.parse(p2, rawText);
             return new FullTextContains(p2, rawText, e);
         } catch (ParseException e) {
@@ -147,11 +147,30 @@ public class FullTextSearchImpl extends
         }
     }
     
+    String getRawText(PropertyValue v) {
+        return v.getValue(Type.STRING);
+    }
+    
     @Override
     public Set<SelectorImpl> getSelectors() {
         return Collections.singleton(selector);
     }
 
+    /**
+     * verify that a property exists in the node. {@code property IS NOT NULL}
+     * 
+     * @param propertyName the property to check
+     * @param selector the selector to work with
+     * @return true if the property is there, false otherwise.
+     */
+    boolean enforcePropertyExistence(String propertyName, SelectorImpl selector) {
+        PropertyValue p = selector.currentProperty(propertyName);
+        if (p == null) {
+            return false;
+        }
+        return true;
+    }
+    
     @Override
     public boolean evaluate() {
         // disable evaluation if a fulltext index is used,
@@ -163,10 +182,7 @@ public class FullTextSearchImpl extends
             // condition checks out, this takes out some extra rows from the index
             // aggregation bits
             if (relativePath == null && propertyName != null) {
-                PropertyValue p = selector.currentProperty(propertyName);
-                if (p == null) {
-                    return false;
-                }
+                return enforcePropertyExistence(propertyName, selector);
             }
             return true;
         }
@@ -259,7 +275,7 @@ public class FullTextSearchImpl extends
                     p = PathUtils.concat(p, relativePath);
                 }                
                 p = normalizePropertyName(p);
-                f.restrictProperty(p, Operator.NOT_EQUAL, null);
+                restrictPropertyOnFilter(p, f);
             }
         }
         f.restrictFulltextCondition(fullTextSearchExpression.currentValue().getValue(Type.STRING));
@@ -272,4 +288,14 @@ public class FullTextSearchImpl extends
         }
     }
 
+    /**
+     * restrict the provided property to the property to the provided filter achieving so
+     * {@code property IS NOT NULL}
+     * 
+     * @param propertyName
+     * @param f
+     */
+    void restrictPropertyOnFilter(String propertyName, FilterImpl f) {
+        f.restrictProperty(propertyName, Operator.NOT_EQUAL, null);
+    }
 }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextImpl.java?rev=1706212&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextImpl.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextImpl.java Thu Oct  1 09:59:34 2015
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.query.ast;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.query.fulltext.FullTextExpression;
+import org.apache.jackrabbit.oak.query.index.FilterImpl;
+
+/**
+ * class used to "wrap" a {@code NOT CONTAINS} clause. The main differences with a {@link NotImpl}
+ * reside in the {@link NotImpl#evaluate()} and restricts.
+ */
+public class NotFullTextImpl extends NotImpl {
+    public NotFullTextImpl(@Nonnull FullTextSearchImpl constraint) {
+        super(new NotFullTextSearchImpl(checkNotNull(constraint)));
+    }
+
+    @Override
+    public boolean evaluate() {
+        return getConstraint().evaluate();
+    }
+
+    @Override
+    public void restrict(FilterImpl f) {
+        if (!f.getSelector().isOuterJoinRightHandSide()) {
+            getConstraint().restrict(f);
+        }
+    }
+
+    @Override
+    public void restrictPushDown(SelectorImpl s) {
+        getConstraint().restrictPushDown(s);    
+    }
+    
+    @Override
+    public FullTextExpression getFullTextConstraint(SelectorImpl s) {
+        return getConstraint().getFullTextConstraint(s);
+    }
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextSearchImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextSearchImpl.java?rev=1706212&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextSearchImpl.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/NotFullTextSearchImpl.java Thu Oct  1 09:59:34 2015
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.query.ast;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Set;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.PropertyValue;
+import org.apache.jackrabbit.oak.query.index.FilterImpl;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
+
+public class NotFullTextSearchImpl extends FullTextSearchImpl {
+    private static final Set<String> KEYWORDS = ImmutableSet.of("or");
+    private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
+
+    public NotFullTextSearchImpl(String selectorName, String propertyName,
+                                 StaticOperandImpl fullTextSearchExpression) {
+        super(selectorName, propertyName, fullTextSearchExpression);
+    }
+
+    public NotFullTextSearchImpl(FullTextSearchImpl ft) {
+        this(ft.selectorName, ft.propertyName, ft.fullTextSearchExpression);
+    }
+    
+    @Override
+    String getRawText(PropertyValue v) {
+        Iterable<String> terms = SPACE_SPLITTER.split(super.getRawText(v));
+        StringBuffer raw = new StringBuffer();
+        for (String term : terms) {
+            if (isKeyword(term)) {
+                raw.append(String.format("%s ", term));
+            } else {
+                raw.append(String.format("-%s ", term));
+            }
+        }
+        return raw.toString().trim();
+    }
+
+    private boolean isKeyword(@Nonnull String term) {
+        return KEYWORDS.contains(checkNotNull(term).toLowerCase());
+    }
+    
+    @Override
+    void restrictPropertyOnFilter(String propertyName, FilterImpl f) {
+        // Intentionally left empty. A NOT CONTAINS() can be valid if the property is actually not
+        // there.
+    }
+
+    @Override
+    public String toString() {
+        return "not " + super.toString();
+    }
+
+    @Override
+    boolean enforcePropertyExistence(String propertyName, SelectorImpl selector) {
+        // in case of NOT CONTAINS we want to match nodes without the property as well. In this way
+        // we don't care whether the property is there or not.
+        return true;
+    }
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextContains.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextContains.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextContains.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextContains.java Thu Oct  1 09:59:34 2015
@@ -75,4 +75,8 @@ public class FullTextContains extends Fu
         return rawText;
     }
 
+    @Override
+    public boolean isNot() {
+        return base.isNot();
+    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextExpression.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextExpression.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextExpression.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/FullTextExpression.java Thu Oct  1 09:59:34 2015
@@ -89,4 +89,13 @@ public abstract class FullTextExpression
      */
     public abstract boolean accept(FullTextVisitor v);
     
+    /**
+     * whether the current {@link FullTextExpression} is a {@code NOT} condition or not. Default is
+     * false
+     * 
+     * @return true if the current condition represent a NOT, false otherwise.
+     */
+    public boolean isNot() {
+        return false;
+    }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/package-info.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/package-info.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/fulltext/package-info.java Thu Oct  1 09:59:34 2015
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.0")
+@Version("1.1")
 @Export(optional = "provide:=true")
 package org.apache.jackrabbit.oak.query.fulltext;
 

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndexQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndexQueryTest.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndexQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndexQueryTest.java Thu Oct  1 09:59:34 2015
@@ -20,8 +20,14 @@ import static com.google.common.collect.
 import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
 import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
 import static org.apache.jackrabbit.oak.plugins.index.IndexUtils.createIndexDefinition;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.NT_UNSTRUCTURED;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.List;
+
+import javax.jcr.query.Query;
 
 import org.apache.jackrabbit.oak.Oak;
 import org.apache.jackrabbit.oak.api.ContentRepository;
@@ -48,8 +54,10 @@ public class NodeTypeIndexQueryTest exte
                 .createContentRepository();
     }
 
-    private static void child(Tree t, String n, String type) {
-        t.addChild(n).setProperty(JCR_PRIMARYTYPE, type, Type.NAME);
+    private static Tree child(Tree t, String n, String type) {
+        Tree t1 = t.addChild(n);
+        t1.setProperty(JCR_PRIMARYTYPE, type, Type.NAME);
+        return t1;
     }
 
     private static void mixLanguage(Tree t, String n) {
@@ -84,4 +92,33 @@ public class NodeTypeIndexQueryTest exte
 
         setTraversalEnabled(true);
     }
+    
+    @Test
+    public void oak3371() throws Exception {
+        setTraversalEnabled(false);
+        Tree t, t1;
+
+        NodeUtil n = new NodeUtil(root.getTree("/oak:index"));
+        createIndexDefinition(n, "nodeType", false, new String[] {
+                JCR_PRIMARYTYPE }, new String[] { NT_UNSTRUCTURED });
+
+        root.commit();
+        
+        t = root.getTree("/");
+        t = child(t, "test", NT_UNSTRUCTURED);
+        t1 = child(t, "a", NT_UNSTRUCTURED);
+        t1.setProperty("foo", "bar");
+        child(t, "b", NT_UNSTRUCTURED);
+        
+        root.commit();
+        
+        List<String> plan = executeQuery(
+            "explain SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE([/test]) AND NOT CONTAINS(foo, 'bar')",
+            Query.JCR_SQL2, false);
+        
+        assertEquals(1, plan.size());
+        assertTrue(plan.get(0).contains("no-index"));
+        
+        setTraversalEnabled(true);
+    }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndex.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndex.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndex.java Thu Oct  1 09:59:34 2015
@@ -70,7 +70,6 @@ import org.apache.jackrabbit.oak.spi.que
 import org.apache.jackrabbit.oak.spi.query.IndexRow;
 import org.apache.jackrabbit.oak.spi.query.PropertyValues;
 import org.apache.jackrabbit.oak.spi.query.QueryConstants;
-import org.apache.jackrabbit.oak.spi.query.QueryIndex;
 import org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvanceFulltextQueryIndex;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.lucene.analysis.Analyzer;
@@ -594,14 +593,8 @@ public class LuceneIndex implements Adva
         if (qs.size() == 0) {
             return new LuceneRequestFacade<Query>(new MatchAllDocsQuery());
         }
-        if (qs.size() == 1) {
-            return new LuceneRequestFacade<Query>(qs.get(0));
-        }
-        BooleanQuery bq = new BooleanQuery();
-        for (Query q : qs) {
-            bq.add(q, MUST);
-        }
-        return new LuceneRequestFacade<Query>(bq);
+        
+        return LucenePropertyIndex.performAdditionalWraps(qs);
     }
 
     private static void addNonFullTextConstraints(List<Query> qs,

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=1706212&r1=1706211&r2=1706212&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 Thu Oct  1 09:59:34 2015
@@ -29,6 +29,7 @@ import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.jcr.PropertyType;
 
@@ -37,6 +38,7 @@ import com.google.common.collect.Iterabl
 import com.google.common.collect.Lists;
 import com.google.common.collect.Queues;
 import com.google.common.collect.Sets;
+
 import org.apache.jackrabbit.oak.api.PropertyValue;
 import org.apache.jackrabbit.oak.api.Result.SizePrecision;
 import org.apache.jackrabbit.oak.api.Type;
@@ -102,6 +104,7 @@ import org.apache.lucene.util.Version;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.Lists.newArrayListWithCapacity;
 import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
@@ -661,16 +664,73 @@ public class LucenePropertyIndex impleme
 
             throw new IllegalStateException("No query created for filter " + filter);
         }
+        return performAdditionalWraps(qs);
+    }
+
+    /**
+     * Perform additional wraps on the list of queries to allow, for example, the NOT CONTAINS to
+     * play properly when sent to lucene.
+     * 
+     * @param qs the list of queries. Cannot be null.
+     * @return
+     */
+    @Nonnull
+    public static LuceneRequestFacade<Query> performAdditionalWraps(@Nonnull List<Query> qs) {
+        checkNotNull(qs);
         if (qs.size() == 1) {
+            Query q = qs.get(0);
+            if (q instanceof BooleanQuery) {
+                BooleanQuery ibq = (BooleanQuery) q;
+                boolean onlyNotClauses = true;
+                for (BooleanClause c : ibq.getClauses()) {
+                    if (c.getOccur() != BooleanClause.Occur.MUST_NOT) {
+                        onlyNotClauses = false;
+                        break;
+                    }
+                }
+                if (onlyNotClauses) {
+                    // if we have only NOT CLAUSES we have to add a match all docs (*.*) for the
+                    // query to work
+                    ibq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
+                }
+            }
             return new LuceneRequestFacade<Query>(qs.get(0));
         }
         BooleanQuery bq = new BooleanQuery();
         for (Query q : qs) {
-            bq.add(q, MUST);
+            boolean unwrapped = false;
+            if (q instanceof BooleanQuery) {
+                unwrapped = unwrapMustNot((BooleanQuery) q, bq);
+            }
+
+            if (!unwrapped) {
+                bq.add(q, MUST);                
+            }
         }
         return new LuceneRequestFacade<Query>(bq);
     }
 
+    /**
+     * unwraps any NOT clauses from the provided boolean query into another boolean query.
+     * 
+     * @param input the query to be analysed for the existence of NOT clauses. Cannot be null.
+     * @param output the query where the unwrapped NOTs will be saved into. Cannot be null.
+     * @return true if there where at least one unwrapped NOT. false otherwise.
+     */
+    private static boolean unwrapMustNot(@Nonnull BooleanQuery input, @Nonnull BooleanQuery output) {
+        checkNotNull(input);
+        checkNotNull(output);
+        boolean unwrapped = false;
+        for (BooleanClause bc : input.getClauses()) {
+            if (bc.getOccur() == BooleanClause.Occur.MUST_NOT) {
+                output.add(bc);
+                unwrapped = true;
+            }
+        }
+        
+        return unwrapped;
+    }
+    
     private CustomScoreQuery getCustomScoreQuery(IndexPlan plan, Query subQuery) {
         PlanResult planResult = getPlanResult(plan);
         IndexDefinition idxDef = planResult.indexDefinition;
@@ -1035,7 +1095,7 @@ public class LucenePropertyIndex impleme
 
             @Override
             public boolean visit(FullTextContains contains) {
-                visitTerm(contains.getPropertyName(), contains.getRawText(), null, false);
+                visitTerm(contains.getPropertyName(), contains.getRawText(), null, contains.isNot());
                 return true;
             }
 

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/package-info.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/package-info.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/package-info.java Thu Oct  1 09:59:34 2015
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("2.5.0")
+@Version("2.6.0")
 @Export(optional = "provide:=true")
 package org.apache.jackrabbit.oak.plugins.index.lucene;
 

Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexAggregationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexAggregationTest.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexAggregationTest.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexAggregationTest.java Thu Oct  1 09:59:34 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.lucene;
 
+import static com.google.common.collect.ImmutableList.of;
 import static com.google.common.collect.Lists.newArrayList;
 import static org.apache.jackrabbit.JcrConstants.JCR_CONTENT;
 import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
@@ -28,15 +29,19 @@ import java.util.Calendar;
 
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.ContentRepository;
 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.aggregate.NodeAggregator;
 import org.apache.jackrabbit.oak.plugins.index.aggregate.SimpleNodeAggregator;
 
 import static org.apache.jackrabbit.oak.plugins.index.lucene.TestUtil.newNodeAggregator;
 import static org.apache.jackrabbit.oak.plugins.index.lucene.TestUtil.useV2;
 import static org.apache.jackrabbit.oak.plugins.memory.BinaryPropertyState.binaryProperty;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent;
 import org.apache.jackrabbit.oak.query.AbstractQueryTest;
@@ -412,4 +417,55 @@ public class LuceneIndexAggregationTest
                     "xpath", ImmutableList.of("/myFolder", "/myFolder/myFile", "/myFolder/myFile/jcr:content"));
     }
 
+    @Test
+    public void oak3371AggregateV2() throws CommitFailedException {
+        oak3371();
+    }
+
+    @Test
+    public void oak3371AggregateV1() throws CommitFailedException {
+        
+        Tree indexdef = root.getTree("/oak:index/" + TEST_INDEX_NAME);
+        assertNotNull(indexdef);
+        assertTrue(indexdef.exists());
+        indexdef.setProperty(LuceneIndexConstants.COMPAT_MODE, 1L);
+        indexdef.setProperty(IndexConstants.REINDEX_PROPERTY_NAME, true);
+        root.commit();
+        
+        oak3371();
+    }
+
+    private void oak3371() throws CommitFailedException {
+        setTraversalEnabled(false);
+        Tree test, t;
+        
+        test = root.getTree("/").addChild("test");
+        t = test.addChild("a");
+        t.setProperty(JCR_PRIMARYTYPE, NT_FOLDER, Type.NAME);
+        t.setProperty("foo", "bar");
+        t = test.addChild("b");
+        t.setProperty(JCR_PRIMARYTYPE, NT_FOLDER, Type.NAME);
+        t.setProperty("foo", "cat");
+        t = test.addChild("c");
+        t.setProperty(JCR_PRIMARYTYPE, NT_FOLDER, Type.NAME);
+        t = test.addChild("d");
+        t.setProperty(JCR_PRIMARYTYPE, NT_FOLDER, Type.NAME);
+        t.setProperty("foo", "bar cat");
+        root.commit();
+        
+        assertQuery(
+            "SELECT * FROM [nt:folder] WHERE ISDESCENDANTNODE('/test') AND CONTAINS(foo, 'bar')",
+            of("/test/a", "/test/d"));
+        assertQuery(
+            "SELECT * FROM [nt:folder] WHERE ISDESCENDANTNODE('/test') AND NOT CONTAINS(foo, 'bar')",
+            of("/test/b", "/test/c"));
+        assertQuery(
+            "SELECT * FROM [nt:folder] WHERE ISDESCENDANTNODE('/test') AND CONTAINS(foo, 'bar cat')",
+            of("/test/d"));
+        assertQuery(
+            "SELECT * FROM [nt:folder] WHERE ISDESCENDANTNODE('/test') AND NOT CONTAINS(foo, 'bar cat')",
+            of("/test/c"));
+
+        setTraversalEnabled(true);
+    }
 }

Modified: jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java?rev=1706212&r1=1706211&r2=1706212&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexQueryTest.java Thu Oct  1 09:59:34 2015
@@ -514,4 +514,46 @@ public class LuceneIndexQueryTest extend
             walktree(t1);
         }
     }
+    
+    private static Tree child(Tree t, String n, String type) {
+        Tree t1 = t.addChild(n);
+        t1.setProperty(JCR_PRIMARYTYPE, type, Type.NAME);
+        return t1;
+    }
+
+    @Test
+    public void oak3371() throws Exception {
+        setTraversalEnabled(false);
+        Tree t, t1;
+        
+        t = root.getTree("/");
+        t = child(t, "test", NT_UNSTRUCTURED);
+        t1 = child(t, "a", NT_UNSTRUCTURED);
+        t1.setProperty("foo", "bar");
+        t1 = child(t, "b", NT_UNSTRUCTURED);
+        t1.setProperty("foo", "cat");
+        t1 = child(t, "c", NT_UNSTRUCTURED);
+        t1 = child(t, "d", NT_UNSTRUCTURED);
+        t1.setProperty("foo", "bar cat");
+
+        root.commit();
+
+        assertQuery(
+            "SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/test') AND CONTAINS(foo, 'bar')",
+            of("/test/a", "/test/d"));
+
+        assertQuery(
+            "SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/test') AND NOT CONTAINS(foo, 'bar')",
+            of("/test/b", "/test/c"));
+
+        assertQuery(
+            "SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/test') AND CONTAINS(foo, 'bar cat')",
+            of("/test/d"));
+
+        assertQuery(
+            "SELECT * FROM [nt:unstructured] WHERE ISDESCENDANTNODE('/test') AND NOT CONTAINS(foo, 'bar cat')",
+            of("/test/c"));
+
+        setTraversalEnabled(true);
+    }
 }