From 7282a5c69ff17b93eec6091891f1c6def00fdd7d Mon Sep 17 00:00:00 2001 From: coolneng Date: Sun, 10 Jan 2021 19:54:01 +0100 Subject: [PATCH] Populate the index with the data directory files --- src/main/java/org/RI/P2/Indexer.java | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/RI/P2/Indexer.java b/src/main/java/org/RI/P2/Indexer.java index 563b390..f61d6c5 100644 --- a/src/main/java/org/RI/P2/Indexer.java +++ b/src/main/java/org/RI/P2/Indexer.java @@ -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 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 readFiles() throws IOException { + List 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(); } }