lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject svn commit: r477424 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/WildcardQuery.java src/test/org/apache/lucene/search/TestWildcard.java
Date Tue, 21 Nov 2006 00:09:50 GMT
Author: ehatcher
Date: Mon Nov 20 16:09:50 2006
New Revision: 477424

URL: http://svn.apache.org/viewvc?view=rev&rev=477424
Log:
LUCENE-543: WildcardQuery without wildcarded term rewrites to a TermQuery

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestWildcard.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=477424&r1=477423&r2=477424
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Mon Nov 20 16:09:50 2006
@@ -30,6 +30,11 @@
     for range queries. Added useOldRangeQuery property to QueryParser to allow 
     selection of old RangeQuery class if required.      
 
+ 6. LUCENE-543: WildcardQuery now performs a TermQuery if the provided term
+    does not contain a wildcard character (? or *), when previously a
+    StringIndexOutOfBoundsException was thrown.
+    (Michael Busch via Erik Hatcher)
+
 New features
 
  1. LUCENE-503: New ThaiAnalyzer and ThaiWordFilter in contrib/analyzers
@@ -58,7 +63,7 @@
  7. LUCENE-573: QueryParser now allows backslash escaping in
     quoted terms and phrases. (Michael Busch via Yonik Seeley)
 
- 7. LUCENE-716: QueryParser now allows specification of unicode
+ 8. LUCENE-716: QueryParser now allows specification of unicode
     characters in terms via a unicode escape of the form \uXXXX
     (Michael Busch via Yonik Seeley)
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java?view=diff&rev=477424&r1=477423&r2=477424
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/WildcardQuery.java Mon Nov 20 16:09:50
2006
@@ -31,8 +31,11 @@
  * @see WildcardTermEnum
  */
 public class WildcardQuery extends MultiTermQuery {
+  private boolean termContainsWildcard;
+    
   public WildcardQuery(Term term) {
     super(term);
+    this.termContainsWildcard = (term.text().indexOf('*') != -1) || (term.text().indexOf('?')
!= -1);
   }
 
   protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
@@ -44,5 +47,13 @@
       return super.equals(o);
 
     return false;
+  }
+  
+  public Query rewrite(IndexReader reader) throws IOException {
+      if (this.termContainsWildcard) {
+          return super.rewrite(reader);
+      }
+      
+      return new TermQuery(getTerm());
   }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestWildcard.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestWildcard.java?view=diff&rev=477424&r1=477423&r2=477424
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestWildcard.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestWildcard.java Mon Nov 20 16:09:50
2006
@@ -54,6 +54,21 @@
     assertFalse(wq1.equals(fq));
     assertFalse(fq.equals(wq1));
   }
+  
+  /**
+   * Tests if a WildcardQuery that has no wildcard in the term is rewritten to a single
+   * TermQuery.
+   */
+  public void testTermWithoutWildcard() throws IOException {
+      RAMDirectory indexStore = getIndexStore("field", new String[]{"nowildcard", "nowildcardx"});
+      IndexSearcher searcher = new IndexSearcher(indexStore);
+
+      Query wq = new WildcardQuery(new Term("field", "nowildcard"));
+      assertMatches(searcher, wq, 1);
+
+      wq = searcher.rewrite(wq);
+      assertTrue(wq instanceof TermQuery);
+  }
 
   /**
    * Tests Wildcard queries with an asterisk.



Mime
View raw message