1
package org.apache.lucene.search;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.store.Directory;
21
import org.apache.lucene.util.LuceneTestCase;
23
import org.apache.lucene.document.DateTools;
24
import org.apache.lucene.document.Document;
25
import org.apache.lucene.document.Field;
26
import org.apache.lucene.index.IndexReader;
27
import org.apache.lucene.index.RandomIndexWriter;
28
import org.apache.lucene.index.Term;
30
import java.io.IOException;
33
* DateFilter JUnit tests.
36
* @version $Revision: 1066722 $
38
public class TestDateFilter extends LuceneTestCase {
43
public void testBefore() throws IOException {
45
Directory indexStore = newDirectory();
46
RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
48
long now = System.currentTimeMillis();
50
Document doc = new Document();
51
// add time that is in the past
52
doc.add(newField("datefield", DateTools.timeToString(now - 1000,
53
DateTools.Resolution.MILLISECOND), Field.Store.YES,
54
Field.Index.NOT_ANALYZED));
55
doc.add(newField("body", "Today is a very sunny day in New York City",
56
Field.Store.YES, Field.Index.ANALYZED));
57
writer.addDocument(doc);
59
IndexReader reader = writer.getReader();
61
IndexSearcher searcher = newSearcher(reader);
63
// filter that should preserve matches
64
// DateFilter df1 = DateFilter.Before("datefield", now);
65
TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools
66
.timeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools
67
.timeToString(now, DateTools.Resolution.MILLISECOND), false, true);
68
// filter that should discard matches
69
// DateFilter df2 = DateFilter.Before("datefield", now - 999999);
70
TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools
71
.timeToString(0, DateTools.Resolution.MILLISECOND), DateTools
72
.timeToString(now - 2000, DateTools.Resolution.MILLISECOND), true,
75
// search something that doesn't exist with DateFilter
76
Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
78
// search for something that does exists
79
Query query2 = new TermQuery(new Term("body", "sunny"));
83
// ensure that queries return expected results without DateFilter first
84
result = searcher.search(query1, null, 1000).scoreDocs;
85
assertEquals(0, result.length);
87
result = searcher.search(query2, null, 1000).scoreDocs;
88
assertEquals(1, result.length);
90
// run queries with DateFilter
91
result = searcher.search(query1, df1, 1000).scoreDocs;
92
assertEquals(0, result.length);
94
result = searcher.search(query1, df2, 1000).scoreDocs;
95
assertEquals(0, result.length);
97
result = searcher.search(query2, df1, 1000).scoreDocs;
98
assertEquals(1, result.length);
100
result = searcher.search(query2, df2, 1000).scoreDocs;
101
assertEquals(0, result.length);
110
public void testAfter() throws IOException {
112
Directory indexStore = newDirectory();
113
RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
115
long now = System.currentTimeMillis();
117
Document doc = new Document();
118
// add time that is in the future
119
doc.add(newField("datefield", DateTools.timeToString(now + 888888,
120
DateTools.Resolution.MILLISECOND), Field.Store.YES,
121
Field.Index.NOT_ANALYZED));
122
doc.add(newField("body", "Today is a very sunny day in New York City",
123
Field.Store.YES, Field.Index.ANALYZED));
124
writer.addDocument(doc);
126
IndexReader reader = writer.getReader();
128
IndexSearcher searcher = newSearcher(reader);
130
// filter that should preserve matches
131
// DateFilter df1 = DateFilter.After("datefield", now);
132
TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools
133
.timeToString(now, DateTools.Resolution.MILLISECOND), DateTools
134
.timeToString(now + 999999, DateTools.Resolution.MILLISECOND), true,
136
// filter that should discard matches
137
// DateFilter df2 = DateFilter.After("datefield", now + 999999);
138
TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools
139
.timeToString(now + 999999, DateTools.Resolution.MILLISECOND),
140
DateTools.timeToString(now + 999999999,
141
DateTools.Resolution.MILLISECOND), false, true);
143
// search something that doesn't exist with DateFilter
144
Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
146
// search for something that does exists
147
Query query2 = new TermQuery(new Term("body", "sunny"));
151
// ensure that queries return expected results without DateFilter first
152
result = searcher.search(query1, null, 1000).scoreDocs;
153
assertEquals(0, result.length);
155
result = searcher.search(query2, null, 1000).scoreDocs;
156
assertEquals(1, result.length);
158
// run queries with DateFilter
159
result = searcher.search(query1, df1, 1000).scoreDocs;
160
assertEquals(0, result.length);
162
result = searcher.search(query1, df2, 1000).scoreDocs;
163
assertEquals(0, result.length);
165
result = searcher.search(query2, df1, 1000).scoreDocs;
166
assertEquals(1, result.length);
168
result = searcher.search(query2, df2, 1000).scoreDocs;
169
assertEquals(0, result.length);