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 org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.util.ToStringUtils;
24 import java.io.IOException;
26 /** Implements the wildcard search query. Supported wildcards are <code>*</code>, which
27 * matches any character sequence (including the empty one), and <code>?</code>,
28 * which matches any single character. Note this query can be slow, as it
29 * needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
30 * a Wildcard term should not start with one of the wildcards <code>*</code> or
33 * <p>This query uses the {@link
34 * MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
37 * @see WildcardTermEnum */
38 public class WildcardQuery extends MultiTermQuery {
39 private boolean termContainsWildcard;
40 private boolean termIsPrefix;
43 public WildcardQuery(Term term) {
45 String text = term.text();
46 this.termContainsWildcard = (text.indexOf('*') != -1)
47 || (text.indexOf('?') != -1);
48 this.termIsPrefix = termContainsWildcard
49 && (text.indexOf('?') == -1)
50 && (text.indexOf('*') == text.length() - 1);
54 protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
56 return new PrefixTermEnum(reader, term.createTerm(term.text()
57 .substring(0, term.text().indexOf('*'))));
58 } else if (termContainsWildcard) {
59 return new WildcardTermEnum(reader, getTerm());
61 return new SingleTermEnum(reader, getTerm());
66 * Returns the pattern term.
68 public Term getTerm() {
72 /** Prints a user-readable version of this query. */
74 public String toString(String field) {
75 StringBuilder buffer = new StringBuilder();
76 if (!term.field().equals(field)) {
77 buffer.append(term.field());
80 buffer.append(term.text());
81 buffer.append(ToStringUtils.boost(getBoost()));
82 return buffer.toString();
86 public int hashCode() {
88 int result = super.hashCode();
89 result = prime * result + ((term == null) ? 0 : term.hashCode());
94 public boolean equals(Object obj) {
97 if (!super.equals(obj))
99 if (getClass() != obj.getClass())
101 WildcardQuery other = (WildcardQuery) obj;
103 if (other.term != null)
105 } else if (!term.equals(other.term))