1 package org.apache.lucene.search; 2 3 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 ; 31 32 import junit.framework.TestCase; 33 34 40 public class TestDateFilter 41 extends TestCase 42 { 43 public TestDateFilter(String name) 44 { 45 super(name); 46 } 47 48 51 public static void testBefore() 52 throws IOException 53 { 54 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 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 DateFilter df1 = DateFilter.Before("datefield", now); 72 73 DateFilter df2 = DateFilter.Before("datefield", now - 999999); 75 76 Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); 78 79 Query query2 = new TermQuery(new Term("body", "sunny")); 81 82 Hits result; 83 84 result = searcher.search(query1); 86 assertEquals(0, result.length()); 87 88 result = searcher.search(query2); 89 assertEquals(1, result.length()); 90 91 92 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 109 public static void testAfter() 110 throws IOException 111 { 112 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 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 DateFilter df1 = DateFilter.After("datefield", now); 130 131 DateFilter df2 = DateFilter.After("datefield", now + 999999); 133 134 Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); 136 137 Query query2 = new TermQuery(new Term("body", "sunny")); 139 140 Hits result; 141 142 result = searcher.search(query1); 144 assertEquals(0, result.length()); 145 146 result = searcher.search(query2); 147 assertEquals(1, result.length()); 148 149 150 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 |