diff --git a/src/main/java/com/coveo/pushapiclient/Document.java b/src/main/java/com/coveo/pushapiclient/Document.java
index 22fa35b4..cc94e3d3 100644
--- a/src/main/java/com/coveo/pushapiclient/Document.java
+++ b/src/main/java/com/coveo/pushapiclient/Document.java
@@ -1,8 +1,26 @@
package com.coveo.pushapiclient;
-import java.util.Map;
+import java.util.HashMap;
public class Document {
+ /**
+ * The metadata key-value pairs for a given document.
+ *
+ * Each metadata in the document must be unique.
+ *
+ * Metadata are case-insensitive (e.g., the Push API considers mykey, MyKey, myKey, MYKEY, etc. as identical).
+ *
+ * See https://docs.coveo.com/en/115 for more information.
+ */
+ public final HashMap metadata;
+ /**
+ * The list of permission sets for this item.
+ *
+ * This is useful when item based security is required (i.e., when security isn't configured at the source level).
+ *
+ * See https://docs.coveo.com/en/107 for more information.
+ */
+ public final DocumentPermissions[] permissions;
/**
* The Uniform Resource Identifier (URI) that uniquely identifies the document in a Coveo index.
*
@@ -73,24 +91,6 @@ public class Document {
* See https://docs.coveo.com/en/73 for more information.
*/
public CompressedBinaryData compressedBinaryData;
- /**
- * The metadata key-value pairs for a given document.
- *
- * Each metadata in the document must be unique.
- *
- * Metadata are case-insensitive (e.g., the Push API considers mykey, MyKey, myKey, MYKEY, etc. as identical).
- *
- * See https://docs.coveo.com/en/115 for more information.
- */
- public Map metadata;
- /**
- * The list of permission sets for this item.
- *
- * This is useful when item based security is required (i.e., when security isn't configured at the source level).
- *
- * See https://docs.coveo.com/en/107 for more information.
- */
- public DocumentPermissions[] permissions;
/**
* The file extension of the data you're pushing.
*
@@ -104,6 +104,7 @@ public class Document {
public Document() {
this.permissions = new DocumentPermissions[]{new DocumentPermissions()};
+ this.metadata = new HashMap();
}
}
diff --git a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java
index e7a7ce96..edda89ab 100644
--- a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java
+++ b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java
@@ -1,14 +1,28 @@
package com.coveo.pushapiclient;
import com.google.gson.Gson;
+import com.google.gson.JsonObject;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
+import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
public class DocumentBuilder {
- private Document document;
+
+ private static final ArrayList reservedKeynames = new ArrayList<>() {{
+ add("compressedBinaryData");
+ add("compressedBinaryDataFileId");
+ add("parentId");
+ add("fileExtension");
+ add("data");
+ add("permissions");
+ add("documentId");
+ add("orderingId");
+ }};
+
+ private final Document document;
public DocumentBuilder(String uri, String title) {
this.document = new Document();
@@ -133,7 +147,13 @@ public DocumentBuilder withDeniedPermissions() {
}
public String marshal() {
- return new Gson().toJson(this.document);
+ JsonObject jsonDocument = new Gson().toJsonTree(this.document).getAsJsonObject();
+ this.document.metadata.forEach((key, value) -> {
+ jsonDocument.add(key, new Gson().toJsonTree(value));
+ });
+ jsonDocument.remove("metadata");
+ return jsonDocument.toString();
+
}
private String dateFormat(DateTime dt) {
@@ -154,6 +174,8 @@ private void validateFileExtension(String fileExtension) {
}
private void validateReservedMetadataKeyNames(String key) {
- // TODO
+ if (reservedKeynames.contains(key)) {
+ throw new RuntimeException(String.format("Cannot use %s as a metadata key: It is a reserved keynames. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names", key));
+ }
}
}
diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java
index 650f0582..afdb857d 100644
--- a/src/main/java/com/coveo/testlocally/TestingLocally.java
+++ b/src/main/java/com/coveo/testlocally/TestingLocally.java
@@ -28,13 +28,21 @@ public static void main(String[] args) {
public static void testPushDocument(String sourceId, Source source) {
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
+ DocumentBuilder docWithMetadata = new DocumentBuilder("https://perdu.com/3", "the title 3").withMetadata(new HashMap<>() {{
+ put("foo", "bar");
+ put("my_field_1", "1");
+ put("my_field_2", false);
+ put("my_field_3", 1234);
+ put("my_field_4", new String[]{"a", "b", "c"});
+ }});
System.out.println(doc.marshal());
+ System.out.println(docWithMetadata.marshal());
try {
source.addOrUpdateDocument(sourceId, doc);
+ source.addOrUpdateDocument(sourceId, docWithMetadata);
} catch (IOException | InterruptedException e) {
System.out.println(e);
}
-
}
public static void testManageIdentities(Source source) {