From 16b181632b845640e581afa6b938b7e55367d273 Mon Sep 17 00:00:00 2001
From: Carina Antunes <carina.oliveira.antunes@cern.ch>
Date: Mon, 25 Nov 2019 09:23:30 +0000
Subject: [PATCH] [85] edms: file v5 (create record with_bucket flag)

---
 .env                                          |   2 +-
 .pipenv.env                                   |   2 +-
 .../modules/cernsearch/api.py                 |  21 +-
 .../edms/edms/document_v5.0.0.json            | 151 +++++++++++
 .../jsonschemas/edms/edms/file_v5.0.0.json    | 173 ++++++++++++
 .../edms/v6/edms/document_v5.0.0.json         | 226 ++++++++++++++++
 .../mappings/edms/v6/edms/file_v5.0.0.json    | 255 ++++++++++++++++++
 .../mappings/test/v6/test/api_ubq_v0.0.2.json |   9 +-
 .../mappings/test/v6/test/binary_v0.0.2.json  |   9 +-
 .../mappings/test/v6/test/date_v0.0.2.json    |   9 +-
 .../mappings/test/v6/test/doc_v0.0.2.json     |   9 +-
 .../mappings/test/v6/test/file_v0.0.2.json    |   7 +
 .../mappings/test/v6/test/nested_v0.0.2.json  |   9 +-
 .../test/v6/test/permission_v0.0.2.json       |   9 +-
 .../mappings/test/v6/test/suggest_v0.0.2.json |   9 +-
 .../modules/cernsearch/utils.py               |  31 ++-
 16 files changed, 916 insertions(+), 15 deletions(-)
 create mode 100644 cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/document_v5.0.0.json
 create mode 100644 cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/file_v5.0.0.json
 create mode 100644 cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/document_v5.0.0.json
 create mode 100644 cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/file_v5.0.0.json

diff --git a/.env b/.env
index 7d3064f9..3c03cd86 100644
--- a/.env
+++ b/.env
@@ -22,7 +22,7 @@ INVENIO_CERN_APP_CREDENTIALS={'consumer_key':'bah'}
 INVENIO_CERN_APP_CREDENTIALS_CONSUMER_KEY=xxx
 INVENIO_COLLECT_STORAGE=flask_collect.storage.file
 INVENIO_INDEXER_DEFAULT_DOC_TYPE=doc_v0.0.2
-INVENIO_INDEXER_DEFAULT_INDEX=cernsearch-test-doc_v0.0.2
+INVENIO_INDEXER_DEFAULT_INDEX=test-doc_v0.0.2
 INVENIO_LOGGING_CONSOLE='True'
 INVENIO_LOGGING_CONSOLE_LEVEL=DEBUG
 INVENIO_RATELIMIT_STORAGE_URL='redis://redis:6379/3'
diff --git a/.pipenv.env b/.pipenv.env
index 18f5364b..2551d773 100644
--- a/.pipenv.env
+++ b/.pipenv.env
@@ -22,7 +22,7 @@ INVENIO_CERN_APP_CREDENTIALS={'consumer_key':'bah'}
 INVENIO_CERN_APP_CREDENTIALS_CONSUMER_KEY=xxx
 INVENIO_COLLECT_STORAGE=flask_collect.storage.file
 INVENIO_INDEXER_DEFAULT_DOC_TYPE=doc_v0.0.2
-INVENIO_INDEXER_DEFAULT_INDEX=cernsearch-test-doc_v0.0.2
+INVENIO_INDEXER_DEFAULT_INDEX=test-doc_v0.0.2
 INVENIO_LOGGING_CONSOLE='True'
 INVENIO_LOGGING_CONSOLE_LEVEL=DEBUG
 INVENIO_RATELIMIT_STORAGE_URL='redis://localhost:6379/3'
diff --git a/cern_search_rest_api/modules/cernsearch/api.py b/cern_search_rest_api/modules/cernsearch/api.py
index 57640f53..76140286 100644
--- a/cern_search_rest_api/modules/cernsearch/api.py
+++ b/cern_search_rest_api/modules/cernsearch/api.py
@@ -4,10 +4,11 @@
 # This file is part of CERN Search.
 # Copyright (C) 2018-2019 CERN.
 #
-# CERN Search is free software; you can redistribute it and/or modify it
+# Citadel Search is free software; you can redistribute it and/or modify it
 # under the terms of the MIT License; see LICENSE file for more details.
 
