| 1 |
|
package org.apache.lucene.demo; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import org.apache.lucene.analysis.Analyzer; |
| 20 |
|
import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 21 |
|
import org.apache.lucene.document.Document; |
| 22 |
|
import org.apache.lucene.index.FilterIndexReader; |
| 23 |
|
import org.apache.lucene.index.IndexReader; |
| 24 |
|
import org.apache.lucene.queryParser.QueryParser; |
| 25 |
|
import org.apache.lucene.search.Hits; |
| 26 |
|
import org.apache.lucene.search.IndexSearcher; |
| 27 |
|
import org.apache.lucene.search.Query; |
| 28 |
|
import org.apache.lucene.search.Searcher; |
| 29 |
|
|
| 30 |
|
import java.io.BufferedReader; |
| 31 |
|
import java.io.FileReader; |
| 32 |
|
import java.io.IOException; |
| 33 |
|
import java.io.InputStreamReader; |
| 34 |
|
import java.util.Date; |
| 35 |
|
|
| 36 |
|
|
|
|
|
| 0% |
Uncovered Elements: 123 (123) |
Complexity: 29 |
Complexity Density: 0.38 |
|
| 37 |
|
public class SearchFiles { |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 44 |
|
private static class OneNormsReader extends FilterIndexReader { |
| 45 |
|
private String field; |
| 46 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 47 |
0
|
public OneNormsReader(IndexReader in, String field) {... |
| 48 |
0
|
super(in); |
| 49 |
0
|
this.field = field; |
| 50 |
|
} |
| 51 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 52 |
0
|
public byte[] norms(String field) throws IOException {... |
| 53 |
0
|
return in.norms(this.field); |
| 54 |
|
} |
| 55 |
|
} |
| 56 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 57 |
0
|
private SearchFiles() {}... |
| 58 |
|
|
| 59 |
|
|
|
|
|
| 0% |
Uncovered Elements: 121 (121) |
Complexity: 28 |
Complexity Density: 0.36 |
|
| 60 |
0
|
public static void main(String[] args) throws Exception {... |
| 61 |
0
|
String usage = |
| 62 |
|
"Usage: java org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-raw] [-norms field]"; |
| 63 |
0
|
if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { |
| 64 |
0
|
System.out.println(usage); |
| 65 |
0
|
System.exit(0); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
0
|
String index = "index"; |
| 69 |
0
|
String field = "contents"; |
| 70 |
0
|
String queries = null; |
| 71 |
0
|
int repeat = 0; |
| 72 |
0
|
boolean raw = false; |
| 73 |
0
|
String normsField = null; |
| 74 |
|
|
| 75 |
0
|
for (int i = 0; i < args.length; i++) { |
| 76 |
0
|
if ("-index".equals(args[i])) { |
| 77 |
0
|
index = args[i+1]; |
| 78 |
0
|
i++; |
| 79 |
0
|
} else if ("-field".equals(args[i])) { |
| 80 |
0
|
field = args[i+1]; |
| 81 |
0
|
i++; |
| 82 |
0
|
} else if ("-queries".equals(args[i])) { |
| 83 |
0
|
queries = args[i+1]; |
| 84 |
0
|
i++; |
| 85 |
0
|
} else if ("-repeat".equals(args[i])) { |
| 86 |
0
|
repeat = Integer.parseInt(args[i+1]); |
| 87 |
0
|
i++; |
| 88 |
0
|
} else if ("-raw".equals(args[i])) { |
| 89 |
0
|
raw = true; |
| 90 |
0
|
} else if ("-norms".equals(args[i])) { |
| 91 |
0
|
normsField = args[i+1]; |
| 92 |
0
|
i++; |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
0
|
IndexReader reader = IndexReader.open(index); |
| 97 |
|
|
| 98 |
0
|
if (normsField != null) |
| 99 |
0
|
reader = new OneNormsReader(reader, normsField); |
| 100 |
|
|
| 101 |
0
|
Searcher searcher = new IndexSearcher(reader); |
| 102 |
0
|
Analyzer analyzer = new StandardAnalyzer(); |
| 103 |
|
|
| 104 |
0
|
BufferedReader in = null; |
| 105 |
0
|
if (queries != null) { |
| 106 |
0
|
in = new BufferedReader(new FileReader(queries)); |
| 107 |
|
} else { |
| 108 |
0
|
in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); |
| 109 |
|
} |
| 110 |
0
|
QueryParser parser = new QueryParser(field, analyzer); |
| 111 |
0
|
while (true) { |
| 112 |
0
|
if (queries == null) |
| 113 |
0
|
System.out.print("Query: "); |
| 114 |
|
|
| 115 |
0
|
String line = in.readLine(); |
| 116 |
|
|
| 117 |
0
|
if (line == null || line.length() == -1) |
| 118 |
0
|
break; |
| 119 |
|
|
| 120 |
0
|
Query query = parser.parse(line); |
| 121 |
0
|
System.out.println("Searching for: " + query.toString(field)); |
| 122 |
|
|
| 123 |
0
|
Hits hits = searcher.search(query); |
| 124 |
|
|
| 125 |
0
|
if (repeat > 0) { |
| 126 |
0
|
Date start = new Date(); |
| 127 |
0
|
for (int i = 0; i < repeat; i++) { |
| 128 |
0
|
hits = searcher.search(query); |
| 129 |
|
} |
| 130 |
0
|
Date end = new Date(); |
| 131 |
0
|
System.out.println("Time: "+(end.getTime()-start.getTime())+"ms"); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
0
|
System.out.println(hits.length() + " total matching documents"); |
| 135 |
|
|
| 136 |
0
|
final int HITS_PER_PAGE = 10; |
| 137 |
0
|
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) { |
| 138 |
0
|
int end = Math.min(hits.length(), start + HITS_PER_PAGE); |
| 139 |
0
|
for (int i = start; i < end; i++) { |
| 140 |
|
|
| 141 |
0
|
if (raw) { |
| 142 |
0
|
System.out.println("doc="+hits.id(i)+" score="+hits.score(i)); |
| 143 |
0
|
continue; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
0
|
Document doc = hits.doc(i); |
| 147 |
0
|
String path = doc.get("path"); |
| 148 |
0
|
if (path != null) { |
| 149 |
0
|
System.out.println((i+1) + ". " + path); |
| 150 |
0
|
String title = doc.get("title"); |
| 151 |
0
|
if (title != null) { |
| 152 |
0
|
System.out.println(" Title: " + doc.get("title")); |
| 153 |
|
} |
| 154 |
|
} else { |
| 155 |
0
|
System.out.println((i+1) + ". " + "No path for this document"); |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| 159 |
0
|
if (queries != null) |
| 160 |
0
|
break; |
| 161 |
|
|
| 162 |
0
|
if (hits.length() > end) { |
| 163 |
0
|
System.out.print("more (y/n) ? "); |
| 164 |
0
|
line = in.readLine(); |
| 165 |
0
|
if (line.length() == 0 || line.charAt(0) == 'n') |
| 166 |
0
|
break; |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
0
|
reader.close(); |
| 171 |
|
} |
| 172 |
|
} |