def fix_nested_filter(query, parent_key):
    """
    Fix the invalid 'filter' in the Elasticsearch queries

    Args:
        query (dict): An Elasticsearch query
        parent_key (any): The parent key

    Returns:
        dict: An updated Elasticsearch query with filter replaced with query
    """
    if isinstance(query, dict):
        if 'filter' in query and parent_key == 'nested':
            copy = dict(query)
            if 'query' in copy:
                raise Exception("Unexpected 'query' found")
            copy['query'] = copy['filter']
            del copy['filter']
            return copy
        else:
            return {
                key: fix_nested_filter(value, key) for key, value in query.items()
            }
    elif isinstance(query, list):
        return [
            fix_nested_filter(piece, key) for key, piece in enumerate(query)
        ]
    else:
        return query