Populate the index with the data directory files
This commit is contained in:
parent
2cb004c6d0
commit
7282a5c69f
|
@ -1,14 +1,20 @@
|
|||
package org.RI.P2;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||
import org.apache.lucene.analysis.en.EnglishAnalyzer;
|
||||
|
@ -27,10 +33,12 @@ import org.json.simple.JSONValue;
|
|||
public class Indexer {
|
||||
IndexWriter index;
|
||||
String folderPath;
|
||||
List<File> files;
|
||||
PerFieldAnalyzerWrapper customAnalyzer;
|
||||
|
||||
Indexer(String folderPath) throws IOException, ParseException {
|
||||
this.folderPath = folderPath;
|
||||
files = readFiles();
|
||||
customAnalyzer = createAnalyzer();
|
||||
}
|
||||
|
||||
|
@ -43,8 +51,12 @@ public class Indexer {
|
|||
return customAnalyzer;
|
||||
}
|
||||
|
||||
JSONArray parseJSONFile(String filePath) throws IOException, ParseException {
|
||||
InputStream jsonFile = getClass().getResourceAsStream(filePath);
|
||||
List<File> readFiles() throws IOException {
|
||||
List<File> files = Files.walk(Paths.get(folderPath)).filter(Files::isRegularFile).map(Path::toFile)
|
||||
.collect(Collectors.toList());
|
||||
return files;
|
||||
}
|
||||
|
||||
JSONArray parseJSONFile(File file) throws IOException {
|
||||
InputStream jsonFile = new FileInputStream(file);
|
||||
Reader readerJson = new InputStreamReader(jsonFile);
|
||||
|
@ -73,10 +85,12 @@ public class Indexer {
|
|||
index.close();
|
||||
}
|
||||
|
||||
void createIndex() throws IOException, ParseException {
|
||||
JSONArray jsonObjects = parseJSONFile(folderPath);
|
||||
openIndex();
|
||||
addDocuments(jsonObjects);
|
||||
void populateIndex() throws IOException, ParseException {
|
||||
createIndex();
|
||||
for (File file : files) {
|
||||
JSONArray jsonObjects = parseJSONFile(file);
|
||||
addDocument(jsonObjects);
|
||||
}
|
||||
commitChanges();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue