lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From briango...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/queryParser TestQueryParser.java
Date Thu, 17 Jan 2002 02:00:09 GMT
briangoetz    02/01/16 18:00:09

  Modified:    src/java/org/apache/lucene/queryParser QueryParser.jj
               src/java/org/apache/lucene/search MultiTermQuery.java
                        PhraseQuery.java PrefixQuery.java Query.java
                        RangeQuery.java TermQuery.java
               src/test/org/apache/lucene/queryParser TestQueryParser.java
  Log:
  Migrate setBoost/getBoost into base QUery class; don't wrap queries with BooleanQuery in
query parser unless necessary; more test cases for query parser
  
  Revision  Changes    Path
  1.8       +16 -15    jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj
  
  Index: QueryParser.jj
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- QueryParser.jj	14 Jan 2002 03:02:39 -0000	1.7
  +++ QueryParser.jj	17 Jan 2002 02:00:09 -0000	1.8
  @@ -320,21 +320,29 @@
   Query Query(String field) :
   {
     Vector clauses = new Vector();
  -  Query q;
  +  Query q, firstQuery=null;
     int conj, mods; 
   }
   {
     mods=Modifiers() q=Clause(field) 
  -  { addClause(clauses, CONJ_NONE, mods, q); }
  +  { 
  +    addClause(clauses, CONJ_NONE, mods, q); 
  +    if (mods == MOD_NONE) 
  +        firstQuery=q; 
  +  }
     ( 
       conj=Conjunction() mods=Modifiers() q=Clause(field) 
       { addClause(clauses, conj, mods, q); }
     )*
       {
  -      BooleanQuery query = new BooleanQuery();
  -      for (int i = 0; i < clauses.size(); i++)
  -  	query.add((BooleanClause)clauses.elementAt(i));
  -      return query;
  +      if (clauses.size() == 1 && firstQuery != null)
  +        return firstQuery;
  +      else {
  +        BooleanQuery query = new BooleanQuery();
  +        for (int i = 0; i < clauses.size(); i++)
  +  	  query.add((BooleanClause)clauses.elementAt(i));
  +        return query;
  +      }
       }
   }
   
  @@ -375,7 +383,7 @@
          | term=<NUMBER>
        )
        [ <FUZZY> { fuzzy=true; } ]
  -     [ <CARAT> boost=<NUMBER> ]
  +     [ <CARAT> boost=<NUMBER> [ <FUZZY> { fuzzy=true; } ] ]
        { 
          if (wildcard)
            q = new WildcardQuery(new Term(field, term.image));
  @@ -409,14 +417,7 @@
         }
         catch (Exception ignored) { }
   
  -      if (q instanceof TermQuery) 
  -        ((TermQuery) q).setBoost(f);
  -      else if (q instanceof PhraseQuery) 
  -        ((PhraseQuery) q).setBoost(f);
  -      else if (q instanceof MultiTermQuery)
  -        ((MultiTermQuery) q).setBoost(f);
  -      else if (q instanceof RangeQuery)
  -        ((RangeQuery) q).setBoost(f);
  +      q.setBoost(f);
       }
       return q; 
     }
  
  
  
  1.2       +0 -13     jakarta-lucene/src/java/org/apache/lucene/search/MultiTermQuery.java
  
  Index: MultiTermQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/MultiTermQuery.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MultiTermQuery.java	18 Sep 2001 16:29:57 -0000	1.1
  +++ MultiTermQuery.java	17 Jan 2002 02:00:09 -0000	1.2
  @@ -70,7 +70,6 @@
       private Term term;
       private FilteredTermEnum enum;
       private IndexReader reader;
  -    private float boost = 1.0f;
       private BooleanQuery query;
       
       /** Enable or disable lucene style toString(field) format */
  @@ -85,18 +84,6 @@
       /** Set the TermEnum to be used */
       protected void setEnum(FilteredTermEnum enum) {
           this.enum = enum;
  -    }
  -    
  -    /** Sets the boost for this term to <code>b</code>.  Documents containing
  -     * this term will (in addition to the normal weightings) have their score
  -     * multiplied by <code>boost</code>. */
  -    final public void setBoost(float boost) {
  -        this.boost = boost;
  -    }
  -    
  -    /** Returns the boost for this term. */
  -    final public float getBoost() {
  -        return boost;
       }
       
       final float sumOfSquaredWeights(Searcher searcher) throws IOException {
  
  
  
  1.2       +0 -10     jakarta-lucene/src/java/org/apache/lucene/search/PhraseQuery.java
  
  Index: PhraseQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/PhraseQuery.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PhraseQuery.java	18 Sep 2001 16:29:57 -0000	1.1
  +++ PhraseQuery.java	17 Jan 2002 02:00:09 -0000	1.2
  @@ -71,7 +71,6 @@
     private float idf = 0.0f;
     private float weight = 0.0f;
   
  -  private float boost = 1.0f;
     private int slop = 0;
   
   
  @@ -79,15 +78,6 @@
     public PhraseQuery() {
     }
   
  -  /** Sets the boost for this term to <code>b</code>.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>b</code>. */
  -  public final void setBoost(float b) { boost = b; }
  -  /** Gets the boost for this term.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>b</code>.   The boost is 1.0 by default.  */
  -  public final float getBoost() { return boost; }
  -  
     /** Sets the number of other words permitted between words in query phrase.
       If zero, then this is an exact phrase search.  For larger values this works
       like a <code>WITHIN</code> or <code>NEAR</code> operator.
  
  
  
  1.2       +0 -13     jakarta-lucene/src/java/org/apache/lucene/search/PrefixQuery.java
  
  Index: PrefixQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/PrefixQuery.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PrefixQuery.java	18 Sep 2001 16:29:58 -0000	1.1
  +++ PrefixQuery.java	17 Jan 2002 02:00:09 -0000	1.2
  @@ -64,7 +64,6 @@
   final public class PrefixQuery extends Query {
     private Term prefix;
     private IndexReader reader;
  -  private float boost = 1.0f;
     private BooleanQuery query;
   
     /** Constructs a query for terms starting with <code>prefix</code>. */
  @@ -73,18 +72,6 @@
       this.reader = reader;
     }
   
  -  /** Sets the boost for this term to <code>b</code>.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>boost</code>. */
  -  public void setBoost(float boost) {
  -    this.boost = boost;
  -  }
  -
  -  /** Returns the boost for this term. */
  -  public float getBoost() {
  -    return boost;
  -  }
  -  
     final void prepare(IndexReader reader) {
       this.query = null;
       this.reader = reader;
  
  
  
  1.2       +13 -0     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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Query.java	18 Sep 2001 16:29:58 -0000	1.1
  +++ Query.java	17 Jan 2002 02:00:09 -0000	1.2
  @@ -73,6 +73,9 @@
     */
   abstract public class Query {
   
  +  // query boost factor
  +  protected float boost = 1.0f;
  +
     // query weighting
     abstract float sumOfSquaredWeights(Searcher searcher) throws IOException;
     abstract void normalize(float norm);
  @@ -91,6 +94,16 @@
       return query.scorer(reader);
     }
   
  +  /** Sets the boost for this term to <code>b</code>.  Documents containing
  +    this term will (in addition to the normal weightings) have their score
  +    multiplied by <code>b</code>. */
  +  public void setBoost(float b) { boost = b; }
  +
  +  /** Gets the boost for this term.  Documents containing
  +    this term will (in addition to the normal weightings) have their score
  +    multiplied by <code>b</code>.   The boost is 1.0 by default.  */
  +  public float getBoost() { return boost; }
  +  
     /** Prints a query to a string, with <code>field</code> as the default field
       for terms.
       <p>The representation used is one that is readable by
  
  
  
  1.3       +0 -15     jakarta-lucene/src/java/org/apache/lucene/search/RangeQuery.java
  
  Index: RangeQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/RangeQuery.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RangeQuery.java	11 Oct 2001 15:19:37 -0000	1.2
  +++ RangeQuery.java	17 Jan 2002 02:00:09 -0000	1.3
  @@ -67,7 +67,6 @@
       private Term upperTerm;
       private boolean inclusive;
       private IndexReader reader;
  -    private float boost = 1.0f;
       private BooleanQuery query;
       
       /** Constructs a query selecting all terms greater than 
  @@ -89,20 +88,6 @@
           this.lowerTerm = lowerTerm;
           this.upperTerm = upperTerm;
           this.inclusive = inclusive;
  -    }
  -    
  -    /** Sets the boost for this term to <code>b</code>.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>boost</code>. */
  -    public void setBoost(float boost)
  -    {
  -        this.boost = boost;
  -    }
  -    
  -    /** Returns the boost for this term. */
  -    public float getBoost()
  -    {
  -        return boost;
       }
       
       final void prepare(IndexReader reader)
  
  
  
  1.2       +0 -10     jakarta-lucene/src/java/org/apache/lucene/search/TermQuery.java
  
  Index: TermQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/TermQuery.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TermQuery.java	18 Sep 2001 16:29:58 -0000	1.1
  +++ TermQuery.java	17 Jan 2002 02:00:09 -0000	1.2
  @@ -64,7 +64,6 @@
     */
   final public class TermQuery extends Query {
     private Term term;
  -  private float boost = 1.0f;
     private float idf = 0.0f;
     private float weight = 0.0f;
   
  @@ -73,15 +72,6 @@
       term = t;
     }
   
  -  /** Sets the boost for this term to <code>b</code>.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>b</code>. */
  -  public void setBoost(float b) { boost = b; }
  -  /** Gets the boost for this term.  Documents containing
  -    this term will (in addition to the normal weightings) have their score
  -    multiplied by <code>b</code>.   The boost is 1.0 by default.  */
  -  public float getBoost() { return boost; }
  -  
     final float sumOfSquaredWeights(Searcher searcher) throws IOException {
       idf = Similarity.idf(term, searcher);
       weight = idf * boost;
  
  
  
  1.6       +35 -7     jakarta-lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java
  
  Index: TestQueryParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestQueryParser.java	14 Jan 2002 03:02:39 -0000	1.5
  +++ TestQueryParser.java	17 Jan 2002 02:00:09 -0000	1.6
  @@ -114,17 +114,20 @@
       }
     }
     
  -  public void assertQueryEquals(String query, Analyzer a, String result) 
  -  throws Exception {
  +  public Query getQuery(String query, Analyzer a) throws Exception {
       if (a == null)
         a = new SimpleAnalyzer();
       QueryParser qp = new QueryParser("field", a);
  -    Query q = qp.parse(query);
  +    return qp.parse(query);
  +  }
  +
  +  public void assertQueryEquals(String query, Analyzer a, String result) 
  +  throws Exception {
  +    Query q = getQuery(query, a);
       String s = q.toString("field");
       if (!s.equals(result)) {
  -      System.err.println("Query /" + query + "/ yielded /" + s 
  -                         + "/, expecting /" + result + "/");
  -      assert(false);
  +      fail("Query /" + query + "/ yielded /" + s 
  +           + "/, expecting /" + result + "/");
       }
     }
   
  @@ -136,6 +139,8 @@
       assertQueryEquals("term 1.0 1 2", null, "term");
   
       assertQueryEquals("a AND b", null, "+a +b");
  +    assertQueryEquals("(a AND b)", null, "+a +b");
  +    assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");
       assertQueryEquals("a AND NOT b", null, "+a -b");
       assertQueryEquals("a AND -b", null, "+a -b");
       assertQueryEquals("a AND !b", null, "+a -b");
  @@ -154,6 +159,10 @@
                         "+foo:term +anotherterm");
       assertQueryEquals("term AND \"phrase phrase\"", null, 
                         "+term +\"phrase phrase\"");
  +    assertQueryEquals("\"hello there\"", null, "\"hello there\"");
  +    assert(getQuery("a AND b", null) instanceof BooleanQuery);
  +    assert(getQuery("hello", null) instanceof TermQuery);
  +    assert(getQuery("\"hello there\"", null) instanceof PhraseQuery);
   
       assertQueryEquals("germ term^2.0", null, "germ term^2.0");
       assertQueryEquals("term^2.0", null, "term^2.0");
  @@ -170,6 +179,21 @@
                         "+(title:dog title:cat) -author:\"bob dole\"");
     }
   
  +  public void testWildcard() throws Exception {
  +    assertQueryEquals("term*", null, "term*");
  +    assertQueryEquals("term*^2", null, "term*^2.0");
  +    assertQueryEquals("term~", null, "term~");
  +    assertQueryEquals("term~^2", null, "term^2.0~");
  +    assertQueryEquals("term^2~", null, "term^2.0~");
  +    assertQueryEquals("term*germ", null, "term*germ");
  +    assertQueryEquals("term*germ^3", null, "term*germ^3.0");
  +
  +    assert(getQuery("term*", null) instanceof PrefixQuery);
  +    assert(getQuery("term*^2", null) instanceof PrefixQuery);
  +    assert(getQuery("term~", null) instanceof FuzzyQuery);
  +    assert(getQuery("term*germ", null) instanceof WildcardQuery);
  +  }
  +
     public void testQPA() throws Exception {
       assertQueryEquals("term term term", qpAnalyzer, "term term term");
       assertQueryEquals("term +stop term", qpAnalyzer, "term term");
  @@ -180,17 +204,21 @@
       assertQueryEquals("term AND NOT phrase term", qpAnalyzer, 
                         "+term -\"phrase1 phrase2\" term");
       assertQueryEquals("stop", qpAnalyzer, "");
  +    assert(getQuery("term term term", qpAnalyzer) instanceof BooleanQuery);
  +    assert(getQuery("term +stop", qpAnalyzer) instanceof TermQuery);
     }
   
     public void testRange() throws Exception {
       assertQueryEquals("[ a z]", null, "[a-z]");
  +    assert(getQuery("[ a z]", null) instanceof RangeQuery);
       assertQueryEquals("[ a z ]", null, "[a-z]");
       assertQueryEquals("{ a z}", null, "{a-z}");
       assertQueryEquals("{ a z }", null, "{a-z}");
       assertQueryEquals("{ a z }^2.0", null, "{a-z}^2.0");
       assertQueryEquals("[ a z] OR bar", null, "[a-z] bar");
       assertQueryEquals("[ a z] AND bar", null, "+[a-z] +bar");
  -    assertQueryEquals("( bar blar { a z}) ", null, "(bar blar {a-z})");
  +    assertQueryEquals("( bar blar { a z}) ", null, "bar blar {a-z}");
  +    assertQueryEquals("gack ( bar blar { a z}) ", null, "gack (bar blar {a-z})");
     }
   }
   
  
  
  

--
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