| package org.apache.lucene.search; |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| import java.io.IOException; |
| |
| import org.apache.lucene.index.IndexReader; |
| |
| /** The abstract base class for queries. |
| <p>Instantiable subclasses are: |
| <ul> |
| <li> {@link TermQuery} |
| <li> {@link BooleanQuery} |
| <li> {@link WildcardQuery} |
| <li> {@link PhraseQuery} |
| <li> {@link PrefixQuery} |
| <li> {@link MultiPhraseQuery} |
| <li> {@link FuzzyQuery} |
| <li> {@link RegexpQuery} |
| <li> {@link TermRangeQuery} |
| <li> {@link NumericRangeQuery} |
| <li> {@link ConstantScoreQuery} |
| <li> {@link DisjunctionMaxQuery} |
| <li> {@link MatchAllDocsQuery} |
| </ul> |
| <p>See also the family of {@link org.apache.lucene.search.spans Span Queries} |
| and additional queries available in the <a href="{@docRoot}/../queries/overview-summary.html">Queries module</a> |
| */ |
| public abstract class Query implements Cloneable { |
| private float boost = 1.0f; // query boost factor |
| |
| /** Sets the boost for this query clause to <code>b</code>. |
| * @deprecated Use {@link BoostQuery} instead to apply boosts. |
| */ |
| @Deprecated |
| public void setBoost(float b) { boost = b; } |
| |
| /** Gets the boost for this clause. |
| * @deprecated Use {@link BoostQuery} instead to apply boosts. |
| */ |
| @Deprecated |
| public float getBoost() { return boost; } |
| |
| /** Prints a query to a string, with <code>field</code> assumed to be the |
| * default field and omitted. |
| */ |
| public abstract String toString(String field); |
| |
| /** Prints a query to a string. */ |
| @Override |
| public final String toString() { |
| return toString(""); |
| } |
| |
| /** |
| * Expert: Constructs an appropriate Weight implementation for this query. |
| * <p> |
| * Only implemented by primitive queries, which re-write to themselves. |
| * |
| * @param needsScores True if document scores ({@link Scorer#score}) or match |
| * frequencies ({@link Scorer#freq}) are needed. |
| */ |
| public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { |
| throw new UnsupportedOperationException("Query " + this + " does not implement createWeight"); |
| } |
| |
| /** Expert: called to re-write queries into primitive queries. For example, |
| * a PrefixQuery will be rewritten into a BooleanQuery that consists |
| * of TermQuerys. |
| */ |
| public Query rewrite(IndexReader reader) throws IOException { |
| if (boost != 1f) { |
| Query rewritten = clone(); |
| rewritten.setBoost(1f); |
| return new BoostQuery(rewritten, boost); |
| } |
| return this; |
| } |
| |
| /** Returns a clone of this query. |
| * @deprecated Cloning was only useful for modifying boosts. Now that |
| * {@link #setBoost(float)} is deprecated, queries should be considered |
| * immutable. */ |
| @Deprecated |
| @Override |
| public Query clone() { |
| try { |
| return (Query)super.clone(); |
| } catch (CloneNotSupportedException e) { |
| throw new RuntimeException("Clone not supported: " + e.getMessage()); |
| } |
| } |
| |
| @Override |
| public int hashCode() { |
| return Float.floatToIntBits(boost) ^ getClass().hashCode(); |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (obj == null) |
| return false; |
| if (getClass() != obj.getClass()) |
| return false; |
| Query other = (Query) obj; |
| return Float.floatToIntBits(boost) == Float.floatToIntBits(other.boost); |
| } |
| } |