Support multiple metadata fields with identical keys (ignoring types)

Change-Id: I7b373b27edd53eb61f84ba2a78d6b3a77a1e7bdf
diff --git a/Changes b/Changes
index e465ea2..8fa53fd 100644
--- a/Changes
+++ b/Changes
@@ -1,12 +1,12 @@
-0.58.4 2019-01-10
+0.58.4 2019-01-16
     - [cleanup] Remove deprecated methods setLicense/getLicense,
       setTokenization/getTokenization, setLayerInfo/getLayerInfo,
       setField/getField (including json serialization)
       (diewald)
     - [cleanup] Remove redundancy for meta field setting in
       AbstractDocument and FieldDocument (diewald)
-    - [cleanup] Remove hardwired deserialization of legacy metadata
-      fields  (diewald)
+    - [cleanup] Remove hardwired (de)serialization of legacy metadata
+      fields (diewald)
 
 0.58.3 2018-12-17
     - [feature] Introduced attachements as meta data fields
diff --git a/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java b/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
index fb467ff..444d862 100644
--- a/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
+++ b/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
@@ -152,11 +152,19 @@
     };
 
 
-    public void populateFields (Document doc, Collection<String> fields) {        
+    public void populateFields (Document doc, Collection<String> fields) {
+        // Remove all fields already set
+        Iterator<String> fieldsIter = fields.iterator();
+        while (fieldsIter.hasNext()) {
+            if (mFields.contains(fieldsIter.next())) {
+                fieldsIter.remove();
+            };
+        };
+
         if (fields.contains("UID"))
             this.setUID(doc.get("UID"));
-
-        Iterator<String> fieldsIter = fields.iterator();
+        
+        fieldsIter = fields.iterator();
 
         while (fieldsIter.hasNext()) {
             String name = fieldsIter.next();
diff --git a/src/main/java/de/ids_mannheim/korap/response/MetaFieldsObj.java b/src/main/java/de/ids_mannheim/korap/response/MetaFieldsObj.java
index 51131e3..049fc3a 100644
--- a/src/main/java/de/ids_mannheim/korap/response/MetaFieldsObj.java
+++ b/src/main/java/de/ids_mannheim/korap/response/MetaFieldsObj.java
@@ -69,6 +69,11 @@
 		if (mf == null)
 			return null;
 
+        if (fieldsMap.containsKey(mf.key)) {
+            fieldsMap.get(mf.key).values.addAll(mf.values);
+            return fieldsMap.get(mf.key);
+        };
+        
         fieldsMap.put(mf.key, mf);
         return mf;
     };
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
index 934ac83..b9c2320 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
@@ -198,12 +198,10 @@
 		fd.addStored("ref", "My reference");
 		fd.addAttachement("ref2", "data:text/plain;charset=UTF-8,My reference2");
 
-        /*
 		fd.addKeywords("keyword", "baum");
 		fd.addKeywords("keyword", "wald");
-        */
 
-        fd.addKeywords("keyword", "baum wald");
+        fd.addKeywords("keyword", "garten laube");
 
 		fd.addText("title", "Der Name der Rose");
 
@@ -258,6 +256,8 @@
 				assertEquals("koral:field", field.at("/@type").asText());
 				assertEquals("baum", field.at("/value/0").asText());
 				assertEquals("wald", field.at("/value/1").asText());
+				assertEquals("garten", field.at("/value/2").asText());
+				assertEquals("laube", field.at("/value/3").asText());
 				checkC++;
 				break;
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java b/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
index 607d146..7b2df00 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
@@ -920,6 +920,7 @@
         KrillIndex ki = new KrillIndex();
         ki.addDoc(createSimpleFieldDoc4());
         ki.commit();
+        
         Match km = ki.getMatchInfo("match-c1!d4-p3-9", "tokens", null, null,
                 false, false);
         JsonNode res = mapper.readTree(km.toJsonString());