| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 21 |
|
import org.apache.lucene.document.DateTools; |
| 22 |
|
import org.apache.lucene.document.Document; |
| 23 |
|
import org.apache.lucene.document.Field; |
| 24 |
|
import org.apache.lucene.index.IndexWriter; |
| 25 |
|
import org.apache.lucene.index.Term; |
| 26 |
|
import org.apache.lucene.store.RAMDirectory; |
| 27 |
|
|
| 28 |
|
import java.io.IOException; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
@author |
| 34 |
|
@version |
| 35 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (58) |
Complexity: 3 |
Complexity Density: 0.05 |
|
| 36 |
|
public class TestDateFilter |
| 37 |
|
extends TestCase |
| 38 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 39 |
2
|
public TestDateFilter(String name)... |
| 40 |
|
{ |
| 41 |
2
|
super(name); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 47 |
1
|
public static void testBefore()... |
| 48 |
|
throws IOException |
| 49 |
|
{ |
| 50 |
|
|
| 51 |
1
|
RAMDirectory indexStore = new RAMDirectory(); |
| 52 |
1
|
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); |
| 53 |
|
|
| 54 |
1
|
long now = System.currentTimeMillis(); |
| 55 |
|
|
| 56 |
1
|
Document doc = new Document(); |
| 57 |
|
|
| 58 |
1
|
doc.add(new Field("datefield", DateTools.timeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 59 |
1
|
doc.add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED)); |
| 60 |
1
|
writer.addDocument(doc); |
| 61 |
1
|
writer.optimize(); |
| 62 |
1
|
writer.close(); |
| 63 |
|
|
| 64 |
1
|
IndexSearcher searcher = new IndexSearcher(indexStore); |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
1
|
RangeFilter df1 = new RangeFilter("datefield", DateTools.timeToString(now - 2000, DateTools.Resolution.MILLISECOND), |
| 69 |
|
DateTools.timeToString(now, DateTools.Resolution.MILLISECOND), false, true); |
| 70 |
|
|
| 71 |
|
|
| 72 |
1
|
RangeFilter df2 = new RangeFilter("datefield", DateTools.timeToString(0, DateTools.Resolution.MILLISECOND), |
| 73 |
|
DateTools.timeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, false); |
| 74 |
|
|
| 75 |
|
|
| 76 |
1
|
Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); |
| 77 |
|
|
| 78 |
|
|
| 79 |
1
|
Query query2 = new TermQuery(new Term("body", "sunny")); |
| 80 |
|
|
| 81 |
1
|
Hits result; |
| 82 |
|
|
| 83 |
|
|
| 84 |
1
|
result = searcher.search(query1); |
| 85 |
1
|
assertEquals(0, result.length()); |
| 86 |
|
|
| 87 |
1
|
result = searcher.search(query2); |
| 88 |
1
|
assertEquals(1, result.length()); |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
1
|
result = searcher.search(query1, df1); |
| 93 |
1
|
assertEquals(0, result.length()); |
| 94 |
|
|
| 95 |
1
|
result = searcher.search(query1, df2); |
| 96 |
1
|
assertEquals(0, result.length()); |
| 97 |
|
|
| 98 |
1
|
result = searcher.search(query2, df1); |
| 99 |
1
|
assertEquals(1, result.length()); |
| 100 |
|
|
| 101 |
1
|
result = searcher.search(query2, df2); |
| 102 |
1
|
assertEquals(0, result.length()); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 108 |
1
|
public static void testAfter()... |
| 109 |
|
throws IOException |
| 110 |
|
{ |
| 111 |
|
|
| 112 |
1
|
RAMDirectory indexStore = new RAMDirectory(); |
| 113 |
1
|
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); |
| 114 |
|
|
| 115 |
1
|
long now = System.currentTimeMillis(); |
| 116 |
|
|
| 117 |
1
|
Document doc = new Document(); |
| 118 |
|
|
| 119 |
1
|
doc.add(new Field("datefield", DateTools.timeToString(now + 888888, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 120 |
1
|
doc.add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED)); |
| 121 |
1
|
writer.addDocument(doc); |
| 122 |
1
|
writer.optimize(); |
| 123 |
1
|
writer.close(); |
| 124 |
|
|
| 125 |
1
|
IndexSearcher searcher = new IndexSearcher(indexStore); |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
1
|
RangeFilter df1 = new RangeFilter("datefield", DateTools.timeToString(now, DateTools.Resolution.MILLISECOND), |
| 130 |
|
DateTools.timeToString(now + 999999, DateTools.Resolution.MILLISECOND), true, false); |
| 131 |
|
|
| 132 |
|
|
| 133 |
1
|
RangeFilter df2 = new RangeFilter("datefield", DateTools.timeToString(now + 999999, DateTools.Resolution.MILLISECOND), |
| 134 |
|
DateTools.timeToString(now + 999999999, DateTools.Resolution.MILLISECOND), false, true); |
| 135 |
|
|
| 136 |
|
|
| 137 |
1
|
Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); |
| 138 |
|
|
| 139 |
|
|
| 140 |
1
|
Query query2 = new TermQuery(new Term("body", "sunny")); |
| 141 |
|
|
| 142 |
1
|
Hits result; |
| 143 |
|
|
| 144 |
|
|
| 145 |
1
|
result = searcher.search(query1); |
| 146 |
1
|
assertEquals(0, result.length()); |
| 147 |
|
|
| 148 |
1
|
result = searcher.search(query2); |
| 149 |
1
|
assertEquals(1, result.length()); |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
1
|
result = searcher.search(query1, df1); |
| 154 |
1
|
assertEquals(0, result.length()); |
| 155 |
|
|
| 156 |
1
|
result = searcher.search(query1, df2); |
| 157 |
1
|
assertEquals(0, result.length()); |
| 158 |
|
|
| 159 |
1
|
result = searcher.search(query2, df1); |
| 160 |
1
|
assertEquals(1, result.length()); |
| 161 |
|
|
| 162 |
1
|
result = searcher.search(query2, df2); |
| 163 |
1
|
assertEquals(0, result.length()); |
| 164 |
|
} |
| 165 |
|
} |