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}.
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
)
49 /// <summary> Constructs a filter for Field <code>f</code> matching dates
50 /// between <code>from</code> and <code>to</code> inclusively.
52 public DateFilter(System
.String f
, System
.DateTime
from, System
.DateTime to
)
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.
63 public DateFilter(System
.String f
, long from, long to
)
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>.
74 public static DateFilter
Before(System
.String field
, System
.DateTime date
)
76 DateFilter result
= new DateFilter(field
);
77 result
.end
= DateField
.DateToString(date
);
81 /// <summary> Constructs a filter for Field <code>f</code> matching times
82 /// on or before <code>time</code>.
84 public static DateFilter
Before(System
.String field
, long time
)
86 DateFilter result
= new DateFilter(field
);
87 result
.end
= DateField
.TimeToString(time
);
91 /// <summary> Constructs a filter for Field <code>f</code> matching
92 /// dates on or after <code>date</code>.
94 public static DateFilter
After(System
.String field
, System
.DateTime date
)
96 DateFilter result
= new DateFilter(field
);
97 result
.start
= DateField
.DateToString(date
);
101 /// <summary> Constructs a filter for Field <code>f</code> matching
102 /// times on or after <code>time</code>.
104 public static DateFilter
After(System
.String field
, long time
)
106 DateFilter result
= new DateFilter(field
);
107 result
.start
= DateField
.TimeToString(time
);
111 /// <summary> Returns a BitSet with true for documents which should be
112 /// permitted in search results, and false for those that should
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)
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())
149 public override System
.String
ToString()
151 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
152 buffer
.Append(field
);
154 buffer
.Append(DateField
.StringToDate(start
).ToString("r"));
156 buffer
.Append(DateField
.StringToDate(end
).ToString("r"));
157 return buffer
.ToString();