lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r1026602 - in /lucene/dev/trunk: lucene/ lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/ lucene/src/java/org/apache/lucene/queryParser/ lucene/src/java/org/apache/lucene/search/ lucene/src/test/org/apache/lucene...
Date Sat, 23 Oct 2010 11:30:52 GMT
Author: yonik
Date: Sat Oct 23 11:30:51 2010
New Revision: 1026602

URL: http://svn.apache.org/viewvc?rev=1026602&view=rev
Log:
LUCENE-995: QP treats * as open end in range

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/AnalyzingQueryParser.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParserBase.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TermRangeQuery.java
    lucene/dev/trunk/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java
    lucene/dev/trunk/solr/src/java/org/apache/solr/search/SolrQueryParser.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Sat Oct 23 11:30:51 2010
@@ -275,6 +275,11 @@ New features
   QueryParser subclasses that overrode getRangeQuery will need to be changed
   to use the new getRangeQuery method.  (Andrew Schurman, Mark Miller, yonik)
 
+* LUCENE-995: The QueryParser now interprets * as an open end for range
+  queries.  Literal asterisks may be represented by quoting or escaping
+  (i.e. \* or "*")  Custom QueryParser sublcasses overriding getRangeQuery()
+  will be passed null for any open endpoint. (Adriano Crestani, yonik)
+
 Optimizations
 
 * LUCENE-2410: ~20% speedup on exact (slop=0) PhraseQuery matching.

Modified: lucene/dev/trunk/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/AnalyzingQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/AnalyzingQueryParser.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/AnalyzingQueryParser.java
(original)
+++ lucene/dev/trunk/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/analyzing/AnalyzingQueryParser.java
Sat Oct 23 11:30:51 2010
@@ -272,49 +272,58 @@ public class AnalyzingQueryParser extend
   protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive,
