| 1 |
|
package org.apache.lucene; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import java.util.GregorianCalendar; |
| 20 |
|
import java.io.PrintWriter; |
| 21 |
|
import java.io.StringWriter; |
| 22 |
|
|
| 23 |
|
import junit.framework.TestCase; |
| 24 |
|
import junit.framework.TestSuite; |
| 25 |
|
import junit.textui.TestRunner; |
| 26 |
|
|
| 27 |
|
import org.apache.lucene.store.*; |
| 28 |
|
import org.apache.lucene.document.*; |
| 29 |
|
import org.apache.lucene.analysis.*; |
| 30 |
|
import org.apache.lucene.index.*; |
| 31 |
|
import org.apache.lucene.search.*; |
| 32 |
|
import org.apache.lucene.queryParser.*; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
@author |
| 36 |
|
@version |
| 37 |
|
|
|
|
|
| 88.2% |
Uncovered Elements: 6 (51) |
Complexity: 8 |
Complexity Density: 0.2 |
|
| 38 |
|
public class TestSearch extends TestCase { |
| 39 |
|
|
| 40 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 41 |
0
|
public static void main(String args[]) {... |
| 42 |
0
|
TestRunner.run (new TestSuite(TestSearch.class)); |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1
PASS
|
|
| 54 |
1
|
public void testSearch() throws Exception {... |
| 55 |
1
|
StringWriter sw = new StringWriter(); |
| 56 |
1
|
PrintWriter pw = new PrintWriter(sw, true); |
| 57 |
1
|
doTestSearch(pw, false); |
| 58 |
1
|
pw.close(); |
| 59 |
1
|
sw.close(); |
| 60 |
1
|
String multiFileOutput = sw.getBuffer().toString(); |
| 61 |
|
|
| 62 |
|
|
| 63 |
1
|
sw = new StringWriter(); |
| 64 |
1
|
pw = new PrintWriter(sw, true); |
| 65 |
1
|
doTestSearch(pw, true); |
| 66 |
1
|
pw.close(); |
| 67 |
1
|
sw.close(); |
| 68 |
1
|
String singleFileOutput = sw.getBuffer().toString(); |
| 69 |
|
|
| 70 |
1
|
assertEquals(multiFileOutput, singleFileOutput); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 5 |
Complexity Density: 0.21 |
|
| 74 |
2
|
private void doTestSearch(PrintWriter out, boolean useCompoundFile)... |
| 75 |
|
throws Exception |
| 76 |
|
{ |
| 77 |
2
|
Directory directory = new RAMDirectory(); |
| 78 |
2
|
Analyzer analyzer = new SimpleAnalyzer(); |
| 79 |
2
|
IndexWriter writer = new IndexWriter(directory, analyzer, true); |
| 80 |
|
|
| 81 |
2
|
writer.setUseCompoundFile(useCompoundFile); |
| 82 |
|
|
| 83 |
2
|
String[] docs = { |
| 84 |
|
"a b c d e", |
| 85 |
|
"a b c d e a b c d e", |
| 86 |
|
"a b c d e f g h i j", |
| 87 |
|
"a c e", |
| 88 |
|
"e c a", |
| 89 |
|
"a c e a c e", |
| 90 |
|
"a c e a b c" |
| 91 |
|
}; |
| 92 |
16
|
for (int j = 0; j < docs.length; j++) { |
| 93 |
14
|
Document d = new Document(); |
| 94 |
14
|
d.add(new Field("contents", docs[j], Field.Store.YES, Field.Index.TOKENIZED)); |
| 95 |
14
|
writer.addDocument(d); |
| 96 |
|
} |
| 97 |
2
|
writer.close(); |
| 98 |
|
|
| 99 |
2
|
Searcher searcher = new IndexSearcher(directory); |
| 100 |
|
|
| 101 |
2
|
String[] queries = { |
| 102 |
|
"a b", |
| 103 |
|
"\"a b\"", |
| 104 |
|
"\"a b c\"", |
| 105 |
|
"a c", |
| 106 |
|
"\"a c\"", |
| 107 |
|
"\"a c e\"", |
| 108 |
|
}; |
| 109 |
2
|
Hits hits = null; |
| 110 |
|
|
| 111 |
2
|
QueryParser parser = new QueryParser("contents", analyzer); |
| 112 |
2
|
parser.setPhraseSlop(4); |
| 113 |
14
|
for (int j = 0; j < queries.length; j++) { |
| 114 |
12
|
Query query = parser.parse(queries[j]); |
| 115 |
12
|
out.println("Query: " + query.toString("contents")); |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
12
|
hits = searcher.search(query); |
| 123 |
|
|
| 124 |
12
|
out.println(hits.length() + " total results"); |
| 125 |
84
|
for (int i = 0 ; i < hits.length() && i < 10; i++) { |
| 126 |
72
|
Document d = hits.doc(i); |
| 127 |
72
|
out.println(i + " " + hits.score(i) |
| 128 |
|
|
| 129 |
|
+ " " + d.get("contents")); |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
2
|
searcher.close(); |
| 133 |
|
} |
| 134 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 135 |
0
|
static long Time(int year, int month, int day) {... |
| 136 |
0
|
GregorianCalendar calendar = new GregorianCalendar(); |
| 137 |
0
|
calendar.set(year, month, day); |
| 138 |
0
|
return calendar.getTime().getTime(); |
| 139 |
|
} |
| 140 |
|
} |