Query.java
001 package gate.creole.annic.apache.lucene.search;
002 
003 /**
004  * Copyright 2002-2004 The Apache Software Foundation
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 
019 import java.io.IOException;
020 
021 import java.util.HashSet;
022 import java.util.Iterator;
023 
024 import gate.creole.annic.apache.lucene.index.IndexReader;
025 
026 /** The abstract base class for queries.
027     <p>Instantiable subclasses are:
028     <ul>
029     <li> {@link TermQuery}
030     <li> {@link BooleanQuery}
031     <li> {@link PhraseQuery}
032     </ul>
033     <p>A parser for queries is contained in:
034     <ul>
035     <li>{@link gate.creole.annic.apache.lucene.queryParser.QueryParser QueryParser}
036     </ul>
037 */
038 public abstract class Query implements java.io.Serializable, Cloneable {
039   private float boost = 1.0f;                     // query boost factor
040 
041   /** Sets the boost for this query clause to <code>b</code>.  Documents
042    * matching this clause will (in addition to the normal weightings) have
043    * their score multiplied by <code>b</code>.
044    */
045   public void setBoost(float b) { boost = b; }
046 
047   /** Gets the boost for this clause.  Documents matching
048    * this clause will (in addition to the normal weightings) have their score
049    * multiplied by <code>b</code>.   The boost is 1.0 by default.
050    */
051   public float getBoost() { return boost; }
052 
053   /** Prints a query to a string, with <code>field</code> as the default field
054    * for terms.  <p>The representation used is one that is readable by
055    {@link gate.creole.annic.apache.lucene.queryParser.QueryParser QueryParser}
056    * (although, if the query was created by the parser, the printed
057    * representation may not be exactly what was parsed).
058    */
059   public abstract String toString(String field);
060 
061   /** Prints a query to a string. */
062   public String toString() {
063     return toString("");
064   }
065 
066   /** Expert: Constructs an appropriate Weight implementation for this query.
067    *
068    <p>Only implemented by primitive queries, which re-write to themselves.
069    */
070   protected Weight createWeight(Searcher searcher) {
071     throw new UnsupportedOperationException();
072   }
073 
074   /** Expert: Constructs an initializes a Weight for a top-level query. */
075   public Weight weight(Searcher searcher)
076     throws IOException {
077     Query query = searcher.rewrite(this);
078     Weight weight = query.createWeight(searcher);
079     float sum = weight.sumOfSquaredWeights();
080     float norm = getSimilarity(searcher).queryNorm(sum);
081     weight.normalize(norm);
082     return weight;
083   }
084 
085   /** Expert: called to re-write queries into primitive queries. */
086   public Query rewrite(IndexReader readerthrows IOException {
087     return this;
088   }
089 
090   /** Expert: called when re-writing queries under MultiSearcher.
091    *
092    <p>Only implemented by derived queries, with no
093    {@link #createWeight(Searcher)} implementatation.
094    */
095   public Query combine(Query[] queries) {
096     throw new UnsupportedOperationException();
097   }
098 
099 
100   /** Expert: merges the clauses of a set of BooleanQuery's into a single
101    * BooleanQuery.
102    *
103    *<p>A utility for use by {@link #combine(Query[])} implementations.
104    */
105   public static Query mergeBooleanQueries(Query[] queries) {
106     HashSet allClauses = new HashSet();
107     for (int i = 0; i < queries.length; i++) {
108       BooleanClause[] clauses = ((BooleanQuery)queries[i]).getClauses();
109       for (int j = 0; j < clauses.length; j++) {
110         allClauses.add(clauses[j]);
111       }
112     }
113 
114     BooleanQuery result = new BooleanQuery();
115     Iterator i = allClauses.iterator();
116     while (i.hasNext()) {
117       result.add((BooleanClause)i.next());
118     }
119     return result;
120   }
121 
122   /** Expert: Returns the Similarity implementation to be used for this query.
123    * Subclasses may override this method to specify their own Similarity
124    * implementation, perhaps one that delegates through that of the Searcher.
125    * By default the Searcher's Similarity implementation is returned.*/
126   public Similarity getSimilarity(Searcher searcher) {
127     return searcher.getSimilarity();
128   }
129 
130   /** Returns a clone of this query. */
131   public Object clone() {
132     try {
133       return (Query)super.clone();
134     catch (CloneNotSupportedException e) {
135       throw new RuntimeException("Clone not supported: " + e.getMessage());
136     }
137   }
138 }