Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / RangeQuery.cs
blob047378786d3875c4f3f47dc7b68cfd1118e4ca00
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 using TermEnum = Lucene.Net.Index.TermEnum;
20 namespace Lucene.Net.Search
23 /// <summary> A Query that matches documents within an exclusive range.
24 ///
25 /// </summary>
26 /// <version> $Id: RangeQuery.cs,v 1.2 2005/01/17 19:54:30 joeshaw Exp $
27 /// </version>
28 [Serializable]
29 public class RangeQuery:Query
31 private Term lowerTerm;
32 private Term upperTerm;
33 private bool inclusive;
35 /// <summary>Constructs a query selecting all terms greater than
36 /// <code>lowerTerm</code> but less than <code>upperTerm</code>.
37 /// There must be at least one term and either term may be null,
38 /// in which case there is no bound on that side, but if there are
39 /// two terms, both terms <b>must</b> be for the same Field.
40 /// </summary>
41 public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive)
43 if (lowerTerm == null && upperTerm == null)
45 throw new System.ArgumentException("At least one term must be non-null");
47 if (lowerTerm != null && upperTerm != null && (System.Object) lowerTerm.Field() != (System.Object) upperTerm.Field())
49 throw new System.ArgumentException("Both terms must be for the same Field");
52 // if we have a lowerTerm, start there. otherwise, start at beginning
53 if (lowerTerm != null)
55 this.lowerTerm = lowerTerm;
57 else
59 this.lowerTerm = new Term(upperTerm.Field(), "");
62 this.upperTerm = upperTerm;
63 this.inclusive = inclusive;
66 /// <summary> FIXME: Describe <code>rewrite</code> method here.
67 ///
68 /// </summary>
69 /// <param name="reader">an <code>IndexReader</code> value
70 /// </param>
71 /// <returns> a <code>Query</code> value
72 /// </returns>
73 /// <exception cref=""> IOException if an error occurs
74 /// </exception>
75 public override Query Rewrite(IndexReader reader)
78 BooleanQuery query = new BooleanQuery();
79 TermEnum enumerator = reader.Terms(lowerTerm);
81 try
84 bool checkLower = false;
85 if (!inclusive)
86 // make adjustments to set to exclusive
87 checkLower = true;
89 System.String testField = GetField();
91 do
93 Term term = enumerator.Term();
94 if (term != null && (System.Object) term.Field() == (System.Object) testField)
96 if (!checkLower || String.CompareOrdinal(term.Text(), lowerTerm.Text()) > 0)
98 checkLower = false;
99 if (upperTerm != null)
101 int compare = String.CompareOrdinal(upperTerm.Text(), term.Text());
102 /* if beyond the upper term, or is exclusive and
103 * this is equal to the upper term, break out */
104 if ((compare < 0) || (!inclusive && compare == 0))
105 break;
107 TermQuery tq = new TermQuery(term); // found a match
108 tq.SetBoost(GetBoost()); // set the boost
109 query.Add(tq, false, false); // add to query
112 else
114 break;
117 while (enumerator.Next());
119 finally
121 enumerator.Close();
123 return query;
126 public override Query Combine(Query[] queries)
128 return Query.MergeBooleanQueries(queries);
131 /// <summary>Returns the Field name for this query </summary>
132 public virtual System.String GetField()
134 return (lowerTerm != null?lowerTerm.Field():upperTerm.Field());
137 /// <summary>Returns the lower term of this range query </summary>
138 public virtual Term GetLowerTerm()
140 return lowerTerm;
143 /// <summary>Returns the upper term of this range query </summary>
144 public virtual Term GetUpperTerm()
146 return upperTerm;
149 /// <summary>Returns <code>true</code> if the range query is inclusive </summary>
150 public virtual bool IsInclusive()
152 return inclusive;
156 /// <summary>Prints a user-readable version of this query. </summary>
157 public override System.String ToString(System.String field)
159 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
160 if (!GetField().Equals(field))
162 buffer.Append(GetField());
163 buffer.Append(":");
165 buffer.Append(inclusive?"[":"{");
166 buffer.Append(lowerTerm != null?lowerTerm.Text():"null");
167 buffer.Append(" TO ");
168 buffer.Append(upperTerm != null?upperTerm.Text():"null");
169 buffer.Append(inclusive?"]":"}");
170 if (GetBoost() != 1.0f)
172 System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
173 nfi.NumberDecimalDigits = 1;
175 buffer.Append("^");
176 buffer.Append(GetBoost().ToString("N", nfi));
178 return buffer.ToString();
180 override public System.Object Clone()
182 return null;