cvsimport
[beagle.git] / beagled / Lucene.Net / Search / DateFilter.cs
blobad11e33c63a780bdbfc670c33e7f196ab371d45c
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.
17 using System;
18 using DateField = Lucene.Net.Documents.DateField;
19 using IndexReader = Lucene.Net.Index.IndexReader;
20 using Term = Lucene.Net.Index.Term;
21 using TermDocs = Lucene.Net.Index.TermDocs;
22 using TermEnum = Lucene.Net.Index.TermEnum;
24 namespace Lucene.Net.Search
27 /// <summary> A Filter that restricts search results to a range of time.
28 ///
29 /// <p>For this to work, documents must have been indexed with a
30 /// {@link DateField}.</p>
31 ///
32 /// </summary>
33 /// <deprecated> Instead, use {@link RangeFilter} combined with
34 /// {@link Lucene.Net.document.DateTools}.
35 /// </deprecated>
36 [Serializable]
37 public class DateFilter : Filter
39 private void InitBlock()
41 start = DateField.MIN_DATE_STRING();
42 end = DateField.MAX_DATE_STRING();
44 internal System.String field;
46 internal System.String start;
47 internal System.String end;
49 private DateFilter(System.String f)
51 InitBlock();
52 field = f;
55 /// <summary> Constructs a filter for field <code>f</code> matching dates
56 /// between <code>from</code> and <code>to</code> inclusively.
57 /// </summary>
58 public DateFilter(System.String f, System.DateTime from, System.DateTime to)
60 InitBlock();
61 field = f;
62 start = DateField.DateToString(from);
63 end = DateField.DateToString(to);
66 /// <summary> Constructs a filter for field <code>f</code> matching times
67 /// between <code>from</code> and <code>to</code> inclusively.
68 /// </summary>
69 public DateFilter(System.String f, long from, long to)
71 InitBlock();
72 field = f;
73 start = DateField.TimeToString(from);
74 end = DateField.TimeToString(to);
77 /// <summary> Constructs a filter for field <code>f</code> matching
78 /// dates on or before before <code>date</code>.
79 /// </summary>
80 public static DateFilter Before(System.String field, System.DateTime date)
82 DateFilter result = new DateFilter(field);
83 result.end = DateField.DateToString(date);
84 return result;
87 /// <summary> Constructs a filter for field <code>f</code> matching times
88 /// on or before <code>time</code>.
89 /// </summary>
90 public static DateFilter Before(System.String field, long time)
92 DateFilter result = new DateFilter(field);
93 result.end = DateField.TimeToString(time);
94 return result;
97 /// <summary> Constructs a filter for field <code>f</code> matching
98 /// dates on or after <code>date</code>.
99 /// </summary>
100 public static DateFilter After(System.String field, System.DateTime date)
102 DateFilter result = new DateFilter(field);
103 result.start = DateField.DateToString(date);
104 return result;
107 /// <summary> Constructs a filter for field <code>f</code> matching
108 /// times on or after <code>time</code>.
109 /// </summary>
110 public static DateFilter After(System.String field, long time)
112 DateFilter result = new DateFilter(field);
113 result.start = DateField.TimeToString(time);
114 return result;
117 /// <summary> Returns a BitSet with true for documents which should be
118 /// permitted in search results, and false for those that should
119 /// not.
120 /// </summary>
121 public override System.Collections.BitArray Bits(IndexReader reader)
123 System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);
124 TermEnum enumerator = reader.Terms(new Term(field, start));
125 TermDocs termDocs = reader.TermDocs();
126 if (enumerator.Term() == null)
128 return bits;
133 Term stop = new Term(field, end);
134 while (enumerator.Term().CompareTo(stop) <= 0)
136 termDocs.Seek(enumerator.Term());
137 while (termDocs.Next())
139 bits.Set(termDocs.Doc(), true);
141 if (!enumerator.Next())
143 break;
147 finally
149 enumerator.Close();
150 termDocs.Close();
152 return bits;
155 public override System.String ToString()
157 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
158 buffer.Append(field);
159 buffer.Append(":");
160 buffer.Append(DateField.StringToDate(start).ToString("r"));
161 buffer.Append("-");
162 buffer.Append(DateField.StringToDate(end).ToString("r"));
163 return buffer.ToString();