| 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 |
|
|
| 21 |
|
import org.apache.lucene.store.*; |
| 22 |
|
import org.apache.lucene.document.*; |
| 23 |
|
import org.apache.lucene.analysis.*; |
| 24 |
|
import org.apache.lucene.index.*; |
| 25 |
|
import org.apache.lucene.search.*; |
| 26 |
|
import org.apache.lucene.queryParser.*; |
| 27 |
|
|
|
|
|
| 0% |
Uncovered Elements: 36 (36) |
Complexity: 7 |
Complexity Density: 0.25 |
|
| 28 |
|
class SearchTest { |
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 6 |
Complexity Density: 0.24 |
|
| 29 |
0
|
public static void main(String[] args) {... |
| 30 |
0
|
try { |
| 31 |
0
|
Directory directory = new RAMDirectory(); |
| 32 |
0
|
Analyzer analyzer = new SimpleAnalyzer(); |
| 33 |
0
|
IndexWriter writer = new IndexWriter(directory, analyzer, true); |
| 34 |
|
|
| 35 |
0
|
String[] docs = { |
| 36 |
|
"a b c d e", |
| 37 |
|
"a b c d e a b c d e", |
| 38 |
|
"a b c d e f g h i j", |
| 39 |
|
"a c e", |
| 40 |
|
"e c a", |
| 41 |
|
"a c e a c e", |
| 42 |
|
"a c e a b c" |
| 43 |
|
}; |
| 44 |
0
|
for (int j = 0; j < docs.length; j++) { |
| 45 |
0
|
Document d = new Document(); |
| 46 |
0
|
d.add(new Field("contents", docs[j], Field.Store.YES, Field.Index.TOKENIZED)); |
| 47 |
0
|
writer.addDocument(d); |
| 48 |
|
} |
| 49 |
0
|
writer.close(); |
| 50 |
|
|
| 51 |
0
|
Searcher searcher = new IndexSearcher(directory); |
| 52 |
|
|
| 53 |
0
|
String[] queries = { |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
"\"a c e\"", |
| 60 |
|
}; |
| 61 |
0
|
Hits hits = null; |
| 62 |
|
|
| 63 |
0
|
QueryParser parser = new QueryParser("contents", analyzer); |
| 64 |
0
|
parser.setPhraseSlop(4); |
| 65 |
0
|
for (int j = 0; j < queries.length; j++) { |
| 66 |
0
|
Query query = parser.parse(queries[j]); |
| 67 |
0
|
System.out.println("Query: " + query.toString("contents")); |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
0
|
hits = searcher.search(query); |
| 75 |
|
|
| 76 |
0
|
System.out.println(hits.length() + " total results"); |
| 77 |
0
|
for (int i = 0 ; i < hits.length() && i < 10; i++) { |
| 78 |
0
|
Document d = hits.doc(i); |
| 79 |
0
|
System.out.println(i + " " + hits.score(i) |
| 80 |
|
|
| 81 |
|
+ " " + d.get("contents")); |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
0
|
searcher.close(); |
| 85 |
|
|
| 86 |
|
} catch (Exception e) { |
| 87 |
0
|
System.out.println(" caught a " + e.getClass() + |
| 88 |
|
"\n with message: " + e.getMessage()); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 92 |
0
|
static long Time(int year, int month, int day) {... |
| 93 |
0
|
GregorianCalendar calendar = new GregorianCalendar(); |
| 94 |
0
|
calendar.set(year, month, day); |
| 95 |
0
|
return calendar.getTime().getTime(); |
| 96 |
|
} |
| 97 |
|
} |