KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestDateFilter


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.index.IndexWriter;
21 import org.apache.lucene.search.Query;
22 import org.apache.lucene.search.Hits;
23 import org.apache.lucene.search.IndexSearcher;
24 import org.apache.lucene.store.RAMDirectory;
25 import org.apache.lucene.analysis.SimpleAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28 import org.apache.lucene.document.DateField;
29
30 import java.io.IOException JavaDoc;
31
32 import junit.framework.TestCase;
33
34  /**
35   * DateFilter JUnit tests.
36   *
37   * @author Otis Gospodnetic
38   * @version $Revision: 1.5 $
39   */

40 public class TestDateFilter
41     extends TestCase
42 {
43     public TestDateFilter(String JavaDoc name)
44     {
45     super(name);
46     }
47
48     /**
49      *
50      */

51     public static void testBefore()
52     throws IOException JavaDoc
53     {
54     // create an index
55
RAMDirectory indexStore = new RAMDirectory();
56         IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
57
58     long now = System.currentTimeMillis();
59
60     Document doc = new Document();
61     // add time that is in the past
62
doc.add(Field.Keyword("datefield", DateField.timeToString(now - 1000)));
63     doc.add(Field.Text("body", "Today is a very sunny day in New York City"));
64     writer.addDocument(doc);
65     writer.optimize();
66     writer.close();
67
68     IndexSearcher searcher = new IndexSearcher(indexStore);
69
70     // filter that should preserve matches
71
DateFilter df1 = DateFilter.Before("datefield", now);
72
73     // filter that should discard matches
74
DateFilter df2 = DateFilter.Before("datefield", now - 999999);
75
76     // search something that doesn't exist with DateFilter
77
Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
78
79     // search for something that does exists
80
Query query2 = new TermQuery(new Term("body", "sunny"));
81
82     Hits result;
83
84     // ensure that queries return expected results without DateFilter first
85
result = searcher.search(query1);
86     assertEquals(0, result.length());
87
88     result = searcher.search(query2);
89     assertEquals(1, result.length());
90
91
92     // run queries with DateFilter
93
result = searcher.search(query1, df1);
94     assertEquals(0, result.length());
95
96     result = searcher.search(query1, df2);
97     assertEquals(0, result.length());
98
99     result = searcher.search(query2, df1);
100     assertEquals(1, result.length());
101
102     result = searcher.search(query2, df2);
103     assertEquals(0, result.length());
104     }
105
106     /**
107      *
108      */

109     public static void testAfter()
110     throws IOException JavaDoc
111     {
112     // create an index
113
RAMDirectory indexStore = new RAMDirectory();
114         IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
115
116     long now = System.currentTimeMillis();
117
118     Document doc = new Document();
119     // add time that is in the future
120
doc.add(Field.Keyword("datefield", DateField.timeToString(now + 888888)));
121     doc.add(Field.Text("body", "Today is a very sunny day in New York City"));
122     writer.addDocument(doc);
123     writer.optimize();
124     writer.close();
125
126     IndexSearcher searcher = new IndexSearcher(indexStore);
127
128     // filter that should preserve matches
129
DateFilter df1 = DateFilter.After("datefield", now);
130
131     // filter that should discard matches
132
DateFilter df2 = DateFilter.After("datefield", now + 999999);
133
134     // search something that doesn't exist with DateFilter
135
Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
136
137     // search for something that does exists
138
Query query2 = new TermQuery(new Term("body", "sunny"));
139
140     Hits result;
141
142     // ensure that queries return expected results without DateFilter first
143
result = searcher.search(query1);
144     assertEquals(0, result.length());
145
146     result = searcher.search(query2);
147     assertEquals(1, result.length());
148
149
150     // run queries with DateFilter
151
result = searcher.search(query1, df1);
152     assertEquals(0, result.length());
153
154     result = searcher.search(query1, df2);
155     assertEquals(0, result.length());
156
157     result = searcher.search(query2, df1);
158     assertEquals(1, result.length());
159
160     result = searcher.search(query2, df2);
161     assertEquals(0, result.length());
162     }
163 }
164
Popular Tags