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.
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.
29 /// <p>For this to work, documents must have been indexed with a
30 /// {@link DateField}.</p>
33 /// <deprecated> Instead, use {@link RangeFilter} combined with
34 /// {@link Lucene.Net.document.DateTools}.
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
)
55 /// <summary> Constructs a filter for field <code>f</code> matching dates
56 /// between <code>from</code> and <code>to</code> inclusively.
58 public DateFilter(System
.String f
, System
.DateTime
from, System
.DateTime to
)
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.
69 public DateFilter(System
.String f
, long from, long to
)
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>.
80 public static DateFilter
Before(System
.String field
, System
.DateTime date
)
82 DateFilter result
= new DateFilter(field
);
83 result
.end
= DateField
.DateToString(date
);
87 /// <summary> Constructs a filter for field <code>f</code> matching times
88 /// on or before <code>time</code>.
90 public static DateFilter
Before(System
.String field
, long time
)
92 DateFilter result
= new DateFilter(field
);
93 result
.end
= DateField
.TimeToString(time
);
97 /// <summary> Constructs a filter for field <code>f</code> matching
98 /// dates on or after <code>date</code>.
100 public static DateFilter
After(System
.String field
, System
.DateTime date
)
102 DateFilter result
= new DateFilter(field
);
103 result
.start
= DateField
.DateToString(date
);
107 /// <summary> Constructs a filter for field <code>f</code> matching
108 /// times on or after <code>time</code>.
110 public static DateFilter
After(System
.String field
, long time
)
112 DateFilter result
= new DateFilter(field
);
113 result
.start
= DateField
.TimeToString(time
);
117 /// <summary> Returns a BitSet with true for documents which should be
118 /// permitted in search results, and false for those that should
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)
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())
155 public override System
.String
ToString()
157 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
158 buffer
.Append(field
);
160 buffer
.Append(DateField
.StringToDate(start
).ToString("r"));
162 buffer
.Append(DateField
.StringToDate(end
).ToString("r"));
163 return buffer
.ToString();