001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.analysis.SimpleAnalyzer;
022: import org.apache.lucene.document.DateTools;
023: import org.apache.lucene.document.Document;
024: import org.apache.lucene.document.Field;
025: import org.apache.lucene.index.IndexWriter;
026: import org.apache.lucene.index.Term;
027: import org.apache.lucene.store.RAMDirectory;
028:
029: import java.io.IOException;
030:
031: /**
032: * DateFilter JUnit tests.
033: *
034: *
035: * @version $Revision: 583534 $
036: */
037: public class TestDateFilter extends LuceneTestCase {
038: public TestDateFilter(String name) {
039: super (name);
040: }
041:
042: /**
043: *
044: */
045: public static void testBefore() throws IOException {
046: // create an index
047: RAMDirectory indexStore = new RAMDirectory();
048: IndexWriter writer = new IndexWriter(indexStore,
049: new SimpleAnalyzer(), true);
050:
051: long now = System.currentTimeMillis();
052:
053: Document doc = new Document();
054: // add time that is in the past
055: doc.add(new Field("datefield", DateTools.timeToString(
056: now - 1000, DateTools.Resolution.MILLISECOND),
057: Field.Store.YES, Field.Index.UN_TOKENIZED));
058: doc.add(new Field("body",
059: "Today is a very sunny day in New York City",
060: Field.Store.YES, Field.Index.TOKENIZED));
061: writer.addDocument(doc);
062: writer.optimize();
063: writer.close();
064:
065: IndexSearcher searcher = new IndexSearcher(indexStore);
066:
067: // filter that should preserve matches
068: //DateFilter df1 = DateFilter.Before("datefield", now);
069: RangeFilter df1 = new RangeFilter("datefield", DateTools
070: .timeToString(now - 2000,
071: DateTools.Resolution.MILLISECOND), DateTools
072: .timeToString(now, DateTools.Resolution.MILLISECOND),
073: false, true);
074: // filter that should discard matches
075: //DateFilter df2 = DateFilter.Before("datefield", now - 999999);
076: RangeFilter df2 = new RangeFilter("datefield", DateTools
077: .timeToString(0, DateTools.Resolution.MILLISECOND),
078: DateTools.timeToString(now - 2000,
079: DateTools.Resolution.MILLISECOND), true, false);
080:
081: // search something that doesn't exist with DateFilter
082: Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
083:
084: // search for something that does exists
085: Query query2 = new TermQuery(new Term("body", "sunny"));
086:
087: Hits result;
088:
089: // ensure that queries return expected results without DateFilter first
090: result = searcher.search(query1);
091: assertEquals(0, result.length());
092:
093: result = searcher.search(query2);
094: assertEquals(1, result.length());
095:
096: // run queries with DateFilter
097: result = searcher.search(query1, df1);
098: assertEquals(0, result.length());
099:
100: result = searcher.search(query1, df2);
101: assertEquals(0, result.length());
102:
103: result = searcher.search(query2, df1);
104: assertEquals(1, result.length());
105:
106: result = searcher.search(query2, df2);
107: assertEquals(0, result.length());
108: }
109:
110: /**
111: *
112: */
113: public static void testAfter() throws IOException {
114: // create an index
115: RAMDirectory indexStore = new RAMDirectory();
116: IndexWriter writer = new IndexWriter(indexStore,
117: new SimpleAnalyzer(), true);
118:
119: long now = System.currentTimeMillis();
120:
121: Document doc = new Document();
122: // add time that is in the future
123: doc.add(new Field("datefield", DateTools.timeToString(
124: now + 888888, DateTools.Resolution.MILLISECOND),
125: Field.Store.YES, Field.Index.UN_TOKENIZED));
126: doc.add(new Field("body",
127: "Today is a very sunny day in New York City",
128: Field.Store.YES, Field.Index.TOKENIZED));
129: writer.addDocument(doc);
130: writer.optimize();
131: writer.close();
132:
133: IndexSearcher searcher = new IndexSearcher(indexStore);
134:
135: // filter that should preserve matches
136: //DateFilter df1 = DateFilter.After("datefield", now);
137: RangeFilter df1 = new RangeFilter("datefield", DateTools
138: .timeToString(now, DateTools.Resolution.MILLISECOND),
139: DateTools.timeToString(now + 999999,
140: DateTools.Resolution.MILLISECOND), true, false);
141: // filter that should discard matches
142: //DateFilter df2 = DateFilter.After("datefield", now + 999999);
143: RangeFilter df2 = new RangeFilter("datefield", DateTools
144: .timeToString(now + 999999,
145: DateTools.Resolution.MILLISECOND), DateTools
146: .timeToString(now + 999999999,
147: DateTools.Resolution.MILLISECOND), false, true);
148:
149: // search something that doesn't exist with DateFilter
150: Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
151:
152: // search for something that does exists
153: Query query2 = new TermQuery(new Term("body", "sunny"));
154:
155: Hits result;
156:
157: // ensure that queries return expected results without DateFilter first
158: result = searcher.search(query1);
159: assertEquals(0, result.length());
160:
161: result = searcher.search(query2);
162: assertEquals(1, result.length());
163:
164: // run queries with DateFilter
165: result = searcher.search(query1, df1);
166: assertEquals(0, result.length());
167:
168: result = searcher.search(query1, df2);
169: assertEquals(0, result.length());
170:
171: result = searcher.search(query2, df1);
172: assertEquals(1, result.length());
173:
174: result = searcher.search(query2, df2);
175: assertEquals(0, result.length());
176: }
177: }
|