1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.util.ToStringUtils;
26 /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
27 * is built by QueryParser for input like <code>app*</code>.
29 * <p>This query uses the {@link
30 * MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
32 public class PrefixQuery extends MultiTermQuery {
35 /** Constructs a query for terms starting with <code>prefix</code>. */
36 public PrefixQuery(Term prefix) {
40 /** Returns the prefix of this query. */
41 public Term getPrefix() { return prefix; }
44 protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
45 return new PrefixTermEnum(reader, prefix);
48 /** Prints a user-readable version of this query. */
50 public String toString(String field) {
51 StringBuilder buffer = new StringBuilder();
52 if (!prefix.field().equals(field)) {
53 buffer.append(prefix.field());
56 buffer.append(prefix.text());
58 buffer.append(ToStringUtils.boost(getBoost()));
59 return buffer.toString();
63 public int hashCode() {
65 int result = super.hashCode();
66 result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
71 public boolean equals(Object obj) {
74 if (!super.equals(obj))
76 if (getClass() != obj.getClass())
78 PrefixQuery other = (PrefixQuery) obj;
80 if (other.prefix != null)
82 } else if (!prefix.equals(other.prefix))