| 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.index.IndexWriter; |
| 21 |
|
|
| 22 |
|
import java.io.File; |
| 23 |
|
import java.io.FileNotFoundException; |
| 24 |
|
import java.io.IOException; |
| 25 |
|
import java.util.Date; |
| 26 |
|
|
| 27 |
|
|
|
|
|
| 0% |
Uncovered Elements: 49 (49) |
Complexity: 13 |
Complexity Density: 0.41 |
|
| 28 |
|
public class IndexFiles { |
| 29 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 30 |
0
|
private IndexFiles() {}... |
| 31 |
|
|
| 32 |
|
static final File INDEX_DIR = new File("index"); |
| 33 |
|
|
| 34 |
|
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 6 |
Complexity Density: 0.27 |
|
| 35 |
0
|
public static void main(String[] args) {... |
| 36 |
0
|
String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>"; |
| 37 |
0
|
if (args.length == 0) { |
| 38 |
0
|
System.err.println("Usage: " + usage); |
| 39 |
0
|
System.exit(1); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
0
|
if (INDEX_DIR.exists()) { |
| 43 |
0
|
System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first"); |
| 44 |
0
|
System.exit(1); |
| 45 |
|
} |
| 46 |
|
|
| 47 |
0
|
final File docDir = new File(args[0]); |
| 48 |
0
|
if (!docDir.exists() || !docDir.canRead()) { |
| 49 |
0
|
System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path"); |
| 50 |
0
|
System.exit(1); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
0
|
Date start = new Date(); |
| 54 |
0
|
try { |
| 55 |
0
|
IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true); |
| 56 |
0
|
System.out.println("Indexing to directory '" +INDEX_DIR+ "'..."); |
| 57 |
0
|
indexDocs(writer, docDir); |
| 58 |
0
|
System.out.println("Optimizing..."); |
| 59 |
0
|
writer.optimize(); |
| 60 |
0
|
writer.close(); |
| 61 |
|
|
| 62 |
0
|
Date end = new Date(); |
| 63 |
0
|
System.out.println(end.getTime() - start.getTime() + " total milliseconds"); |
| 64 |
|
|
| 65 |
|
} catch (IOException e) { |
| 66 |
0
|
System.out.println(" caught a " + e.getClass() + |
| 67 |
|
"\n with message: " + e.getMessage()); |
| 68 |
|
} |
| 69 |
|
} |
| 70 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 6 |
Complexity Density: 0.6 |
|
| 71 |
0
|
static void indexDocs(IndexWriter writer, File file)... |
| 72 |
|
throws IOException { |
| 73 |
|
|
| 74 |
0
|
if (file.canRead()) { |
| 75 |
0
|
if (file.isDirectory()) { |
| 76 |
0
|
String[] files = file.list(); |
| 77 |
|
|
| 78 |
0
|
if (files != null) { |
| 79 |
0
|
for (int i = 0; i < files.length; i++) { |
| 80 |
0
|
indexDocs(writer, new File(file, files[i])); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
} else { |
| 84 |
0
|
System.out.println("adding " + file); |
| 85 |
0
|
try { |
| 86 |
0
|
writer.addDocument(FileDocument.Document(file)); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
catch (FileNotFoundException fnfe) { |
| 91 |
0
|
; |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
} |