Browse code

[mapi] BM-14939 Fix: sort mail by internal date in ES

Nicolas Lascombes authored on 23/07/2019 10:25:11
Showing 3 changed files
... ...
@@ -65,6 +65,10 @@
65 65
 					"type": "date",
66 66
 					"store": true
67 67
 				},
68
+				"internalDate": {
69
+					"type": "date",
70
+					"store": true
71
+				},
68 72
 				"owner": {
69 73
 					"type": "keyword",
70 74
 					"store": false
... ...
@@ -238,6 +238,9 @@ public class MailIndexService implements IMailIndexService {
238 238
 		mutableContent.put("is", is);
239 239
 		mutableContent.put("itemId", item.internalId);
240 240
 		mutableContent.put("parentId", parentUid);
241
+		if (mail.internalDate != null) {
242
+			mutableContent.put("internalDate", mail.internalDate.toInstant().toString());
243
+		}
241 244
 		mutableContent.put(JOIN_FIELD, ImmutableMap.of("name", CHILD_TYPE, "parent", parentUid));
242 245
 
243 246
 		// deduplicate fields
... ...
@@ -795,6 +798,7 @@ public class MailIndexService implements IMailIndexService {
795 798
 		searchBuilder.addStoredField("itemId");
796 799
 		searchBuilder.addStoredField("uid");
797 800
 		searchBuilder.addStoredField("preview");
801
+		searchBuilder.addStoredField("internalDate");
798 802
 		searchBuilder.setFetchSource(true);
799 803
 		searchBuilder.setFrom(Long.valueOf(query.offset).intValue());
800 804
 		searchBuilder.setSize(Long.valueOf(query.maxResults).intValue());
... ...
@@ -840,8 +844,16 @@ public class MailIndexService implements IMailIndexService {
840 844
 		String subject = (String) source.get("subject");
841 845
 		logger.debug("matching result itemId:{} subject:'{}' in folder:{}", itemId, subject, folderUid);
842 846
 		int size = (int) source.get("size");
843
-		ZonedDateTime date = ZonedDateTime.parse((String) source.get("date"));
847
+
848
+		String internalDate = (String) source.get("internalDate");
849
+		ZonedDateTime date;
850
+		if (internalDate != null) {
851
+			date = ZonedDateTime.parse(internalDate);
852
+		} else {
853
+			date = ZonedDateTime.parse((String) source.get("date"));
854
+		}
844 855
 		Date messageDate = Date.from(date.toInstant());
856
+
845 857
 		List<String> flags = (List<String>) source.get("is");
846 858
 		boolean seen = flags.contains("seen");
847 859
 		boolean flagged = flags.contains("flagged");
... ...
@@ -83,7 +83,7 @@ class rcube_storage_bm extends rcube_imap {
83 83
 
84 84
   private $sortMap = array(
85 85
     "date" => "date",
86
-    "arrival" => "date",
86
+    "arrival" => "internalDate",
87 87
     "size" => "size",
88 88
     "from" => "headers.from",
89 89
     "to" => "headers.to",