01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
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.
18: */
19:
20: import org.apache.lucene.index.IndexReader;
21: import org.apache.lucene.index.Term;
22: import java.io.IOException;
23:
24: /** Implements the wildcard search query. Supported wildcards are <code>*</code>, which
25: * matches any character sequence (including the empty one), and <code>?</code>,
26: * which matches any single character. Note this query can be slow, as it
27: * needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
28: * a Wildcard term should not start with one of the wildcards <code>*</code> or
29: * <code>?</code>.
30: *
31: * @see WildcardTermEnum
32: */
33: public class WildcardQuery extends MultiTermQuery {
34: private boolean termContainsWildcard;
35:
36: public WildcardQuery(Term term) {
37: super (term);
38: this .termContainsWildcard = (term.text().indexOf('*') != -1)
39: || (term.text().indexOf('?') != -1);
40: }
41:
42: protected FilteredTermEnum getEnum(IndexReader reader)
43: throws IOException {
44: return new WildcardTermEnum(reader, getTerm());
45: }
46:
47: public boolean equals(Object o) {
48: if (o instanceof WildcardQuery)
49: return super .equals(o);
50:
51: return false;
52: }
53:
54: public Query rewrite(IndexReader reader) throws IOException {
55: if (this .termContainsWildcard) {
56: return super .rewrite(reader);
57: }
58:
59: return new TermQuery(getTerm());
60: }
61: }
|