| 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.io.IOException; |
| 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: 41 (41) |
Complexity: 8 |
Complexity Density: 0.24 |
|
| 28 |
|
class SearchTestForDuplicates { |
| 29 |
|
|
| 30 |
|
static final String PRIORITY_FIELD ="priority"; |
| 31 |
|
static final String ID_FIELD ="id"; |
| 32 |
|
static final String HIGH_PRIORITY ="high"; |
| 33 |
|
static final String MED_PRIORITY ="medium"; |
| 34 |
|
static final String LOW_PRIORITY ="low"; |
| 35 |
|
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 3 |
Complexity Density: 0.11 |
|
| 36 |
0
|
public static void main(String[] args) {... |
| 37 |
0
|
try { |
| 38 |
0
|
Directory directory = new RAMDirectory(); |
| 39 |
0
|
Analyzer analyzer = new SimpleAnalyzer(); |
| 40 |
0
|
IndexWriter writer = new IndexWriter(directory, analyzer, true); |
| 41 |
|
|
| 42 |
0
|
final int MAX_DOCS = 225; |
| 43 |
|
|
| 44 |
0
|
for (int j = 0; j < MAX_DOCS; j++) { |
| 45 |
0
|
Document d = new Document(); |
| 46 |
0
|
d.add(new Field(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES, Field.Index.TOKENIZED)); |
| 47 |
0
|
d.add(new Field(ID_FIELD, Integer.toString(j), Field.Store.YES, Field.Index.TOKENIZED)); |
| 48 |
0
|
writer.addDocument(d); |
| 49 |
|
} |
| 50 |
0
|
writer.close(); |
| 51 |
|
|
| 52 |
|
|
| 53 |
0
|
Searcher searcher = new IndexSearcher(directory); |
| 54 |
0
|
Hits hits = null; |
| 55 |
|
|
| 56 |
0
|
QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer); |
| 57 |
|
|
| 58 |
0
|
Query query = parser.parse(HIGH_PRIORITY); |
| 59 |
0
|
System.out.println("Query: " + query.toString(PRIORITY_FIELD)); |
| 60 |
|
|
| 61 |
0
|
hits = searcher.search(query); |
| 62 |
0
|
printHits(hits); |
| 63 |
|
|
| 64 |
0
|
searcher.close(); |
| 65 |
|
|
| 66 |
|
|
| 67 |
0
|
searcher = new IndexSearcher(directory); |
| 68 |
0
|
hits = null; |
| 69 |
|
|
| 70 |
0
|
parser = new QueryParser(PRIORITY_FIELD, analyzer); |
| 71 |
|
|
| 72 |
0
|
query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY); |
| 73 |
0
|
System.out.println("Query: " + query.toString(PRIORITY_FIELD)); |
| 74 |
|
|
| 75 |
0
|
hits = searcher.search(query); |
| 76 |
0
|
printHits(hits); |
| 77 |
|
|
| 78 |
0
|
searcher.close(); |
| 79 |
|
|
| 80 |
|
} catch (Exception e) { |
| 81 |
0
|
System.out.println(" caught a " + e.getClass() + |
| 82 |
|
"\n with message: " + e.getMessage()); |
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
| 86 |
0
|
private static void printHits( Hits hits ) throws IOException {... |
| 87 |
0
|
System.out.println(hits.length() + " total results\n"); |
| 88 |
0
|
for (int i = 0 ; i < hits.length(); i++) { |
| 89 |
0
|
if ( i < 10 || (i > 94 && i < 105) ) { |
| 90 |
0
|
Document d = hits.doc(i); |
| 91 |
0
|
System.out.println(i + " " + d.get(ID_FIELD)); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
} |