Replaced array nodes of field values with a simple string.
Change-Id: Icdccc3440d47abf1ce2a91fb39840e6344fa5bd5
diff --git a/Changes b/Changes
index 4c54d21..713b281 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.60.2 2022-01-31
+0.60.2 2022-01-04
- [security] More log4j updates (diewald)
- [feature] Support for field value vector method (fixes #81; diewald)
- [cleanup] Moved and updated cache-tests from TestKrillCollectionIndex
diff --git a/src/main/java/de/ids_mannheim/korap/response/VirtualCorpusResponse.java b/src/main/java/de/ids_mannheim/korap/response/VirtualCorpusResponse.java
index fd8a512..44c4ea8 100644
--- a/src/main/java/de/ids_mannheim/korap/response/VirtualCorpusResponse.java
+++ b/src/main/java/de/ids_mannheim/korap/response/VirtualCorpusResponse.java
@@ -1,26 +1,34 @@
package de.ids_mannheim.korap.response;
import java.util.List;
+import static de.ids_mannheim.korap.util.KrillString.quote;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.util.RawValue;
public class VirtualCorpusResponse extends Response {
public JsonNode createKoralQueryForField (String fieldName,
List<String> fieldValues) {
- ArrayNode arrayNode = mapper.createArrayNode();
- for (String v : fieldValues) {
- arrayNode.add(v);
+ StringBuffer sb = new StringBuffer("[");
+ int size = fieldValues.size();
+ for (int i=0; i < size; i++) {
+ if (fieldValues.get(i).length() == 0)
+ continue;
+ sb.append(quote(fieldValues.get(i)));
+ if (i < size-1) {
+ sb.append(",");
+ }
}
+ sb.append("]");
ObjectNode collectionNode = mapper.createObjectNode();
collectionNode.put("@type", "koral:doc");
collectionNode.put("key", fieldName);
collectionNode.put("type", "type:string");
- collectionNode.set("value", arrayNode);
+ collectionNode.putRawValue("value", new RawValue(sb.toString()));
ObjectNode root = mapper.createObjectNode();
root.put("@context", KORAL_VERSION);
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestVcField.java b/src/test/java/de/ids_mannheim/korap/search/TestVcField.java
index b9b24ce..2c27204 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestVcField.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestVcField.java
@@ -9,6 +9,7 @@
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import de.ids_mannheim.korap.Krill;
import de.ids_mannheim.korap.KrillIndex;
@@ -17,29 +18,31 @@
public class TestVcField {
private KrillIndex ki;
+ private ObjectMapper mapper = new ObjectMapper();
private FieldDocument createFieldDoc (int uid, String textSigle) {
FieldDocument fd = new FieldDocument();
fd.addString("textSigle", textSigle);
fd.setUID(uid);
return fd;
-
}
public TestVcField () throws IOException {
ki = new KrillIndex();
ki.addDoc(createFieldDoc(1, "WPD/AAA/00001"));
-
+
FieldDocument fd = createFieldDoc(2, "WPD/AAA/00002");
fd.addString("author", "Frank");
+ fd.addString("quote", "The \"quoted\" example");
+ fd.addString("empty", "");
ki.addDoc(fd);
-
+
fd = createFieldDoc(3, "WPD/AAA/00003");
fd.addTV("tokens", "a b c", "[(0-1)s:a|i:a|_0$<i>0<i>1|-:t$<i>3]"
+ "[(2-3)s:b|i:b|_1$<i>2<i>3]" + "[(4-5)s:c|i:c|_2$<i>4<i>5]");
ki.addDoc(fd);
-
+
ki.addDoc(createFieldDoc(4, "WPD/AAA/00004"));
ki.commit();
ki.addDoc(createFieldDoc(5, "WPD/AAA/00005"));
@@ -61,33 +64,51 @@
String json = getJsonResource(file);
JsonNode n = new Krill().retrieveFieldValues(json, ki, "textSigle");
- System.out.println(n.toPrettyString());
assertEquals(Response.KORAL_VERSION, n.at("/@context").asText());
assertEquals("koral:doc", n.at("/corpus/@type").asText());
assertEquals("textSigle", n.at("/corpus/key").asText());
assertEquals("type:string", n.at("/corpus/type").asText());
- assertEquals(2, n.at("/corpus/value").size());
- assertEquals("WPD/AAA/00002", n.at("/corpus/value/0").asText());
- assertEquals("WPD/AAA/00003", n.at("/corpus/value/1").asText());
-
+ assertEquals("[\"WPD/AAA/00002\",\"WPD/AAA/00003\"]",
+ n.at("/corpus/value").toString());
+
testRetrieveAuthorOfVc1(json);
testRetrieveTokensOfVc1(json);
+ testRetrieveNullOfVc1(json);
+ testRetrieveQuoteOfVc1(json);
+ testRetrieveEmptyOfVc1(json);
}
private void testRetrieveAuthorOfVc1 (String json) {
JsonNode n = new Krill().retrieveFieldValues(json, ki, "author");
assertEquals("author", n.at("/corpus/key").asText());
- assertEquals("Frank", n.at("/corpus/value/0").asText());
+ assertEquals("[\"Frank\"]", n.at("/corpus/value").toString());
}
private void testRetrieveTokensOfVc1 (String json) {
JsonNode n = new Krill().retrieveFieldValues(json, ki, "tokens");
assertEquals("tokens", n.at("/corpus/key").asText());
- assertEquals(1, n.at("/corpus/value").size());
- assertEquals("a b c", n.at("/corpus/value/0").asText());
+ assertEquals("[\"a b c\"]", n.at("/corpus/value").toString());
+ }
+
+ private void testRetrieveNullOfVc1 (String json) {
+ JsonNode n = new Krill().retrieveFieldValues(json, ki, "hello");
+ assertEquals("hello", n.at("/corpus/key").asText());
+ assertEquals("[]", n.at("/corpus/value").toString());
+ }
+
+ private void testRetrieveQuoteOfVc1 (String json) {
+ JsonNode n = new Krill().retrieveFieldValues(json, ki, "quote");
+ assertEquals("quote", n.at("/corpus/key").asText());
+ assertEquals("[\"The \\\"quoted\\\" example\"]", n.at("/corpus/value").toString());
+ }
+
+ private void testRetrieveEmptyOfVc1 (String json) {
+ JsonNode n = new Krill().retrieveFieldValues(json, ki, "empty");
+ assertEquals("empty", n.at("/corpus/key").asText());
+ assertEquals("[]", n.at("/corpus/value").toString());
}
@@ -97,7 +118,11 @@
String file = "/queries/collections/named-vcs/named-vc3.jsonld";
JsonNode n = new Krill().retrieveFieldValues(getJsonResource(file), ki,
"textSigle");
- assertEquals(2, n.at("/corpus/value").size());
+
+ n = mapper.readTree(n.at("/corpus/value").toString());
+ assertEquals(2, n.size());
+ assertEquals("WPD/AAA/00002", n.at("/0").asText());
+ assertEquals("WPD/AAA/00003", n.at("/1").asText());
}
@@ -107,6 +132,9 @@
String file = "/queries/collections/named-vcs/named-vc4.jsonld";
JsonNode n = new Krill().retrieveFieldValues(getJsonResource(file), ki,
"textSigle");
- assertEquals(3, n.at("/corpus/value").size());
- }
+ n = mapper.readTree(n.at("/corpus/value").toString());
+ assertEquals(3, n.size());
+ assertEquals("WPD/AAA/00001", n.at("/0").asText());
+ assertEquals("WPD/AAA/00002", n.at("/1").asText());
+ assertEquals("WPD/AAA/00003", n.at("/2").asText()); }
}