FileDocCategorySizeDatePackage
Query.javaAPI DocApache Lucene 2.1.07552Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.search

Query

public abstract class Query extends Object implements Cloneable, Serializable
The abstract base class for queries.

Instantiable subclasses are:

  • {@link TermQuery}
  • {@link MultiTermQuery}
  • {@link BooleanQuery}
  • {@link WildcardQuery}
  • {@link PhraseQuery}
  • {@link PrefixQuery}
  • {@link MultiPhraseQuery}
  • {@link FuzzyQuery}
  • {@link RangeQuery}
  • {@link org.apache.lucene.search.spans.SpanQuery}

A parser for queries is contained in:

  • {@link org.apache.lucene.queryParser.QueryParser QueryParser}

Fields Summary
private float
boost
Constructors Summary
Methods Summary
public java.lang.Objectclone()
Returns a clone of this query.

    try {
      return (Query)super.clone();
    } catch (CloneNotSupportedException e) {
      throw new RuntimeException("Clone not supported: " + e.getMessage());
    }
  
public org.apache.lucene.search.Querycombine(org.apache.lucene.search.Query[] queries)
Expert: called when re-writing queries under MultiSearcher. Create a single query suitable for use by all subsearchers (in 1-1 correspondence with queries). This is an optimization of the OR of all queries. We handle the common optimization cases of equal queries and overlapping clauses of boolean OR queries (as generated by MultiTermQuery.rewrite() and RangeQuery.rewrite()). Be careful overriding this method as queries[0] determines which method will be called and is not necessarily of the same type as the other queries.

    HashSet uniques = new HashSet();
    for (int i = 0; i < queries.length; i++) {
      Query query = queries[i];
      BooleanClause[] clauses = null;
      // check if we can split the query into clauses
      boolean splittable = (query instanceof BooleanQuery);
      if(splittable){
        BooleanQuery bq = (BooleanQuery) query;
        splittable = bq.isCoordDisabled();
        clauses = bq.getClauses();
        for (int j = 0; splittable && j < clauses.length; j++) {
          splittable = (clauses[j].getOccur() == BooleanClause.Occur.SHOULD);
        }
      }
      if(splittable){
        for (int j = 0; j < clauses.length; j++) {
          uniques.add(clauses[j].getQuery());
        }
      } else {
        uniques.add(query);
      }
    }
    // optimization: if we have just one query, just return it
    if(uniques.size() == 1){
        return (Query)uniques.iterator().next();
    }
    Iterator it = uniques.iterator();
    BooleanQuery result = new BooleanQuery(true);
    while (it.hasNext())
      result.add((Query) it.next(), BooleanClause.Occur.SHOULD);
    return result;
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)
Expert: Constructs an appropriate Weight implementation for this query.

Only implemented by primitive queries, which re-write to themselves.

    throw new UnsupportedOperationException();
  
public voidextractTerms(java.util.Set terms)
Expert: adds all terms occuring in this query to the terms set. Only works if this query is in its {@link #rewrite rewritten} form.

throws
UnsupportedOperationException if this query is not yet rewritten

    // needs to be implemented by query subclasses
    throw new UnsupportedOperationException();
  
public floatgetBoost()
Gets the boost for this clause. Documents matching this clause will (in addition to the normal weightings) have their score multiplied by b. The boost is 1.0 by default.

 return boost; 
public org.apache.lucene.search.SimilaritygetSimilarity(org.apache.lucene.search.Searcher searcher)
Expert: Returns the Similarity implementation to be used for this query. Subclasses may override this method to specify their own Similarity implementation, perhaps one that delegates through that of the Searcher. By default the Searcher's Similarity implementation is returned.

    return searcher.getSimilarity();
  
public static org.apache.lucene.search.QuerymergeBooleanQueries(org.apache.lucene.search.Query[] queries)
Expert: merges the clauses of a set of BooleanQuery's into a single BooleanQuery.

A utility for use by {@link #combine(Query[])} implementations.

    HashSet allClauses = new HashSet();
    for (int i = 0; i < queries.length; i++) {
      BooleanClause[] clauses = ((BooleanQuery)queries[i]).getClauses();
      for (int j = 0; j < clauses.length; j++) {
        allClauses.add(clauses[j]);
      }
    }

    boolean coordDisabled =
      queries.length==0? false : ((BooleanQuery)queries[0]).isCoordDisabled();
    BooleanQuery result = new BooleanQuery(coordDisabled);
    Iterator i = allClauses.iterator();
    while (i.hasNext()) {
      result.add((BooleanClause)i.next());
    }
    return result;
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)
Expert: called to re-write queries into primitive queries. For example, a PrefixQuery will be rewritten into a BooleanQuery that consists of TermQuerys.

    return this;
  
public voidsetBoost(float b)
Sets the boost for this query clause to b. Documents matching this clause will (in addition to the normal weightings) have their score multiplied by b.

                     // query boost factor

                                
       boost = b; 
public abstract java.lang.StringtoString(java.lang.String field)
Prints a query to a string, with field assumed to be the default field and omitted.

The representation used is one that is supposed to be readable by {@link org.apache.lucene.queryParser.QueryParser QueryParser}. However, there are the following limitations:

  • If the query was created by the parser, the printed representation may not be exactly what was parsed. For example, characters that need to be escaped will be represented without the required backslash.
  • Some of the more complicated queries (e.g. span queries) don't have a representation that can be parsed by QueryParser.

public java.lang.StringtoString()
Prints a query to a string.

    return toString("");
  
public org.apache.lucene.search.Weightweight(org.apache.lucene.search.Searcher searcher)
Expert: Constructs and initializes a Weight for a top-level query.

    Query query = searcher.rewrite(this);
    Weight weight = query.createWeight(searcher);
    float sum = weight.sumOfSquaredWeights();
    float norm = getSimilarity(searcher).queryNorm(sum);
    weight.normalize(norm);
    return weight;