Adds facets to search results.

    Construct a new result payload with `facets` added as a new
    top-level property that carries a mapping from unicode strings to
    lists of content_ids.  The `facet_features` lists the names of
    :class:`~dossier.fc.StringCounter` features in each result
    :class:`~dossier.fc.FeatureCollection` to harvest phrases for the
    facets list.  If not specified, `facet_features` defaults to
    `bowNP_sip`.

    The remainder of the facet logic is handled in the UI.

def add_facets(engine_result, facet_features=None):
    '''Adds facets to search results.

    Construct a new result payload with `facets` added as a new
    top-level property that carries a mapping from unicode strings to
    lists of content_ids.  The `facet_features` lists the names of
    :class:`~dossier.fc.StringCounter` features in each result
    :class:`~dossier.fc.FeatureCollection` to harvest phrases for the
    facets list.  If not specified, `facet_features` defaults to
    `bowNP_sip`.

    The remainder of the facet logic is handled in the UI.
    '''
    if facet_features is None:
        facet_features = ['bowNP_sip']
    phrases = collections.defaultdict(list)
    for r in engine_result['results']:
        cid = r[0]
        fc = r[1]
        for fname in facet_features:
            for phrase in fc.get(u'bowNP_sip', []):
                phrases[phrase].append(cid)
    return dict(engine_result, **{'facets': dict(phrases)})