# 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
        for hit in hits.get('hits', []):
            doc_type = get_doc_type_for_hit(hit)
            doc_cls = self._doc_cls_map.get(doc_type, DynamicDocument)
            self.hits.append(doc_cls(_hit=hit, _result=self))

        self.aggregations = {}
        for agg_name, agg_expr in self._query_aggs.items():
            raw_agg_data = raw_result.get('aggregations', {}).get(agg_name, {})
            agg_result = agg_expr.build_agg_result(
                raw_agg_data, self._doc_cls_map,
                mapper_registry=self._mapper_registry
            )
            self.aggregations[agg_name] = agg_result

        self.scroll_id = raw_result.get('_scroll_id')

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

    def __len__(self):
        return len(self.hits)

    def get_aggregation(self, name):
        return self.aggregations.get(name)

    def _populate_instances(self, doc_cls):
        docs = [doc for doc in self.hits if isinstance(doc, doc_cls)]
        instances = self._instance_mappers.get(doc_cls)(
            [doc._id for doc in docs]
        )

# 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

        for hit in hits.get('hits', []):
            doc_type = get_doc_type_for_hit(hit)
            doc_cls = self._doc_cls_map.get(doc_type, DynamicDocument)
            self.hits.append(doc_cls(_hit=hit, _result=self))

        self.aggregations = {}
        for agg_name, agg_expr in self._query_aggs.items():
            raw_agg_data = raw_result.get('aggregations', {}).get(agg_name, {})
            agg_result = agg_expr.build_agg_result(
                raw_agg_data, self._doc_cls_map,
                mapper_registry=self._mapper_registry
            )
            self.aggregations[agg_name] = agg_result

        self.scroll_id = raw_result.get('_scroll_id')

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

    def __len__(self):
        return len(self.hits)

    def get_aggregation(self, name):
        return self.aggregations.get(name)

    def _populate_instances(self, doc_cls):
        docs = [doc for doc in self.hits if isinstance(doc, doc_cls)]
        instances = self._instance_mappers.get(doc_cls)(
            [doc._id for doc in docs]
        )

        for doc in docs: