Implement trivial custom analyzer
This commit is contained in:
parent
6405617858
commit
c61932d99d
|
@ -7,8 +7,12 @@ import java.io.Reader;
|
|||
import java.nio.file.Paths;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||
import org.apache.lucene.analysis.en.EnglishAnalyzer;
|
||||
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
|
@ -17,20 +21,29 @@ 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.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
public class Indexer {
|
||||
IndexWriter index;
|
||||
String folderPath;
|
||||
Map<String, Analyzer> analyzerPerField;
|
||||
PerFieldAnalyzerWrapper customAnalyzer;
|
||||
|
||||
Indexer(String folderPath) throws IOException, ParseException {
|
||||
this.folderPath = folderPath;
|
||||
analyzerPerField = new HashMap<>();
|
||||
createIndex(folderPath);
|
||||
customAnalyzer = createAnalyzer();
|
||||
}
|
||||
|
||||
public JSONArray parseJSONFile(String filePath) throws IOException, ParseException {
|
||||
PerFieldAnalyzerWrapper createAnalyzer() {
|
||||
Map<String, Analyzer> analyzerPerField = new HashMap<>();
|
||||
analyzerPerField.put("title", new EnglishAnalyzer());
|
||||
analyzerPerField.put("abstract", new EnglishAnalyzer());
|
||||
PerFieldAnalyzerWrapper customAnalyzer = new PerFieldAnalyzerWrapper(new WhitespaceAnalyzer(),
|
||||
analyzerPerField);
|
||||
return customAnalyzer;
|
||||
}
|
||||
|
||||
JSONArray parseJSONFile(String filePath) throws IOException, ParseException {
|
||||
InputStream jsonFile = getClass().getResourceAsStream(filePath);
|
||||
Reader readerJson = new InputStreamReader(jsonFile);
|
||||
Object fileObjects = JSONValue.parse(readerJson);
|
||||
|
@ -38,25 +51,26 @@ public class Indexer {
|
|||
return arrayObjects;
|
||||
}
|
||||
|
||||
public void openIndex() throws IOException {
|
||||
void openIndex() throws IOException {
|
||||
Directory dir = FSDirectory.open(Paths.get(folderPath));
|
||||
Analyzer analyzer = new StandardAnalyzer();
|
||||
IndexWriterConfig config = new IndexWriterConfig(analyzer);
|
||||
IndexWriterConfig config = new IndexWriterConfig(customAnalyzer);
|
||||
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);
|
||||
void addDocuments(JSONArray jsonObjects) throws IOException {
|
||||
for (JSONObject object : (List<JSONObject>) jsonObjects) {
|
||||
Document doc = new Document();
|
||||
index.addDocument(doc);
|
||||
}
|
||||
}
|
||||
|
||||
public void commitChanges() throws IOException {
|
||||
void commitChanges() throws IOException {
|
||||
index.commit();
|
||||
index.close();
|
||||
}
|
||||
|
||||
public void createIndex(String folderPath) throws IOException, ParseException {
|
||||
void createIndex() throws IOException, ParseException {
|
||||
JSONArray jsonObjects = parseJSONFile(folderPath);
|
||||
openIndex();
|
||||
addDocuments(jsonObjects);
|
||||
|
|
Loading…
Reference in New Issue