def group_hits()

in elkserver/docker/redelk-base/redelkinstalldata/scripts/modules/helpers.py [0:0]


def group_hits(hits, groupby, res=None):
    """Takes a list of hits and a list of field names (dot notation) and returns a grouped list"""
    if len(groupby) > 0:
        hits_list = {}
        # First time in the loop
        if res is None:
            for hit in hits:
                value = get_value(f"_source.{groupby[0]}", hit)
                if value in hits_list:
                    hits_list[value].append(hit)
                else:
                    hits_list[value] = [hit]
        else:
            for key, val in res.items():
                for hit in val:
                    value = get_value(f"_source.{groupby[0]}", hit)
                    tmp_key = f"{key} / {value}"
                    if tmp_key in hits_list:
                        hits_list[tmp_key].append(hit)
                    else:
                        hits_list[tmp_key] = [hit]
        groupby.pop(0)
        return group_hits(hits, groupby, hits_list)

    if res is None:
        return hits

    tmp_hits = []
    for key, value in res.items():
        tmp_hits.append(value[0])

    return tmp_hits