-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexer.java
More file actions
72 lines (60 loc) · 2.16 KB
/
Indexer.java
File metadata and controls
72 lines (60 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package edu.ucr.cs172.project.partB;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class Indexer {
private IndexWriter writer;
private Directory indexDirectory;
private ParseHTML htmlParser;
public Indexer(String indexDirectoryPath) throws IOException
{
htmlParser = new ParseHTML();
Analyzer analyzer = new StandardAnalyzer();
//this directory will contain the indexes
indexDirectory = FSDirectory.open(new File(indexDirectoryPath).toPath());
//create the indexer
IndexWriterConfig config = new IndexWriterConfig(analyzer);
writer = new IndexWriter(indexDirectory, config);
}
public void close() throws CorruptIndexException, IOException
{
writer.close();
indexDirectory.close();
}
private void indexFile(File file) throws IOException
{
// The Print statement should be removed after debugging
System.out.println("Indexing "+file.getCanonicalPath());
Document document = new Document();
htmlParser.updateFile(file);
document.add(new TextField("title", htmlParser.title(), Field.Store.YES));
document.add(new TextField("content", htmlParser.body(), Field.Store.YES));
writer.addDocument(document);
}
public void createIndex(String dataDirPath)
throws IOException {
//get all files in the data directory
File[] files = new File(dataDirPath).listFiles();
for (File file : files) {
if(!file.isDirectory()
&& !file.isHidden()
&& file.exists()
&& file.canRead()
){
indexFile(file);
}
}
}
}