Populate the index with the data directory files

This commit is contained in:
coolneng 2021-01-10 19:54:01 +01:00
parent 2cb004c6d0
commit 7282a5c69f
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 20 additions and 6 deletions

View File

@ -1,14 +1,20 @@
package org.RI.P2; package org.RI.P2;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer; import org.apache.lucene.analysis.en.EnglishAnalyzer;
@ -27,10 +33,12 @@ import org.json.simple.JSONValue;
public class Indexer { public class Indexer {
IndexWriter index; IndexWriter index;
String folderPath; String folderPath;
List<File> files;
PerFieldAnalyzerWrapper customAnalyzer; PerFieldAnalyzerWrapper customAnalyzer;
Indexer(String folderPath) throws IOException, ParseException { Indexer(String folderPath) throws IOException, ParseException {
this.folderPath = folderPath; this.folderPath = folderPath;
files = readFiles();
customAnalyzer = createAnalyzer(); customAnalyzer = createAnalyzer();
} }
@ -43,8 +51,12 @@ public class Indexer {
return customAnalyzer; return customAnalyzer;
} }
JSONArray parseJSONFile(String filePath) throws IOException, ParseException { List<File> readFiles() throws IOException {
InputStream jsonFile = getClass().getResourceAsStream(filePath); 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 { JSONArray parseJSONFile(File file) throws IOException {
InputStream jsonFile = new FileInputStream(file); InputStream jsonFile = new FileInputStream(file);
Reader readerJson = new InputStreamReader(jsonFile); Reader readerJson = new InputStreamReader(jsonFile);
@ -73,10 +85,12 @@ public class Indexer {
index.close(); index.close();
} }
void createIndex() throws IOException, ParseException { void populateIndex() throws IOException, ParseException {
JSONArray jsonObjects = parseJSONFile(folderPath); createIndex();
openIndex(); for (File file : files) {
addDocuments(jsonObjects); JSONArray jsonObjects = parseJSONFile(file);
addDocument(jsonObjects);
}
commitChanges(); commitChanges();
} }
} }