Replace Parser class with Indexer
This commit is contained in:
parent
5155726341
commit
6405617858
|
@ -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<String, Analyzer> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<File> 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 <directory>");
|
|
||||||
System.out.println("option directory: directory that contains JSON files");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue