Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
WildcardQuery.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using Term = Lucene.Net.Index.Term;
22 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary>Implements the wildcard search query. Supported wildcards are <c>*</c>, which
28  /// matches any character sequence (including the empty one), and <c>?</c>,
29  /// which matches any single character. Note this query can be slow, as it
30  /// needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
31  /// a Wildcard term should not start with one of the wildcards <c>*</c> or
32  /// <c>?</c>.
33  ///
34  /// <p/>This query uses the <see cref="MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT" />
35  ///
36  /// rewrite method.
37  ///
38  /// </summary>
39  /// <seealso cref="WildcardTermEnum">
40  /// </seealso>
41  [Serializable]
43  {
44  private readonly bool _termContainsWildcard;
45  private readonly bool _termIsPrefix;
46  protected internal Term internalTerm;
47 
48  public WildcardQuery(Term term)
49  {
50  this.internalTerm = term;
51  string text = term.Text;
52  _termContainsWildcard = (term.Text.IndexOf('*') != -1)
53  || (term.Text.IndexOf('?') != -1);
54  _termIsPrefix = _termContainsWildcard
55  && (text.IndexOf('?') == -1)
56  && (text.IndexOf('*') == text.Length - 1);
57  }
58 
59  protected internal override FilteredTermEnum GetEnum(IndexReader reader)
60  {
61  if (_termContainsWildcard)
62  {
63  return new WildcardTermEnum(reader, Term);
64  }
65  else
66  {
67  return new SingleTermEnum(reader, Term);
68  }
69  }
70 
71  /// <summary> Returns the pattern term.</summary>
72  public Term Term
73  {
74  get { return internalTerm; }
75  }
76 
77  public override Query Rewrite(IndexReader reader)
78  {
79  if (_termIsPrefix)
80  {
81  MultiTermQuery rewritten =
82  new PrefixQuery(internalTerm.CreateTerm(internalTerm.Text.Substring(0, internalTerm.Text.IndexOf('*'))));
83  rewritten.Boost = Boost;
84  rewritten.RewriteMethod = RewriteMethod;
85  return rewritten;
86  }
87  else
88  {
89  return base.Rewrite(reader);
90  }
91  }
92 
93  /// <summary>Prints a user-readable version of this query. </summary>
94  public override System.String ToString(System.String field)
95  {
96  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
97  if (!internalTerm.Field.Equals(field))
98  {
99  buffer.Append(internalTerm.Field);
100  buffer.Append(":");
101  }
102  buffer.Append(internalTerm.Text);
103  buffer.Append(ToStringUtils.Boost(Boost));
104  return buffer.ToString();
105  }
106 
107  //@Override
108  public override int GetHashCode()
109  {
110  int prime = 31;
111  int result = base.GetHashCode();
112  result = prime * result + ((internalTerm == null)?0:internalTerm.GetHashCode());
113  return result;
114  }
115 
116  //@Override
117  public override bool Equals(System.Object obj)
118  {
119  if (this == obj)
120  return true;
121  if (!base.Equals(obj))
122  return false;
123  if (GetType() != obj.GetType())
124  return false;
125  WildcardQuery other = (WildcardQuery) obj;
126  if (internalTerm == null)
127  {
128  if (other.internalTerm != null)
129  return false;
130  }
131  else if (!internalTerm.Equals(other.internalTerm))
132  return false;
133  return true;
134  }
135  }
136 }