Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / DateFilter.cs
blobf0e7d5813bb678a4689576a1fdba8b9faa50c7c3
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}.
29 /// </summary>
30 [Serializable]
31 public class DateFilter:Filter
33 private void InitBlock()
35 start = DateField.MIN_DATE_STRING();
36 end = DateField.MAX_DATE_STRING();
38 internal System.String field;
40 internal System.String start;
41 internal System.String end;
43 private DateFilter(System.String f)
45 InitBlock();
46 field = f;
49 /// <summary> Constructs a filter for Field <code>f</code> matching dates
50 /// between <code>from</code> and <code>to</code> inclusively.
51 /// </summary>
52 public DateFilter(System.String f, System.DateTime from, System.DateTime to)
54 InitBlock();
55 field = f;
56 start = DateField.DateToString(from);
57 end = DateField.DateToString(to);
60 /// <summary> Constructs a filter for Field <code>f</code> matching times
61 /// between <code>from</code> and <code>to</code> inclusively.
62 /// </summary>
63 public DateFilter(System.String f, long from, long to)
65 InitBlock();
66 field = f;
67 start = DateField.TimeToString(from);
68 end = DateField.TimeToString(to);
71 /// <summary> Constructs a filter for Field <code>f</code> matching
72 /// dates on or before before <code>date</code>.
73 /// </summary>
74 public static DateFilter Before(System.String field, System.DateTime date)
76 DateFilter result = new DateFilter(field);
77 result.end = DateField.DateToString(date);
78 return result;
81 /// <summary> Constructs a filter for Field <code>f</code> matching times
82 /// on or before <code>time</code>.
83 /// </summary>
84 public static DateFilter Before(System.String field, long time)
86 DateFilter result = new DateFilter(field);
87 result.end = DateField.TimeToString(time);
88 return result;
91 /// <summary> Constructs a filter for Field <code>f</code> matching
92 /// dates on or after <code>date</code>.
93 /// </summary>
94 public static DateFilter After(System.String field, System.DateTime date)
96 DateFilter result = new DateFilter(field);
97 result.start = DateField.DateToString(date);
98 return result;
101 /// <summary> Constructs a filter for Field <code>f</code> matching
102 /// times on or after <code>time</code>.
103 /// </summary>
104 public static DateFilter After(System.String field, long time)
106 DateFilter result = new DateFilter(field);
107 result.start = DateField.TimeToString(time);
108 return result;
111 /// <summary> Returns a BitSet with true for documents which should be
112 /// permitted in search results, and false for those that should
113 /// not.
114 /// </summary>
115 public override System.Collections.BitArray Bits(IndexReader reader)
117 System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);
118 TermEnum enumerator = reader.Terms(new Term(field, start));
119 TermDocs termDocs = reader.TermDocs();
120 if (enumerator.Term() == null)
122 return bits;
127 Term stop = new Term(field, end);
128 while (enumerator.Term().CompareTo(stop) <= 0)
130 termDocs.Seek(enumerator.Term());
131 while (termDocs.Next())
133 bits.Set(termDocs.Doc(), true);
135 if (!enumerator.Next())
137 break;
141 finally
143 enumerator.Close();
144 termDocs.Close();
146 return bits;
149 public override System.String ToString()
151 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
152 buffer.Append(field);
153 buffer.Append(":");
154 buffer.Append(DateField.StringToDate(start).ToString("r"));
155 buffer.Append("-");
156 buffer.Append(DateField.StringToDate(end).ToString("r"));
157 return buffer.ToString();