| 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.standard.StandardAnalyzer; |
| 20 |
|
import org.apache.lucene.document.Document; |
| 21 |
|
import org.apache.lucene.index.IndexReader; |
| 22 |
|
import org.apache.lucene.index.IndexWriter; |
| 23 |
|
import org.apache.lucene.index.Term; |
| 24 |
|
import org.apache.lucene.index.TermEnum; |
| 25 |
|
import java.io.File; |
| 26 |
|
import java.util.Date; |
| 27 |
|
import java.util.Arrays; |
| 28 |
|
|
| 29 |
|
|
|
|
|
| 0% |
Uncovered Elements: 104 (104) |
Complexity: 29 |
Complexity Density: 0.44 |
|
| 30 |
|
public class IndexHTML { |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 31 |
0
|
private IndexHTML() {}... |
| 32 |
|
|
| 33 |
|
private static boolean deleting = false; |
| 34 |
|
private static IndexReader reader; |
| 35 |
|
private static IndexWriter writer; |
| 36 |
|
private static TermEnum uidIter; |
| 37 |
|
|
| 38 |
|
|
|
|
|
| 0% |
Uncovered Elements: 43 (43) |
Complexity: 8 |
Complexity Density: 0.26 |
|
| 39 |
0
|
public static void main(String[] argv) {... |
| 40 |
0
|
try { |
| 41 |
0
|
String index = "index"; |
| 42 |
0
|
boolean create = false; |
| 43 |
0
|
File root = null; |
| 44 |
|
|
| 45 |
0
|
String usage = "IndexHTML [-create] [-index <index>] <root_directory>"; |
| 46 |
|
|
| 47 |
0
|
if (argv.length == 0) { |
| 48 |
0
|
System.err.println("Usage: " + usage); |
| 49 |
0
|
return; |
| 50 |
|
} |
| 51 |
|
|
| 52 |
0
|
for (int i = 0; i < argv.length; i++) { |
| 53 |
0
|
if (argv[i].equals("-index")) { |
| 54 |
0
|
index = argv[++i]; |
| 55 |
0
|
} else if (argv[i].equals("-create")) { |
| 56 |
0
|
create = true; |
| 57 |
0
|
} else if (i != argv.length-1) { |
| 58 |
0
|
System.err.println("Usage: " + usage); |
| 59 |
0
|
return; |
| 60 |
|
} else |
| 61 |
0
|
root = new File(argv[i]); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
0
|
Date start = new Date(); |
| 65 |
|
|
| 66 |
0
|
if (!create) { |
| 67 |
0
|
deleting = true; |
| 68 |
0
|
indexDocs(root, index, create); |
| 69 |
|
} |
| 70 |
0
|
writer = new IndexWriter(index, new StandardAnalyzer(), create); |
| 71 |
0
|
writer.setMaxFieldLength(1000000); |
| 72 |
0
|
indexDocs(root, index, create); |
| 73 |
|
|
| 74 |
0
|
System.out.println("Optimizing index..."); |
| 75 |
0
|
writer.optimize(); |
| 76 |
0
|
writer.close(); |
| 77 |
|
|
| 78 |
0
|
Date end = new Date(); |
| 79 |
|
|
| 80 |
0
|
System.out.print(end.getTime() - start.getTime()); |
| 81 |
0
|
System.out.println(" total milliseconds"); |
| 82 |
|
|
| 83 |
|
} catch (Exception e) { |
| 84 |
0
|
System.out.println(" caught a " + e.getClass() + |
| 85 |
|
"\n with message: " + e.getMessage()); |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
| 95 |
0
|
private static void indexDocs(File file, String index, boolean create)... |
| 96 |
|
throws Exception { |
| 97 |
0
|
if (!create) { |
| 98 |
|
|
| 99 |
0
|
reader = IndexReader.open(index); |
| 100 |
0
|
uidIter = reader.terms(new Term("uid", "")); |
| 101 |
|
|
| 102 |
0
|
indexDocs(file); |
| 103 |
|
|
| 104 |
0
|
if (deleting) { |
| 105 |
0
|
while (uidIter.term() != null && uidIter.term().field() == "uid") { |
| 106 |
0
|
System.out.println("deleting " + |
| 107 |
|
HTMLDocument.uid2url(uidIter.term().text())); |
| 108 |
0
|
reader.deleteDocuments(uidIter.term()); |
| 109 |
0
|
uidIter.next(); |
| 110 |
|
} |
| 111 |
0
|
deleting = false; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
0
|
uidIter.close(); |
| 115 |
0
|
reader.close(); |
| 116 |
|
|
| 117 |
|
} else |
| 118 |
0
|
indexDocs(file); |
| 119 |
|
} |
| 120 |
|
|
|
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 15 |
Complexity Density: 0.68 |
|
| 121 |
0
|
private static void indexDocs(File file) throws Exception {... |
| 122 |
0
|
if (file.isDirectory()) { |
| 123 |
0
|
String[] files = file.list(); |
| 124 |
0
|
Arrays.sort(files); |
| 125 |
0
|
for (int i = 0; i < files.length; i++) |
| 126 |
0
|
indexDocs(new File(file, files[i])); |
| 127 |
|
|
| 128 |
0
|
} else if (file.getPath().endsWith(".html") || |
| 129 |
|
file.getPath().endsWith(".htm") || |
| 130 |
|
file.getPath().endsWith(".txt")) { |
| 131 |
|
|
| 132 |
0
|
if (uidIter != null) { |
| 133 |
0
|
String uid = HTMLDocument.uid(file); |
| 134 |
|
|
| 135 |
0
|
while (uidIter.term() != null && uidIter.term().field() == "uid" && |
| 136 |
|
uidIter.term().text().compareTo(uid) < 0) { |
| 137 |
0
|
if (deleting) { |
| 138 |
0
|
System.out.println("deleting " + |
| 139 |
|
HTMLDocument.uid2url(uidIter.term().text())); |
| 140 |
0
|
reader.deleteDocuments(uidIter.term()); |
| 141 |
|
} |
| 142 |
0
|
uidIter.next(); |
| 143 |
|
} |
| 144 |
0
|
if (uidIter.term() != null && uidIter.term().field() == "uid" && |
| 145 |
|
uidIter.term().text().compareTo(uid) == 0) { |
| 146 |
0
|
uidIter.next(); |
| 147 |
0
|
} else if (!deleting) { |
| 148 |
0
|
Document doc = HTMLDocument.Document(file); |
| 149 |
0
|
System.out.println("adding " + doc.get("path")); |
| 150 |
0
|
writer.addDocument(doc); |
| 151 |
|
} |
| 152 |
|
} else { |
| 153 |
0
|
Document doc = HTMLDocument.Document(file); |
| 154 |
0
|
System.out.println("adding " + doc.get("path")); |
| 155 |
0
|
writer.addDocument(doc); |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
} |