本文整理汇总了Python中mongo_connector.doc_managers.formatters.DefaultDocumentFormatter.transform_value方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultDocumentFormatter.transform_value方法的具体用法?Python DefaultDocumentFormatter.transform_value怎么用?Python DefaultDocumentFormatter.transform_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongo_connector.doc_managers.formatters.DefaultDocumentFormatter
的用法示例。
在下文中一共展示了DefaultDocumentFormatter.transform_value方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DocManager
# 需要导入模块: from mongo_connector.doc_managers.formatters import DefaultDocumentFormatter [as 别名]
# 或者: from mongo_connector.doc_managers.formatters.DefaultDocumentFormatter import transform_value [as 别名]
class DocManager(DocManagerBase):
"""Elasticsearch implementation of the DocManager interface.
Receives documents from an OplogThread and takes the appropriate actions on
Elasticsearch.
"""
def __init__(self, url, auto_commit_interval=DEFAULT_COMMIT_INTERVAL,
unique_key='_id', chunk_size=DEFAULT_MAX_BULK,
meta_index_name="mongodb_meta", meta_type="mongodb_meta",
attachment_field="content", **kwargs):
self.elastic = Elasticsearch(
hosts=[url], **kwargs.get('clientOptions', {}))
self.auto_commit_interval = auto_commit_interval
self.meta_index_name = meta_index_name
self.meta_type = meta_type
self.unique_key = unique_key
self.chunk_size = chunk_size
self.routing = kwargs.get('routing', {})
if self.auto_commit_interval not in [None, 0]:
self.run_auto_commit()
self._formatter = DefaultDocumentFormatter()
self.has_attachment_mapping = False
self.attachment_field = attachment_field
def _index_and_mapping(self, namespace):
"""Helper method for getting the index and type from a namespace."""
index, doc_type = namespace.split('.', 1)
return index.lower(), doc_type
def _get_parent_id(self, doc_type, doc):
"""Get parent ID from doc"""
if doc_type in self.routing:
if '_parent' in doc:
return doc.pop('_parent')
parent_field = self.routing[doc_type].get('parentField')
if not parent_field:
return None
parent_id = doc.pop(parent_field) if parent_field in doc else None
return self._formatter.transform_value(parent_id)
def _search_doc_by_id(self, index, doc_type, doc_id):
"""Search document in Elasticsearch by _id"""
result = self.elastic.search(index=index, doc_type=doc_type,
body={
'query': {
'ids': {
'type': doc_type,
'values': [u(doc_id)]
}
}
})
if result['hits']['total'] == 1:
return result['hits']['hits'][0]
else:
return None
def stop(self):
"""Stop the auto-commit thread."""
self.auto_commit_interval = None
def apply_update(self, doc, update_spec):
if "$set" not in update_spec and "$unset" not in update_spec:
# Don't try to add ns and _ts fields back in from doc
return update_spec
return super(DocManager, self).apply_update(doc, update_spec)
@wrap_exceptions
def handle_command(self, doc, namespace, timestamp):
db = namespace.split('.', 1)[0]
if doc.get('dropDatabase'):
dbs = self.command_helper.map_db(db)
for _db in dbs:
self.elastic.indices.delete(index=_db.lower())
if doc.get('renameCollection'):
raise errors.OperationFailed(
"elastic_doc_manager does not support renaming a mapping.")
if doc.get('create'):
db, coll = self.command_helper.map_collection(db, doc['create'])
if db and coll:
self.elastic.indices.put_mapping(
index=db.lower(), doc_type=coll,
body={
"_source": {"enabled": True}
})
if doc.get('drop'):
db, coll = self.command_helper.map_collection(db, doc['drop'])
if db and coll:
# This will delete the items in coll, but not get rid of the
# mapping.
warnings.warn("Deleting all documents of type %s on index %s."
"The mapping definition will persist and must be"
"removed manually." % (coll, db))
#.........这里部分代码省略.........