Compare commits

..

No commits in common. "725be7993e1d8075c40750ff39157aa67f220c8a" and "c61932d99dd6167ccf232293df7269019f5cfb83" have entirely different histories.

View File

@ -1,20 +1,14 @@
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;
@ -33,12 +27,10 @@ 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();
} }
@ -51,25 +43,18 @@ public class Indexer {
return customAnalyzer; return customAnalyzer;
} }
List<File> readFiles() throws IOException { JSONArray parseJSONFile(String filePath) throws IOException, ParseException {
List<File> files = Files.walk(Paths.get(folderPath)).filter(Files::isRegularFile).map(Path::toFile) InputStream jsonFile = getClass().getResourceAsStream(filePath);
.collect(Collectors.toList());
return files;
}
JSONArray parseJSONFile(File file) throws IOException {
InputStream jsonFile = new FileInputStream(file);
Reader readerJson = new InputStreamReader(jsonFile); Reader readerJson = new InputStreamReader(jsonFile);
Object fileObject = JSONValue.parse(readerJson); Object fileObjects = JSONValue.parse(readerJson);
JSONArray arrayObject = new JSONArray(); JSONArray arrayObjects = (JSONArray) fileObjects;
arrayObject.add(fileObject); return arrayObjects;
return arrayObject;
} }
void createIndex() throws IOException { void openIndex() 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); config.setOpenMode(OpenMode.CREATE_OR_APPEND);
index = new IndexWriter(dir, config); index = new IndexWriter(dir, config);
} }
@ -85,26 +70,10 @@ public class Indexer {
index.close(); index.close();
} }
void populateIndex() throws IOException, ParseException { void createIndex() throws IOException, ParseException {
createIndex(); JSONArray jsonObjects = parseJSONFile(folderPath);
for (File file : files) { openIndex();
JSONArray jsonObjects = parseJSONFile(file); addDocuments(jsonObjects);
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();
}
} }