From 640561785821b361e3ea9e51914dee9aa1b19594 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 8 Jan 2021 23:21:18 +0100 Subject: [PATCH] Replace Parser class with Indexer --- src/main/java/org/RI/P2/Indexer.java | 65 ++++++++++++++++++++++++++++ src/main/java/org/RI/P2/Parser.java | 39 ----------------- 2 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 src/main/java/org/RI/P2/Indexer.java delete mode 100644 src/main/java/org/RI/P2/Parser.java diff --git a/src/main/java/org/RI/P2/Indexer.java b/src/main/java/org/RI/P2/Indexer.java new file mode 100644 index 0000000..ac28ef4 --- /dev/null +++ b/src/main/java/org/RI/P2/Indexer.java @@ -0,0 +1,65 @@ +package org.RI.P2; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.file.Paths; +import java.text.ParseException; +import java.util.HashMap; +import java.util.Map; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.IndexWriterConfig.OpenMode; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.json.simple.JSONArray; +import org.json.simple.JSONValue; + +public class Indexer { + IndexWriter index; + String folderPath; + Map analyzerPerField; + + Indexer(String folderPath) throws IOException, ParseException { + this.folderPath = folderPath; + analyzerPerField = new HashMap<>(); + createIndex(folderPath); + } + + public JSONArray parseJSONFile(String filePath) throws IOException, ParseException { + InputStream jsonFile = getClass().getResourceAsStream(filePath); + Reader readerJson = new InputStreamReader(jsonFile); + Object fileObjects = JSONValue.parse(readerJson); + JSONArray arrayObjects = (JSONArray) fileObjects; + return arrayObjects; + } + + public void openIndex() throws IOException { + Directory dir = FSDirectory.open(Paths.get(folderPath)); + Analyzer analyzer = new StandardAnalyzer(); + IndexWriterConfig config = new IndexWriterConfig(analyzer); + config.setOpenMode(OpenMode.CREATE_OR_APPEND); + index = new IndexWriter(dir, config); + } + + public void addDocuments(JSONArray jsonObjects) throws IOException { + Document doc = new Document(); + index.addDocument(doc); + } + + public void commitChanges() throws IOException { + index.commit(); + index.close(); + } + + public void createIndex(String folderPath) throws IOException, ParseException { + JSONArray jsonObjects = parseJSONFile(folderPath); + openIndex(); + addDocuments(jsonObjects); + commitChanges(); + } +} diff --git a/src/main/java/org/RI/P2/Parser.java b/src/main/java/org/RI/P2/Parser.java deleted file mode 100644 index ba5afca..0000000 --- a/src/main/java/org/RI/P2/Parser.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.RI.P2; - -import java.util.List; -import java.io.IOException; -import java.io.Reader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.stream.Collectors; - -import org.json.simple.JSONArray; -import org.json.simple.JSONValue; -import org.json.simple.parser.ParseException; - -public class Parser { - private static List files; - - private static void readFiles(String directory) throws IOException { - files = Files.walk(Paths.get(directory)).filter(Files::isRegularFile).map(Path::toFile) - .collect(Collectors.toList()); - } - - public JSONArray parseJSONFile(String filePath) throws IOException, ParseException { - InputStream jsonFile = getClass().getResourceAsStream(filePath); - Reader readerJson = new InputStreamReader(jsonFile); - Object fileObjects = JSONValue.parseWithException(readerJson); - JSONArray arrayObjects = (JSONArray) fileObjects; - return arrayObjects; - } - - private static void usage() { - System.out.println("Usage: Parser "); - System.out.println("option directory: directory that contains JSON files"); - System.exit(1); - } -}