From c61932d99dd6167ccf232293df7269019f5cfb83 Mon Sep 17 00:00:00 2001
From: coolneng <akasroua@gmail.com>
Date: Sat, 9 Jan 2021 06:20:06 +0100
Subject: [PATCH] Implement trivial custom analyzer

---
 src/main/java/org/RI/P2/Indexer.java | 38 +++++++++++++++++++---------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/RI/P2/Indexer.java b/src/main/java/org/RI/P2/Indexer.java
index ac28ef4..b6fc78a 100644
--- a/src/main/java/org/RI/P2/Indexer.java
+++ b/src/main/java/org/RI/P2/Indexer.java
@@ -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);