-""" Record API"""
+"""Record API."""
+from cern_search_rest_api.modules.cernsearch.utils import default_record_to_mapping
 from invenio_pidstore.models import PersistentIdentifier
 from invenio_records_files.api import Record
 
@@ -15,12 +16,26 @@ from .fetchers import recid_fetcher
 
 
 class CernSearchRecord(Record):
-    """CERN Searc Record."""
+    """CERN Search Record."""
 
     record_fetcher = staticmethod(recid_fetcher)
 
+    @classmethod
+    def create(cls, data, id_=None, with_bucket=True, **kwargs):
+        """Create a record and the associated bucket.
+
+        :param with_bucket: Create a bucket automatically on record creation if mapping allows
+        """
+        bucket_allowed = False
+        mapping = default_record_to_mapping(data)
+        if mapping is not None:
+            bucket_allowed = '_bucket' in mapping['properties']
+
+        return super(CernSearchRecord, cls).create(data, id_=id_, with_bucket=bucket_allowed, **kwargs)
+
     @property
     def pid(self):
         """Return an instance of record PID."""
         pid = self.record_fetcher(self.id, self)
+
         return PersistentIdentifier.get(pid.pid_type, pid.pid_value)
diff --git a/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/document_v5.0.0.json b/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/document_v5.0.0.json
new file mode 100644
index 00000000..7a1c2760
--- /dev/null
+++ b/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/document_v5.0.0.json
@@ -0,0 +1,151 @@
+{
+    "title": "EDMS Object Type Document schema v5.0.0",
+    "id": "http://0.0.0.0:5000/schemas/edms/document_v5.0.0.json",
+    "$schema": "http://0.0.0.0:5000/schemas/edms/document_v5.0.0.json",
+    "type": "object",
+    "properties": {
+      "_access": {
+        "type": "object",
+        "properties": {
+          "owner":{
+            "type": "array",
+            "items": {
+              "type": "string"
+            }
+          },
+          "read":{
+            "type": "array",
+            "items": {
+              "type": "string"
+            }
+          },
+          "update":{
+            "type": "array",
+            "items": {
+              "type": "string"
+            }
+          },
+          "delete":{
+            "type": "array",
+            "items": {
+              "type": "string"
+            }
+          }
+        }
+      },
+      "_data": {
+        "type": "object",
+        "properties": {
+          "document_id": {
+            "type": "integer"
+          },
+          "document_id_version": {
+            "type": "string"
+          },
+          "cern_id": {
+            "type": "string"
+          },
+          "cern_id_version": {
+            "type": "string"
+          },
+          "external_reference": {
+            "type": "string"
+          },
+          "version": {
+            "type": "string"
+          },
+          "title": {
+            "type": "string"
+          },
+          "description": {
+            "type": "string"
+          },
+          "author": {
+            "type": "object",
+            "properties": {
+              "full_name": {
+                "type": "string"
+              },
+              "email": {
+                "type": "string"
+              }
+            }
+          },
+          "keywords": {
+            "type": "string"
+          },
+          "equipment_code": {
+            "type": "string"
+          }
+        }
+      },
+      "cid": {
+        "type": "integer"
+      },
+      "owner": {
+        "type": "string"
+      },
+      "group":{
+        "type": "string"
+      },
+      "object_type": {
+        "type": "string"
+      },
+      "parents": {
+        "type": "array",
+        "items": {
+          "type": "object"
+        }
+      },
+      "status": {
+        "type": "string"
+      },
+      "latest_version": {
+        "type": "boolean"
+      },
+      "obsolete": {
+        "type": "boolean"
+      },
+      "context": {
+        "type": "object",
+        "properties": {
+          "name": {
+            "type": "string"
+          },
+          "context_id": {
+            "type": "string"
+          }
+        }
+      },
+      "document_type": {
+        "type": "string"
+      },
+      "release_procedure": {
+        "type": "string"
+      },
+      "object_properties": {
+        "type": "array",
+        "items": {
+          "type": "object"
+        }
+      },
+      "attributes": {
+        "type": "array",
+        "items": {
+          "type": "string"
+        }
+      },
+      "creation_date": {
+        "type": "string"
+      },
+      "modification_date": {
+        "type": "string"
+      },
+      "control_number": {
+        "type": "string"
+      },
+      "$schema": {
+        "type": "string"
+      }
+    }
+  }
diff --git a/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/file_v5.0.0.json b/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/file_v5.0.0.json
new file mode 100644
index 00000000..a43d0bf7
--- /dev/null
+++ b/cern_search_rest_api/modules/cernsearch/jsonschemas/edms/edms/file_v5.0.0.json
@@ -0,0 +1,173 @@
+{
+  "title": "EDMS Object Type File schema v5.0.0",
+  "id": "http://0.0.0.0:5000/schemas/edms/file_v5.0.0.json",
+  "$schema": "http://0.0.0.0:5000/schemas/edms/file_v5.0.0.json",
+  "type": "object",
+  "properties": {
+    "_access": {
+      "type": "object",
+      "properties": {
+        "owner": {
+          "type": "array",
+          "items": {
+            "type": "string"
+          }
+        },
+        "read": {
+          "type": "array",
+          "items": {
+            "type": "string"
+          }
+        },
+        "update": {
+          "type": "array",
+          "items": {
+            "type": "string"
+          }
+        },
+        "delete": {
+          "type": "array",
+          "items": {
+            "type": "string"
+          }
+        }
+      }
+    },
+    "_data": {
+      "type": "object",
+      "properties": {
+        "file_name": {
+          "type": "string"
+        },
+        "document_id": {
+          "type": "integer"
+        },
+        "document_id_version": {
+          "type": "string"
+        },
+        "cern_id": {
+          "type": "string"
+        },
+        "cern_id_version": {
+          "type": "string"
+        },
+        "external_reference": {
+          "type": "string"
+        },
+        "version": {
+          "type": "string"
+        },
+        "title": {
+          "type": "string"
+        },
+        "description": {
+          "type": "string"
+        },
+        "author": {
+          "type": "object",
+          "properties": {
+            "full_name": {
+              "type": "string"
+            },
+            "email": {
+              "type": "string"
+            }
+          }
+        },
+        "keywords": {
+          "type": "string"
+        },
+        "equipment_code": {
+          "type": "string"
+        }
+      }
+    },
+    "file_id": {
+      "type": "integer"
+    },
+    "cid": {
+      "type": "integer"
+    },
+    "owner": {
+      "type": "string"
+    },
+    "group": {
+      "type": "string"
+    },
+    "object_type": {
+      "type": "string"
+    },
+    "parents": {
+      "type": "array",
+      "items": {
+        "type": "object",
+        "properties": {
+          "parent_type": {
+            "type": "string"
+          },
+          "parent_id": {
+            "type": "string"
+          }
+        }
+      }
+    },
+    "status": {
+      "type": "string"
+    },
+    "latest_version": {
+      "type": "boolean"
+    },
+    "obsolete": {
+      "type": "boolean"
+    },
+    "context": {
+      "type": "object",
+      "properties": {
+        "name": {
+          "type": "string"
+        },
+        "context_id": {
+          "type": "string"
+        }
+      }
+    },
+    "document_type": {
+      "type": "string"
+    },
+    "release_procedure": {
+      "type": "string"
+    },
+    "object_properties": {
+      "type": "array",
+      "items": {
+        "type": "object",
+        "properties": {
+          "name": {
+            "type": "string"
+          },
+          "value": {
+            "type": "string"
+          }
+        }
+      }
+    },
+    "attributes": {
+      "type": "array",
+      "items": {
+        "type": "string"
+      }
+    },
+    "creation_date": {
+      "type": "string"
+    },
+    "modification_date": {
+      "type": "string"
+    },
+    "control_number": {
+      "type": "string"
+    },
+    "$schema": {
+      "type": "string"
+    }
+  }
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/document_v5.0.0.json b/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/document_v5.0.0.json
new file mode 100644
index 00000000..b1064cd3
--- /dev/null
+++ b/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/document_v5.0.0.json
@@ -0,0 +1,226 @@
+{
+    "settings": {
+      "index.percolator.map_unmapped_fields_as_string": true,
+      "index.mapping.total_fields.limit": 500
+    },
+    "mappings": {
+      "document_v5.0.0": {
+        "dynamic": "strict",
+        "numeric_detection": true,
+        "properties": {
+          "_access": {
+            "type": "object",
+            "properties": {
+              "owner":{"type": "keyword"},
+              "read": {"type": "keyword"},
+              "update": {"type": "keyword"},
+              "delete": {"type": "keyword"}
+            }
+          },
+          "_data": {
+            "type": "object",
+            "properties": {
+              "document_id": {
+                "type": "keyword",
+                "boost": 20
+              },
+              "document_id_version": {
+                "type": "keyword",
+                "boost": 20
+              },
+              "cern_id": {
+                "type": "keyword",
+                "boost": 20
+              },
+              "cern_id_version": {
+                "type": "keyword",
+                "boost": 20
+              },
+              "external_reference": {
+                "type": "text",
+                "boost": 8,
+                "fields": {
+                  "exact_match": {
+                    "type": "keyword",
+                    "boost": 8
+                  }
+                }
+              },
+              "version": {
+                "type": "keyword",
+                "boost": 1
+              },
+              "title": {
+                "type": "text",
+                "boost": 8,
+                "fields": {
+                  "english": {
+                    "type": "text",
+                    "boost": 8,
+                    "analyzer": "english"
+                  },
+                  "french": {
+                    "type": "text",
+                    "boost": 8,
+                    "analyzer": "french"
+                  },
+                  "exact_match": {
+                    "type": "keyword",
+                    "boost": 8
+                  }
+                }
+              },
+              "description": {
+                "type": "text",
+                "boost": 4,
+                "fields": {
+                  "english": {
+                    "type": "text",
+                    "boost": 4,
+                    "analyzer": "english"
+                  },
+                  "french": {
+                    "type": "text",
+                    "boost": 4,
+                    "analyzer": "french"
+                  }
+                }
+              },
+              "author": {
+                "type": "object",
+                "properties": {
+                  "full_name": {
+                    "type": "text",
+                    "boost": 2
+                  },
+                  "email": {
+                    "type": "text",
+                    "boost": 2
+                  }
+                }
+              },
+              "keywords": {
+                "type": "text",
+                "boost": 6,
+                "fields": {
+                  "english": {
+                    "type": "text",
+                    "analyzer": "english",
+                    "boost": 6
+                  },
+                  "french": {
+                    "type": "text",
+                    "analyzer": "french",
+                    "boost": 6
+                  },
+                  "exact_match": {
+                    "type": "keyword",
+                    "boost": 6
+                  }
+                }
+              },
+              "equipment_code": {
+                "type": "keyword",
+                "boost": 6
+              }
+            }
+          },
+          "cid": {
+            "type": "keyword"
+          },
+          "owner": {
+            "type": "keyword"
+          },
+          "group": {
+            "type": "keyword"
+          },
+          "object_type": {
+            "type": "keyword"
+          },
+          "parents": {
+            "type": "object",
+            "properties": {
+              "parent_type": {
+                "type": "keyword"
+              },
+              "parent_id": {
+                "type": "keyword"
+              }
+            }
+          },
+          "status": {
+            "type": "keyword"
+          },
+          "latest_version": {
+            "type": "keyword"
+          },
+          "obsolete": {
+            "type": "keyword"
+          },
+          "context": {
+            "type": "object",
+            "properties": {
+              "name": {
+                "type": "keyword"
+              },
+              "context_id": {
+                "type": "keyword"
+              }
+            }
+          },
+          "document_type": {
+            "type": "keyword"
+          },
+          "release_procedure": {
+            "type": "keyword"
+          },
+          "object_properties": {
+            "properties": {
+              "name": {
+                "type": "keyword"
+              },
+              "value": {
+                "type": "text",
+                "fields": {
+                  "english": {
+                    "type": "text",
+                    "analyzer": "english"
+                  },
+                  "french": {
+                    "type": "text",
+                    "analyzer": "french"
+                  },
+                  "exact_match": {
+                    "type": "keyword"
+                  }
+                }
+              }
+            }
+          },
+          "attributes": {
+            "type": "keyword"
+          },
+          "creation_date": {
+            "type": "date",
+            "format": "yyyy-MM-dd'T'HH:mm:ss"
+          },
+          "modification_date": {
+            "type": "date",
+            "format": "yyyy-MM-dd'T'HH:mm:ss"
+          },
+          "_updated": {
+            "type": "date"
+          },
+          "_created": {
+            "type": "date"
+          },
+          "control_number": {
+            "type": "keyword"
+          },
+          "$schema": {
+            "enabled": false
+          }
+        }
+      }
+    }
+  }
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/file_v5.0.0.json b/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/file_v5.0.0.json
new file mode 100644
index 00000000..634b013b
--- /dev/null
+++ b/cern_search_rest_api/modules/cernsearch/mappings/edms/v6/edms/file_v5.0.0.json
@@ -0,0 +1,255 @@
+{
+  "settings": {
+    "index.percolator.map_unmapped_fields_as_string": true,
+    "index.mapping.total_fields.limit": 500
+  },
+  "mappings": {
+    "file_v5.0.0": {
+      "numeric_detection": true,
+      "properties": {
+        "_access": {
+          "type": "object",
+          "properties": {
+            "owner": {
+              "type": "keyword"
+            },
+            "read": {
+              "type": "keyword"
+            },
+            "update": {
+              "type": "keyword"
+            },
+            "delete": {
+              "type": "keyword"
+            }
+          }
+        },
+        "_bucket": {
+          "type": "keyword",
+          "enabled": false
+        },
+        "_data": {
+          "type": "object",
+          "properties": {
+            "file_name": {
+              "type": "text",
+              "fields": {
+                "english": {
+                  "type": "text",
+                  "analyzer": "english",
+                  "boost": 20
+                },
+                "french": {
+                  "type": "text",
+                  "analyzer": "french",
+                  "boost": 20
+                }
+              }
+            },
+            "document_id": {
+              "type": "keyword",
+              "boost": 20
+            },
+            "document_id_version": {
+              "type": "keyword",
+              "boost": 20
+            },
+            "cern_id": {
+              "type": "keyword",
+              "boost": 20
+            },
+            "cern_id_version": {
+              "type": "keyword",
+              "boost": 20
+            },
+            "external_reference": {
+              "type": "text",
+              "boost": 8,
+              "fields": {
+                "exact_match": {
+                  "type": "keyword",
+                  "boost": 8
+                }
+              }
+            },
+            "version": {
+              "type": "keyword",
+              "boost": 1
+            },
+            "title": {
+              "type": "text",
+              "boost": 8,
+              "fields": {
+                "english": {
+                  "type": "text",
+                  "boost": 8,
+                  "analyzer": "english"
+                },
+                "french": {
+                  "type": "text",
+                  "boost": 8,
+                  "analyzer": "french"
+                },
+                "exact_match": {
+                  "type": "keyword",
+                  "boost": 8
+                }
+              }
+            },
+            "description": {
+              "type": "text",
+              "boost": 4,
+              "fields": {
+                "english": {
+                  "type": "text",
+                  "boost": 4,
+                  "analyzer": "english"
+                },
+                "french": {
+                  "type": "text",
+                  "boost": 4,
+                  "analyzer": "french"
+                }
+              }
+            },
+            "author": {
+              "type": "object",
+              "properties": {
+                "full_name": {
+                  "type": "text",
+                  "boost": 2
+                },
+                "email": {
+                  "type": "text",
+                  "boost": 2
+                }
+              }
+            },
+            "keywords": {
+              "type": "text",
+              "boost": 6,
+              "fields": {
+                "english": {
+                  "type": "text",
+                  "analyzer": "english",
+                  "boost": 6
+                },
+                "french": {
+                  "type": "text",
+                  "analyzer": "french",
+                  "boost": 6
+                },
+                "exact_match": {
+                  "type": "keyword",
+                  "boost": 6
+                }
+              }
+            },
+            "equipment_code": {
+              "type": "keyword",
+              "boost": 6
+            }
+          }
+        },
+        "file_cid": {
+          "type": "keyword"
+        },
+        "cid": {
+          "type": "keyword"
+        },
+        "owner": {
+          "type": "keyword"
+        },
+        "group": {
+          "type": "keyword"
+        },
+        "object_type": {
+          "type": "keyword"
+        },
+        "parents": {
+          "type": "object",
+          "properties": {
+            "parent_type": {
+              "type": "keyword"
+            },
+            "parent_id": {
+              "type": "keyword"
+            }
+          }
+        },
+        "status": {
+          "type": "keyword"
+        },
+        "latest_version": {
+          "type": "keyword"
+        },
+        "obsolete": {
+          "type": "keyword"
+        },
+        "context": {
+          "type": "object",
+          "properties": {
+            "name": {
+              "type": "keyword"
+            },
+            "context_id": {
+              "type": "keyword"
+            }
+          }
+        },
+        "document_type": {
+          "type": "keyword"
+        },
+        "release_procedure": {
+          "type": "keyword"
+        },
+        "object_properties": {
+          "properties": {
+            "name": {
+              "type": "keyword"
+            },
+            "value": {
+              "type": "text",
+              "fields": {
+                "english": {
+                  "type": "text",
+                  "analyzer": "english"
+                },
+                "french": {
+                  "type": "text",
+                  "analyzer": "french"
+                },
+                "exact_match": {
+                  "type": "keyword"
+                }
+              }
+            }
+          }
+        },
+        "attributes": {
+          "type": "keyword"
+        },
+        "creation_date": {
+          "type": "date",
+          "format": "yyyy-MM-dd'T'HH:mm:ss"
+        },
+        "modification_date": {
+          "type": "date",
+          "format": "yyyy-MM-dd'T'HH:mm:ss"
+        },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
+        "control_number": {
+          "type": "keyword"
+        },
+        "$schema": {
+          "enabled": false
+        }
+      }
+    }
+  }
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/api_ubq_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/api_ubq_v0.0.2.json
index f5ab5280..e26dd7ba 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/api_ubq_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/api_ubq_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "api_ubq_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -35,6 +36,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -44,4 +51,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/binary_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/binary_v0.0.2.json
index d6372fca..cf329d7a 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/binary_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/binary_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "binary_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -104,6 +105,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -113,4 +120,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/date_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/date_v0.0.2.json
index 353b9b25..f7e23803 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/date_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/date_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "date_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -49,6 +50,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -58,4 +65,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/doc_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/doc_v0.0.2.json
index dc5f736e..aa55acf6 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/doc_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/doc_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "doc_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -48,6 +49,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -57,4 +64,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/file_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/file_v0.0.2.json
index 733db26b..55d5675f 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/file_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/file_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "file_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -38,6 +39,12 @@
         "_bucket": {
           "type": "keyword"
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/nested_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/nested_v0.0.2.json
index c56cab4b..66db8448 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/nested_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/nested_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "nested_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -64,6 +65,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -73,4 +80,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/permission_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/permission_v0.0.2.json
index c5cfeac7..d3fb8a97 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/permission_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/permission_v0.0.2.json
@@ -5,6 +5,7 @@
   },
   "mappings": {
     "permission_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -35,6 +36,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "control_number": {
           "type": "keyword"
         },
@@ -44,4 +51,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/suggest_v0.0.2.json b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/suggest_v0.0.2.json
index 1d7b7a4e..5bf38015 100644
--- a/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/suggest_v0.0.2.json
+++ b/cern_search_rest_api/modules/cernsearch/mappings/test/v6/test/suggest_v0.0.2.json
@@ -42,6 +42,7 @@
   },
   "mappings": {
     "suggest_v0.0.2": {
+      "dynamic": "strict",
       "numeric_detection": true,
       "_meta": {
         "_owner": "CernSearch-Administrators@cern.ch"
@@ -82,6 +83,12 @@
             }
           }
         },
+        "_updated": {
+          "type": "date"
+        },
+        "_created": {
+          "type": "date"
+        },
         "suggest": {
           "type": "text",
           "analyzer": "autocomplete",
@@ -101,4 +108,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/cern_search_rest_api/modules/cernsearch/utils.py b/cern_search_rest_api/modules/cernsearch/utils.py
index a6dcf4e0..f4584df8 100644
--- a/cern_search_rest_api/modules/cernsearch/utils.py
+++ b/cern_search_rest_api/modules/cernsearch/utils.py
@@ -4,14 +4,15 @@
 # This file is part of CERN Search.
 # Copyright (C) 2018-2019 CERN.
 #
-# CERN Search is free software; you can redistribute it and/or modify it
+# Citadel Search is free software; you can redistribute it and/or modify it
 # under the terms of the MIT License; see LICENSE file for more details.
 
 """Helper methods for CERN Search records."""
 
 from flask import current_app, g
-from invenio_indexer.utils import schema_to_index
-from invenio_search import current_search
+from invenio_indexer.utils import default_record_to_index, schema_to_index
+from invenio_search import current_search, current_search_client
+from invenio_search.utils import prefix_index
 
 
 def get_user_provides():
@@ -40,3 +41,27 @@ def record_from_index(record):
     else:
         return (current_app.config['INDEXER_DEFAULT_INDEX'],
                 current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
+
+
+def default_record_to_mapping(record):
+    """Get mapping given a record.
+
+    It tries to extract from `record['$schema']` the index and doc_type.
+    If it fails, uses the default values.
+
+    :param record: The record object.
+    :returns: mapping
+    """
+    index, doc = default_record_to_index(record)
+    index = prefix_index(index)
+    current_app.logger.debug('Using index {idx} and doc {doc}'.format(idx=index, doc=doc))
+
+    mapping = current_search_client.indices.get_mapping([index])
+    if mapping is not None:
+        doc_type = next(iter(mapping))
+        current_app.logger.debug('Using mapping for {idx}'.format(idx=index))
+        current_app.logger.debug('Mapping {mapping}'.format(mapping=mapping))
+
+        return mapping[doc_type]['mappings'][doc]
+
+    return None
-- 
GitLab