2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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.
27 /// <p>For this to work, documents must have been indexed with a
28 /// {@link DateField}.</p>
31 /// <deprecated> Instead, use {@link RangeFilter} combined with
32 /// {@link Lucene.Net.document.DateTools}.
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
)
53 /// <summary> Constructs a filter for Field <code>f</code> matching dates
54 /// between <code>from</code> and <code>to</code> inclusively.
56 public DateFilter(System
.String f
, System
.DateTime
from, System
.DateTime to
)
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.
67 public DateFilter(System
.String f
, long from, long to
)
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>.
78 public static DateFilter
Before(System
.String field
, System
.DateTime date
)
80 DateFilter result
= new DateFilter(field
);
81 result
.end
= DateField
.DateToString(date
);
85 /// <summary> Constructs a filter for Field <code>f</code> matching times
86 /// on or before <code>time</code>.
88 public static DateFilter
Before(System
.String field
, long time
)
90 DateFilter result
= new DateFilter(field
);
91 result
.end
= DateField
.TimeToString(time
);
95 /// <summary> Constructs a filter for Field <code>f</code> matching
96 /// dates on or after <code>date</code>.
98 public static DateFilter
After(System
.String field
, System
.DateTime date
)
100 DateFilter result
= new DateFilter(field
);
101 result
.start
= DateField
.DateToString(date
);
105 /// <summary> Constructs a filter for Field <code>f</code> matching
106 /// times on or after <code>time</code>.
108 public static DateFilter
After(System
.String field
, long time
)
110 DateFilter result
= new DateFilter(field
);
111 result
.start
= DateField
.TimeToString(time
);
115 /// <summary> Returns a BitSet with true for documents which should be
116 /// permitted in search results, and false for those that should
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)
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())
153 public override System
.String
ToString()
155 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
156 buffer
.Append(field
);
158 buffer
.Append(DateField
.StringToDate(start
).ToString("r"));
160 buffer
.Append(DateField
.StringToDate(end
).ToString("r"));
161 return buffer
.ToString();