# Path: elasticmagic/compat.py
# PY2 = sys.version_info[0] == 2
# def force_unicode(value):
# def with_metaclass(meta, *bases):
#         def __new__(cls, name, this_bases, d):
#     class metaclass(meta):
# 
# Path: elasticmagic/document.py
# class DynamicDocument(with_metaclass(DynamicDocumentMeta, Document)):
#     def _process_source_key_value(self, key, value):
#         key, value = (
#             super(DynamicDocument, self)._process_source_key_value(key, value)
#         )
#         if isinstance(value, dict):
#             return key, DynamicDocument(**value)
#         return key, value
# 
# Path: elasticmagic/document.py
# def get_doc_type_for_hit(hit):
#     fields = hit.get('fields', {})
#     custom_doc_type = fields.get(DOC_TYPE_NAME_FIELD)
#     if custom_doc_type:
#         return custom_doc_type[0]
#     return hit['_type']


from .compat import string_types
from .document import DynamicDocument
from .document import get_doc_type_for_hit

class ErrorReason(object):
    def __init__(self, raw_error):
        self.type = raw_error.get('type')
        self.reason = raw_error.get('reason')
        self.caused_by = raw_error.get('caused_by')


class BulkResult(Result):
    def __init__(self, raw_result):
        super(BulkResult, self).__init__(raw_result)
        self.took = raw_result['took']
        self.errors = raw_result['errors']
        self.items = list(map(ActionResult, raw_result['items']))

    def __iter__(self):
        return iter(self.items)


class DeleteResult(Result):
    def __init__(self, raw_result):
        super(DeleteResult, self).__init__(raw_result)
        self.found = raw_result.get('found')
        self.result = raw_result.get('result')
        self._index = raw_result['_index']
        self._type = raw_result['_type']
        self._id = raw_result['_id']
        self._version = raw_result['_version']



# Path: elasticmagic/compat.py
# PY2 = sys.version_info[0] == 2
# def force_unicode(value):
# def with_metaclass(meta, *bases):
#         def __new__(cls, name, this_bases, d):
#     class metaclass(meta):
# 
# Path: elasticmagic/document.py
# class DynamicDocument(with_metaclass(DynamicDocumentMeta, Document)):
#     def _process_source_key_value(self, key, value):
#         key, value = (
#             super(DynamicDocument, self)._process_source_key_value(key, value)
#         )
#         if isinstance(value, dict):
#             return key, DynamicDocument(**value)
#         return key, value
# 
# Path: elasticmagic/document.py
# def get_doc_type_for_hit(hit):
#     fields = hit.get('fields', {})
#     custom_doc_type = fields.get(DOC_TYPE_NAME_FIELD)
#     if custom_doc_type:
#         return custom_doc_type[0]
#     return hit['_type']


# Path: elasticmagic/result.py
from .compat import string_types
from .document import DynamicDocument
from .document import get_doc_type_for_hit


class ErrorReason(object):
    def __init__(self, raw_error):
        self.type = raw_error.get('type')
        self.reason = raw_error.get('reason')
        self.caused_by = raw_error.get('caused_by')


class BulkResult(Result):
    def __init__(self, raw_result):
        super(BulkResult, self).__init__(raw_result)
        self.took = raw_result['took']
        self.errors = raw_result['errors']
        self.items = list(map(ActionResult, raw_result['items']))

    def __iter__(self):
        return iter(self.items)


class DeleteResult(Result):
    def __init__(self, raw_result):
        super(DeleteResult, self).__init__(raw_result)
        self.found = raw_result.get('found')
        self.result = raw_result.get('result')
        self._index = raw_result['_index']
        self._type = raw_result['_type']
        self._id = raw_result['_id']
        self._version = raw_result['_version']



class DeleteByQueryResult(Result):