Serialize dates in dash separation format
Change-Id: Ib35643cf0367c1e4987a82ec19ff4e37315a0b1b
diff --git a/Changes b/Changes
index 66f24ea..35f3e02 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.56.2 2018-02-19
+0.56.2 2018-03-07
- [feature] Introduce meta field retrieval method (diewald)
- [cleanup] Rename KrillQuery's "_"-method to "nr" to improve
compatibility with future versions of Java (diewald)
@@ -8,6 +8,10 @@
snippet generation (diewald)
- [bugfix] Removed problematic serialization of empty
annotations (diewald)
+ - [feature] Serialize metadata keywords as arrays, when
+ indexed properly (may require reindexing; diewald)
+ - [feature] Serialize dates in dash separation format, when
+ metadata field name ends with "Date" (diewald)
0.56.1 2018-01-31
- [bugfix] Changed relation serialization in snippet to work
diff --git a/src/main/java/de/ids_mannheim/korap/response/MetaFields.java b/src/main/java/de/ids_mannheim/korap/response/MetaFields.java
index 213d2d6..e28a404 100644
--- a/src/main/java/de/ids_mannheim/korap/response/MetaFields.java
+++ b/src/main/java/de/ids_mannheim/korap/response/MetaFields.java
@@ -9,7 +9,9 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
+
import de.ids_mannheim.korap.index.AbstractDocument;
+import de.ids_mannheim.korap.util.KrillDate;
import java.io.IOException;
@@ -20,6 +22,7 @@
import java.io.StringReader;
import java.util.*;
+import java.util.regex.*;
import org.apache.lucene.index.*;
@@ -32,6 +35,11 @@
// This advices the java compiler to ignore all loggings
public static final boolean DEBUG = false;
+ // TODO:
+ // This is a temporary indicator to check
+ // whether a date field is a date
+ private static final Pattern dateKeyPattern = Pattern.compile(".*Date$");
+
// Mapper for JSON serialization
ObjectMapper mapper = new ObjectMapper();
@@ -76,10 +84,25 @@
// Field has numeric value (possibly a date)
if (n != null) {
- // TODO:
- // check if the number is a date!
- mf.type = "type:number";
- mf.values.add(n.toString());
+ // Check if key indicates a date
+ Matcher dateMatcher = dateKeyPattern.matcher(mf.key);
+ if (dateMatcher.matches()) {
+ mf.type = "type:date";
+
+ // Check structure with KrillDate
+ KrillDate date = new KrillDate(n.toString());
+ if (date != null) {
+
+ // Serialize withz dash separation
+ mf.values.add(date.toDisplay());
+ };
+ }
+
+ // Field is a number
+ else {
+ mf.type = "type:number";
+ mf.values.add(n.toString());
+ };
}
// Field has a textual value
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 3ca878d..96f0249 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
@@ -290,8 +290,8 @@
switch (key) {
case "pubDate":
- assertEquals("type:number", field.at("/type").asText());
- assertEquals(20170701, field.at("/value").asInt());
+ assertEquals("type:date", field.at("/type").asText());
+ assertEquals("2017-07-01", field.at("/value").asText());
break;
case "textSigle":