'Return a query set in which the returned objects have been annotated
with data aggregated from related fields.'

def annotate(self, *args, **kwargs):

    obj = self._clone()
    for arg in args:
        if isinstance(arg, Facet):
            obj._facets.append(arg)
        elif isinstance(arg, six.string_types):
            obj._facets.append(TermFacet(arg.replace('__', '.')))
        elif isinstance(arg, Agg):
            obj._aggs.append(arg)
        elif isinstance(arg, six.string_types):
            obj._facets.append(TermFacet(arg.replace('__', '.')))
        else:
            raise NotImplementedError('invalid    type')
    for (name, field) in kwargs.items():
        obj._facets.append(TermFacet(field=field.replace('__', '.'), name=name.replace('__', '.')))
        obj._aggs.append(TermsAgg(field=field.replace('__', '.'), name=name.replace('__', '.')))
    return obj
