@wrap_exceptions<EOL><INDENT>def upsert(self, doc, namespace, timestamp, update_spec=None):<DEDENT>
index, doc_type = self._index_and_mapping(namespace)<EOL>doc_id = str(doc.pop("<STR_LIT>"))<EOL>metadata = {"<STR_LIT>": namespace, "<STR_LIT>": timestamp}<EOL>action = {<EOL>"<STR_LIT>": "<STR_LIT:index>",<EOL>"<STR_LIT>": index,<EOL>"<STR_LIT>": doc_type,<EOL>"<STR_LIT>": doc_id,<EOL>"<STR_LIT>": self._formatter.format_document(doc),<EOL>}<EOL>meta_action = {<EOL>"<STR_LIT>": "<STR_LIT:index>",<EOL>"<STR_LIT>": self.meta_index_name,<EOL>"<STR_LIT>": self.meta_type,<EOL>"<STR_LIT>": doc_id,<EOL>"<STR_LIT>": bson.json_util.dumps(metadata),<EOL>}<EOL>self.index(action, meta_action, doc, update_spec)<EOL>doc["<STR_LIT>"] = doc_id<EOL>
Insert a document into Elasticsearch.