boolean endInclusive)
       throws ParseException {
     // get Analyzer from superclass and tokenize the terms
-    TokenStream source = getAnalyzer().tokenStream(field, new StringReader(part1));
-    CharTermAttribute termAtt = source.addAttribute(CharTermAttribute.class);
+    TokenStream source = null;
+    CharTermAttribute termAtt = null;
     boolean multipleTokens = false;
 
-    // part1
-    try {
-      if (source.incrementToken()) {
-        part1 = termAtt.toString();
-      }
-      multipleTokens = source.incrementToken();
-    } catch (IOException e) {
-      // ignore
-    }
-    try {
-      source.close();
-    } catch (IOException e) {
-      // ignore
-    }
-    if (multipleTokens) {
-      throw new ParseException("Cannot build RangeQuery with analyzer " + getAnalyzer().getClass()
-          + " - tokens were added to part1");
-    }
+    if (part1 != null) {
+      // part1
+      try {
+        source = getAnalyzer().tokenStream(field, new StringReader(part1));
+        termAtt = source.addAttribute(CharTermAttribute.class);
+        multipleTokens = false;
 
-    // part2
-    source = getAnalyzer().tokenStream(field, new StringReader(part2));
-    termAtt = source.addAttribute(CharTermAttribute.class);
-    
-    try {
-      if (source.incrementToken()) {
-        part2 = termAtt.toString();
-      }
-      multipleTokens = source.incrementToken();
-    } catch (IOException e) {
-      // ignore
-    }
-    try {
-      source.close();
-    } catch (IOException e) {
-      // ignore
+
+        if (source.incrementToken()) {
+          part1 = termAtt.toString();
+        }
+        multipleTokens = source.incrementToken();
+      } catch (IOException e) {
+        // ignore
+      }
+      try {
+        source.close();
+      } catch (IOException e) {
+        // ignore
+      }
+      if (multipleTokens) {
+        throw new ParseException("Cannot build RangeQuery with analyzer " + getAnalyzer().getClass()
+            + " - tokens were added to part1");
+      }
     }
-    if (multipleTokens) {
-      throw new ParseException("Cannot build RangeQuery with analyzer " + getAnalyzer().getClass()
-          + " - tokens were added to part2");
+
+    if (part2 != null) {
+      // part2
+      source = getAnalyzer().tokenStream(field, new StringReader(part2));
+      termAtt = source.addAttribute(CharTermAttribute.class);
+
+      try {
+        if (source.incrementToken()) {
+          part2 = termAtt.toString();
+        }
+        multipleTokens = source.incrementToken();
+      } catch (IOException e) {
+        // ignore
+      }
+      try {
+        source.close();
+      } catch (IOException e) {
+        // ignore
+      }
+      if (multipleTokens) {
+        throw new ParseException("Cannot build RangeQuery with analyzer " + getAnalyzer().getClass()
+            + " - tokens were added to part2");
+      }
     }
     return super.getRangeQuery(field, part1, part2, startInclusive, endInclusive);
   }

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.java Sat Oct
23 11:30:51 2010
@@ -448,13 +448,19 @@ public class QueryParser extends QueryPa
         jj_la1[17] = jj_gen;
         ;
       }
+          boolean startOpen=false;
+          boolean endOpen=false;
           if (goop1.kind == RANGE_QUOTED) {
             goop1.image = goop1.image.substring(1, goop1.image.length()-1);
+          } else if ("*".equals(goop1.image)) {
+            startOpen=true;
           }
           if (goop2.kind == RANGE_QUOTED) {
             goop2.image = goop2.image.substring(1, goop2.image.length()-1);
+          } else if ("*".equals(goop2.image)) {
+            endOpen=true;
           }
-          q = getRangeQuery(field, discardEscapeChar(goop1.image), discardEscapeChar(goop2.image),
startInc, endInc);
+          q = getRangeQuery(field, startOpen ? null : discardEscapeChar(goop1.image), endOpen
? null : discardEscapeChar(goop2.image), startInc, endInc);
       break;
     case QUOTED:
       term = jj_consume_token(QUOTED);

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj Sat Oct
23 11:30:51 2010
@@ -328,13 +328,19 @@ Query Term(String field) : {
          ( <RANGEIN_END> {endInc=true;} | <RANGEEX_END>))
        [ <CARAT> boost=<NUMBER> ]
         {
+          boolean startOpen=false;
+          boolean endOpen=false;
           if (goop1.kind == RANGE_QUOTED) {
             goop1.image = goop1.image.substring(1, goop1.image.length()-1);
+          } else if ("*".equals(goop1.image)) {
+            startOpen=true;
           }
           if (goop2.kind == RANGE_QUOTED) {
             goop2.image = goop2.image.substring(1, goop2.image.length()-1);
+          } else if ("*".equals(goop2.image)) {
+            endOpen=true;
           }
-          q = getRangeQuery(field, discardEscapeChar(goop1.image), discardEscapeChar(goop2.image),
startInc, endInc);
+          q = getRangeQuery(field, startOpen ? null : discardEscapeChar(goop1.image), endOpen
? null : discardEscapeChar(goop2.image), startInc, endInc);
         }
      | term=<QUOTED>
        [ fuzzySlop=<FUZZY_SLOP> ]

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParserBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParserBase.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParserBase.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/queryParser/QueryParserBase.java Sat
Oct 23 11:30:51 2010
@@ -702,7 +702,8 @@ public abstract class QueryParserBase {
 
 
   /**
-   * @exception org.apache.lucene.queryParser.ParseException throw in overridden method to
disallow
+   *
+   * @exception org.apache.lucene.queryParser.ParseException
    */
   protected Query getRangeQuery(String field,
                                 String part1,
@@ -711,13 +712,28 @@ public abstract class QueryParserBase {
                                 boolean endInclusive) throws ParseException
   {
     if (lowercaseExpandedTerms) {
-      part1 = part1.toLowerCase();
-      part2 = part2.toLowerCase();
+      part1 = part1==null ? null : part1.toLowerCase();
+      part2 = part2==null ? null : part2.toLowerCase();
     }
+
+
+    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+    df.setLenient(true);
+    DateTools.Resolution resolution = getDateResolution(field);
+    
     try {
-      DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
-      df.setLenient(true);
       Date d1 = df.parse(part1);
+      if (resolution == null) {
+        // no default or field specific date resolution has been set,
+        // use deprecated DateField to maintain compatibility with
+        // pre-1.9 Lucene versions.
+        part1 = DateField.dateToString(d1);
+      } else {
+        part1 = DateTools.dateToString(d1, resolution);
+      }
+    } catch (Exception e) { }
+
+    try {
       Date d2 = df.parse(part2);
       if (endInclusive) {
         // The user can only specify the date, not the time, so make sure
@@ -731,19 +747,15 @@ public abstract class QueryParserBase {
         cal.set(Calendar.MILLISECOND, 999);
         d2 = cal.getTime();
       }
-      DateTools.Resolution resolution = getDateResolution(field);
       if (resolution == null) {
         // no default or field specific date resolution has been set,
         // use deprecated DateField to maintain compatibility with
         // pre-1.9 Lucene versions.
-        part1 = DateField.dateToString(d1);
         part2 = DateField.dateToString(d2);
       } else {
-        part1 = DateTools.dateToString(d1, resolution);
         part2 = DateTools.dateToString(d2, resolution);
       }
-    }
-    catch (Exception e) { }
+    } catch (Exception e) { }
 
     return newRangeQuery(field, part1, part2, startInclusive, endInclusive);
   }

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TermRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TermRangeQuery.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TermRangeQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TermRangeQuery.java Sat Oct
23 11:30:51 2010
@@ -160,9 +160,9 @@ public class TermRangeQuery extends Mult
           buffer.append(":");
       }
       buffer.append(includeLower ? '[' : '{');
-      buffer.append(lowerTerm != null ? lowerTerm : "*");
+      buffer.append(lowerTerm != null ? ("*".equals(lowerTerm) ? "\\*" : lowerTerm)  : "*");
       buffer.append(" TO ");
-      buffer.append(upperTerm != null ? upperTerm : "*");
+      buffer.append(upperTerm != null ? ("*".equals(upperTerm) ? "\\*" : upperTerm) : "*");
       buffer.append(includeUpper ? ']' : '}');
       buffer.append(ToStringUtils.boost(getBoost()));
       return buffer.toString();

Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java Sat
Oct 23 11:30:51 2010
@@ -558,6 +558,11 @@ public class TestQueryParser extends Luc
     assertQueryEquals("[ a TO z] AND bar", null, "+[a TO z] +bar");
     assertQueryEquals("( bar blar { a TO z}) ", null, "bar blar {a TO z}");
     assertQueryEquals("gack ( bar blar { a TO z}) ", null, "gack (bar blar {a TO z})");
+
+    assertQueryEquals("[* TO Z]",null,"[* TO z]");
+    assertQueryEquals("[A TO *]",null,"[a TO *]");
+    assertQueryEquals("[* TO *]",null,"[* TO *]");
+    assertQueryEquals("[\\* TO \"*\"]",null,"[\\* TO \\*]");
  }
     
   public void testFarsiRangeCollating() throws Exception {

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/search/SolrQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/search/SolrQueryParser.java?rev=1026602&r1=1026601&r2=1026602&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/search/SolrQueryParser.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/search/SolrQueryParser.java Sat Oct 23
11:30:51 2010
@@ -166,10 +166,7 @@ public class SolrQueryParser extends Que
   protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive,
boolean endInclusive) throws ParseException {
     checkNullField(field);
     SchemaField sf = schema.getField(field);
-    return sf.getType().getRangeQuery(parser, sf,
-            "*".equals(part1) ? null : part1,
-            "*".equals(part2) ? null : part2,
-            startInclusive, endInclusive);
+    return sf.getType().getRangeQuery(parser, sf, part1, part2, startInclusive, endInclusive);
   }
 
   @Override



Mime
View raw message