Add meta data return values for indexing to communicate with Kanalito

Change-Id: I584cf052b6ee571c5f0f02c2ba33c739ad7f9a64
diff --git a/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java b/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
index dc16ccf..f1a4e12 100644
--- a/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
+++ b/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
@@ -9,6 +9,7 @@
 import com.fasterxml.jackson.annotation.*;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.TextField;
@@ -38,8 +39,8 @@
 public class FieldDocument extends AbstractDocument {
     ObjectMapper mapper = new ObjectMapper();
 
+    @JsonIgnore
     public Document doc = new Document();
-
     private FieldType tvField = new FieldType(TextField.TYPE_STORED);
     private FieldType tvNoField = new FieldType(TextField.TYPE_NOT_STORED);
     private FieldType keywords = new FieldType(TextField.TYPE_STORED);
diff --git a/src/main/java/de/ids_mannheim/korap/response/Response.java b/src/main/java/de/ids_mannheim/korap/response/Response.java
index a1f72a4..2dc6516 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Response.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Response.java
@@ -49,7 +49,10 @@
     private String benchmark;
     private boolean timeExceeded = false;
 
-    private static final String KORAL_VERSION = "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
+    private HashMap<String, ObjectNode> jsonFields;
+
+    private static final String KORAL_VERSION =
+        "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
 
 
     /**
@@ -441,6 +444,12 @@
         return (Response) this.moveNotificationsFrom(meta);
     };
 
+    public void addJsonNode (String key, ObjectNode value) {
+        if (this.jsonFields == null)
+            this.jsonFields = new HashMap<String, ObjectNode>(4);
+        this.jsonFields.put(key, value);
+    };
+
 
     /**
      * Serialize response as a {@link JsonNode}.
@@ -500,6 +509,11 @@
         if (this.totalResults != -2)
             meta.put("totalResults", this.totalResults);
 
+        // Add json fields as passed to the object
+        if (this.jsonFields != null) {
+            json.putAll(this.jsonFields);
+        };
+
         // KoralQuery query object
         if (this.query != null) {
             JsonNode queryNode = this.getQuery().toJsonNode();
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index 83f69b5..993fec3 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -43,6 +43,7 @@
 import java.sql.SQLException;
 import com.mchange.v2.c3p0.ComboPooledDataSource;
 
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 /**
  * Root resource (exposed at root path) of the Krill node.
@@ -136,6 +137,9 @@
         kresp.addMessage(681, "Document was added successfully",
                 fd.getID() != null ? fd.getID() : "Unknown");
 
+        // Mirror meta data
+        kresp.addJsonNode("text", (ObjectNode) fd.toJsonNode());
+
         return kresp.toJsonString();
     };
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java b/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
index 00b9874..b48b48e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
@@ -201,6 +201,7 @@
             FieldDocument fd = ki.addDoc(
                     getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
                     true);
+
         };
         ki.commit();
 
diff --git a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
index 1ab89fe..43222c7 100644
--- a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
+++ b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
@@ -8,6 +8,7 @@
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 import static org.junit.Assert.*;
 import org.junit.Test;
@@ -169,4 +170,18 @@
         assertEquals("tanja", kresp.getNode());
         assertTrue(kresp.hasTimeExceeded());
     };
+
+    @Test
+    public void testResponseJSONadd () throws IOException {
+        Response resp = new Response();
+        ObjectNode jNode = mapper.createObjectNode();
+        jNode.put("Hui", "works");
+        resp.addJsonNode("test", jNode);
+        JsonNode respJson = mapper.readTree(resp.toJsonString());
+
+        assertEquals(
+                "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+                respJson.at("/@context").asText());
+        assertEquals("works", respJson.at("/test/Hui").asText());
+    };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestResource.java b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
index b04490e..966d6e7 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestResource.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
@@ -115,7 +115,7 @@
         JsonNode res;
 
         for (String i : new String[] { "00001", "00002", "00003", "00004",
-                "00005", "00006", "02439" }) {
+                "00005", "00006" }) {
 
             String json = StringfromFile(getClass().getResource(
                     "/wiki/" + i + ".json").getFile());
@@ -129,7 +129,6 @@
 
                 res = mapper.readTree(resp);
                 assertEquals("milena", res.at("/meta/node").asText());
-                // System.err.println(res.toString());
             }
             catch (Exception e) {
                 fail("Server response failed " + e.getMessage()
@@ -137,6 +136,25 @@
             }
         };
 
+        String json = StringfromFile(getClass().getResource("/wiki/02439.json").getFile());
+        Entity jsonE = Entity.json(json);
+
+        try {
+            // Put new documents to the index
+            resp = target.path("/index/02439").request("application/json")
+                .put(jsonE, String.class);
+
+            res = mapper.readTree(resp);
+
+            // Check mirroring
+            assertEquals(2439, res.at("/text/UID").asInt());
+            assertEquals("milena", res.at("/meta/node").asText());
+        }
+        catch (Exception e) {
+            fail("Server response failed " + e.getMessage()
+                 + " (Known issue)");
+        };
+
         // Commit!
         resp = target.path("/index").request("application/json")
                 .post(Entity.text(""), String.class);
@@ -145,7 +163,6 @@
         assertEquals(683, res.at("/messages/0/0").asInt());
     };
 
-
     @Test
     public void testCollection () throws IOException {