Compare commits

...

4 Commits

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,18 +51,25 @@ 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)
Reader readerJson = new InputStreamReader(jsonFile); .collect(Collectors.toList());
Object fileObjects = JSONValue.parse(readerJson); return files;
JSONArray arrayObjects = (JSONArray) fileObjects;
return arrayObjects;
} }
void openIndex() throws IOException { JSONArray parseJSONFile(File file) throws IOException {
InputStream jsonFile = new FileInputStream(file);
Reader readerJson = new InputStreamReader(jsonFile);
Object fileObject = JSONValue.parse(readerJson);
JSONArray arrayObject = new JSONArray();
arrayObject.add(fileObject);
return arrayObject;
}
void createIndex() throws IOException {
Directory dir = FSDirectory.open(Paths.get(folderPath)); Directory dir = FSDirectory.open(Paths.get(folderPath));
IndexWriterConfig config = new IndexWriterConfig(customAnalyzer); IndexWriterConfig config = new IndexWriterConfig(customAnalyzer);
config.setOpenMode(OpenMode.CREATE_OR_APPEND); config.setOpenMode(OpenMode.CREATE);
index = new IndexWriter(dir, config); index = new IndexWriter(dir, config);
} }
@ -70,10 +85,26 @@ 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();
} }
private static void usage() {
System.out.println("Usage: Indexer <directory>");
System.exit(1);
}
public static void main(String[] args) throws ParseException, IOException {
if (args.length != 1) {
usage();
}
String dataDirectory = args[0];
Indexer indexer = new Indexer(dataDirectory);
indexer.populateIndex();
}
} }