Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Search / DateFilter.cs
blob76a128fecddab678681fffd69932934fd7b2525e
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 DateField = Lucene.Net.Documents.DateField;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
20 using TermDocs = Lucene.Net.Index.TermDocs;
21 using TermEnum = Lucene.Net.Index.TermEnum;
22 namespace Lucene.Net.Search
25 /// <summary> A Filter that restricts search results to a range of time.
26 ///
27 /// <p>For this to work, documents must have been indexed with a
28 /// {@link DateField}.</p>
29 ///
30 /// </summary>
31 /// <deprecated> Instead, use {@link RangeFilter} combined with
32 /// {@link Lucene.Net.document.DateTools}.
33 /// </deprecated>
34 [Serializable]
35 public class DateFilter:Filter
37 private void InitBlock()
39 start = DateField.MIN_DATE_STRING();
40 end = DateField.MAX_DATE_STRING();
42 internal System.String field;
44 internal System.String start;
45 internal System.String end;
47 private DateFilter(System.String f)
49 InitBlock();
50 field = f;
53 /// <summary> Constructs a filter for Field <code>f</code> matching dates
54 /// between <code>from</code> and <code>to</code> inclusively.
55 /// </summary>
56 public DateFilter(System.String f, System.DateTime from, System.DateTime to)
58 InitBlock();
59 field = f;
60 start = DateField.DateToString(from);
61 end = DateField.DateToString(to);
64 /// <summary> Constructs a filter for Field <code>f</code> matching times
65 /// between <code>from</code> and <code>to</code> inclusively.
66 /// </summary>
67 public DateFilter(System.String f, long from, long to)
69 InitBlock();
70 field = f;
71 start = DateField.TimeToString(from);
72 end = DateField.TimeToString(to);
75 /// <summary> Constructs a filter for Field <code>f</code> matching
76 /// dates on or before before <code>date</code>.
77 /// </summary>
78 public static DateFilter Before(System.String field, System.DateTime date)
80 DateFilter result = new DateFilter(field);
81 result.end = DateField.DateToString(date);
82 return result;
85 /// <summary> Constructs a filter for Field <code>f</code> matching times
86 /// on or before <code>time</code>.
87 /// </summary>
88 public static DateFilter Before(System.String field, long time)
90 DateFilter result = new DateFilter(field);
91 result.end = DateField.TimeToString(time);
92 return result;
95 /// <summary> Constructs a filter for Field <code>f</code> matching
96 /// dates on or after <code>date</code>.
97 /// </summary>
98 public static DateFilter After(System.String field, System.DateTime date)
100 DateFilter result = new DateFilter(field);
101 result.start = DateField.DateToString(date);
102 return result;
105 /// <summary> Constructs a filter for Field <code>f</code> matching
106 /// times on or after <code>time</code>.
107 /// </summary>
108 public static DateFilter After(System.String field, long time)
110 DateFilter result = new DateFilter(field);
111 result.start = DateField.TimeToString(time);
112 return result;
115 /// <summary> Returns a BitSet with true for documents which should be
116 /// permitted in search results, and false for those that should
117 /// not.
118 /// </summary>
119 public override System.Collections.BitArray Bits(IndexReader reader)
121 System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);
122 TermEnum enumerator = reader.Terms(new Term(field, start));
123 TermDocs termDocs = reader.TermDocs();
124 if (enumerator.Term() == null)
126 return bits;
131 Term stop = new Term(field, end);
132 while (enumerator.Term().CompareTo(stop) <= 0)
134 termDocs.Seek(enumerator.Term());
135 while (termDocs.Next())
137 bits.Set(termDocs.Doc(), true);
139 if (!enumerator.Next())
141 break;
145 finally
147 enumerator.Close();
148 termDocs.Close();
150 return bits;
153 public override System.String ToString()
155 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
156 buffer.Append(field);
157 buffer.Append(":");
158 buffer.Append(DateField.StringToDate(start).ToString("r"));
159 buffer.Append("-");
160 buffer.Append(DateField.StringToDate(end).ToString("r"));
161 return buffer.ToString();