lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/search TestWildcard.java
Date Mon, 20 Jan 2003 18:40:19 GMT
cutting     2003/01/20 10:40:19

  Modified:    src/java/org/apache/lucene/search BooleanQuery.java
                        Query.java
               src/test/org/apache/lucene/search TestWildcard.java
  Log:
  Fixed a bug when rewritten queries were included in a BooleanQuery.
  
  Revision  Changes    Path
  1.12      +30 -4     jakarta-lucene/src/java/org/apache/lucene/search/BooleanQuery.java
  
  Index: BooleanQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/BooleanQuery.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- BooleanQuery.java	15 Jan 2003 19:25:04 -0000	1.11
  +++ BooleanQuery.java	20 Jan 2003 18:40:19 -0000	1.12
  @@ -195,15 +195,41 @@
     }
   
     protected Weight createWeight(Searcher searcher) {
  +    return new BooleanWeight(searcher);
  +  }
  +
  +  public Query rewrite(IndexReader reader) throws IOException {
       if (clauses.size() == 1) {                    // optimize 1-clause queries
         BooleanClause c = (BooleanClause)clauses.elementAt(0);
  -      if (!c.prohibited) {			  // just return clause weight
  -        Query clone = (Query)c.query.clone();
  +      if (!c.prohibited) {			  // just return clause
  +        Query clone = (Query)c.query.clone();     // have to clone to boost
           clone.setBoost(getBoost() * clone.getBoost());
  -        return clone.createWeight(searcher);
  +        return clone;
         }
       }
  -    return new BooleanWeight(searcher);
  +
  +    BooleanQuery clone = (BooleanQuery)this.clone(); // recursively clone
  +    boolean changed = false;
  +    for (int i = 0 ; i < clauses.size(); i++) {
  +      BooleanClause c = (BooleanClause)clauses.elementAt(i);
  +      Query q = c.query.rewrite(reader);
  +      if (q != c.query) {                         // rewrote
  +        changed = true;                           // replace in clone
  +        clone.clauses.setElementAt
  +          (new BooleanClause(q, c.required, c.prohibited), i);
  +      }
  +    }
  +    if (changed)
  +      return clone;                               // clauses rewrote
  +    else
  +      return this;                                // no clauses rewrote
  +  }
  +
  +
  +  public Object clone() {
  +    BooleanQuery clone = (BooleanQuery)super.clone();
  +    clone.clauses = (Vector)this.clauses.clone();
  +    return clone;
     }
   
     /** Prints a user-readable version of this query. */
  
  
  
  1.12      +1 -5      jakarta-lucene/src/java/org/apache/lucene/search/Query.java
  
  Index: Query.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/Query.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Query.java	15 Jan 2003 15:41:52 -0000	1.11
  +++ Query.java	20 Jan 2003 18:40:19 -0000	1.12
  @@ -125,11 +125,7 @@
       return weight;
     }
   
  -  /** Expert: called to re-write queries into primitive queries.
  -   *
  -   * <p>Only implemented by derived queries, with no {@link
  -   * #createWeight(Searcher)} implementatation..
  -   */
  +  /** Expert: called to re-write queries into primitive queries. */
     public Query rewrite(IndexReader reader) throws IOException {
       return this;
     }
  
  
  
  1.5       +17 -0     jakarta-lucene/src/test/org/apache/lucene/search/TestWildcard.java
  
  Index: TestWildcard.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/search/TestWildcard.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestWildcard.java	18 Jul 2002 14:41:39 -0000	1.4
  +++ TestWildcard.java	20 Jan 2003 18:40:19 -0000	1.5
  @@ -101,6 +101,14 @@
           Query query2 = new WildcardQuery(new Term("body", "metal*"));
           Query query3 = new WildcardQuery(new Term("body", "m*tal"));
           Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
  +        Query query5 = new WildcardQuery(new Term("body", "m*tals"));
  +
  +        BooleanQuery query6 = new BooleanQuery();
  +        query6.add(query5, false, false);
  +
  +        BooleanQuery query7 = new BooleanQuery();
  +        query7.add(query3, false, false);
  +        query7.add(query5, false, false);
   
   	Hits result;
   
  @@ -114,6 +122,15 @@
   	assertEquals(1, result.length());
   
   	result = searcher.search(query4);
  +	assertEquals(2, result.length());
  +
  +        result = searcher.search(query5);
  +	assertEquals(1, result.length());
  +
  +        result = searcher.search(query6);
  +	assertEquals(1, result.length());
  +
  +        result = searcher.search(query7);
   	assertEquals(2, result.length());
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>


Mime
View raw message