jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r829823 - /jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java
Date Mon, 26 Oct 2009 15:20:51 GMT
Author: mreutegg
Date: Mon Oct 26 15:20:50 2009
New Revision: 829823

URL: http://svn.apache.org/viewvc?rev=829823&view=rev
Log:
JCR-2353: Poor performance in range queries using dates

Modified:
    jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java

Modified: jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java?rev=829823&r1=829822&r2=829823&view=diff
==============================================================================
--- jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java
(original)
+++ jackrabbit/branches/1.6/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java
Mon Oct 26 15:20:50 2009
@@ -16,33 +16,43 @@
  */
 package org.apache.jackrabbit.core.query.lucene;
 
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.Weight;
-import org.apache.lucene.search.Searcher;
-import org.apache.lucene.search.Scorer;
-import org.apache.lucene.search.Explanation;
-import org.apache.lucene.search.Similarity;
-import org.apache.lucene.search.ConstantScoreRangeQuery;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.TermEnum;
-import org.apache.lucene.index.TermDocs;
-
 import java.io.IOException;
-import java.util.BitSet;
-import java.util.Map;
-import java.util.List;
 import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.BitSet;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.index.TermEnum;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Explanation;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.Similarity;
+import org.apache.lucene.search.Weight;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
- * Implements a lucene range query.
+ * Implements a variant of the lucene class {@link org.apache.lucene.search.RangeQuery}.
+ * This class does not rewrite to basic {@link org.apache.lucene.search.TermQuery}
+ * but will calculate the matching documents itself. That way a
+ * <code>TooManyClauses</code> can be avoided.
  */
 public class RangeQuery extends Query implements Transformable {
 
     /**
+     * Logger instance for this class.
+     */
+    private static final Logger log = LoggerFactory.getLogger(RangeQuery.class);
+
+    /**
      * The lower term. May be <code>null</code> if <code>upperTerm</code>
is not
      * <code>null</code>.
      */
@@ -66,6 +76,12 @@
     private int transform = TRANSFORM_NONE;
 
     /**
+     * The rewritten range query or <code>null</code> if the range spans more
+     * than {@link org.apache.lucene.search.BooleanQuery#maxClauseCount} terms.
+     */
+    private Query stdRangeQuery;
+
+    /**
      * Creates a new RangeQuery. The lower or the upper term may be
      * <code>null</code>, but not both!
      *
@@ -114,8 +130,9 @@
     }
 
     /**
-     * Rewrites this query into a {@link ConstantScoreRangeQuery} if
-     * {@link #transform} is {@link #TRANSFORM_NONE}.
+     * Tries to rewrite this query into a standard lucene RangeQuery.
+     * This rewrite might fail with a TooManyClauses exception. If that
+     * happens, we use our own implementation.
      *
      * @param reader the index reader.
      * @return the rewritten query or this query if rewriting is not possible.
@@ -123,9 +140,16 @@
      */
     public Query rewrite(IndexReader reader) throws IOException {
         if (transform == TRANSFORM_NONE) {
-            return new ConstantScoreRangeQuery(lowerTerm.field(),
-                    lowerTerm.text(), upperTerm.text(), inclusive,
-                    inclusive).rewrite(reader);
+            Query stdRangeQueryImpl
+                    = new org.apache.lucene.search.RangeQuery(lowerTerm, upperTerm, inclusive);
+            try {
+                stdRangeQuery = stdRangeQueryImpl.rewrite(reader);
+                return stdRangeQuery;
+            } catch (BooleanQuery.TooManyClauses e) {
+                log.debug("Too many terms to enumerate, using custom RangeQuery");
+                // failed, use own implementation
+                return this;
+            }
         } else {
             // always use our implementation when we need to transform the
             // term enum
@@ -170,7 +194,9 @@
      * {@inheritDoc}
      */
     public void extractTerms(Set terms) {
-        // cannot extract terms
+        if (stdRangeQuery != null) {
+            stdRangeQuery.extractTerms(terms);
+        }
     }
 
     /**



Mime
View raw message