def _generate_collapse(collapse):
    """Make collapse string for query from collapse configurations.

    A collapse configuration is a dictionary with following fields:
        1. field
            Field with which to collapse results.
        2. null_policy, optional
            Policy with which to handle missing data
        3. size, optional
            Number of documents per group to show in result.

    """
    if 'field' not in collapse:
        msg = 'Field is not specified in collapse configuration.'
        raise ValueError(msg)

    options = ['field={}'.format(collapse['field'])]

    if 'null_policy' in collapse:
        null_policy = collapse['null_policy']
        options.append('nullPolicy={}'.format(null_policy))

    if 'size' in collapse:
        size = collapse['size']
        options.append('size={}'.format(size))

    base_string = '{{!collapse {options}}}'
    return base_string.format(options=' '.join(options))