'Set up reindexing tasks.
Creates a Tasktree that creates a new indexes and indexes all objects,
then points the alias to this new index when finished.'

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

    index_choice = kwargs.get('index', None)
    prefix = kwargs.get('prefix', '')
    force = kwargs.get('force', False)
    if index_choice:
        INDEXES = INDEX_CHOICES.get(index_choice, None)
        if (INDEXES is None):
            raise CommandError(('Incorrect    index    name    specified.    Choose    one    of:    %s' % ',    '.join(INDEX_CHOICES.keys())))
    else:
        INDEXES = INDEXERS
    if (Reindexing.is_reindexing() and (not force)):
        raise CommandError('Indexation    already    occuring    -    use    --force    to    bypass')
    elif force:
        Reindexing.unflag_reindexing()
    for INDEXER in INDEXES:
        index_name = INDEXER.get_mapping_type_name()
        chunk_size = INDEXER.chunk_size
        alias = ES_INDEXES[index_name]
        (chunks, total) = chunk_indexing(INDEXER, chunk_size)
        if (not total):
            _print('No    items    to    queue.', alias)
        else:
            total_chunks = int(ceil((total / float(chunk_size))))
            _print('Indexing    {total}    items    into    {n}    chunks    of    size    {size}'.format(total=total, n=total_chunks, size=chunk_size), alias)
        try:
            aliases = ES.indices.get_alias(name=alias).keys()
        except elasticsearch.NotFoundError:
            aliases = []
        old_index = (aliases[0] if aliases else None)
        new_index = timestamp_index((prefix + alias))
        if old_index:
            try:
                s = ES.indices.get_settings(index=old_index).get(old_index, {}).get('settings', {})
            except elasticsearch.NotFoundError:
                s = {}
        else:
            s = {}
        num_replicas = s.get('number_of_replicas', settings.ES_DEFAULT_NUM_REPLICAS)
        num_shards = s.get('number_of_shards', settings.ES_DEFAULT_NUM_SHARDS)
        pre_task = pre_index.si(new_index, old_index, alias, index_name, {'analysis': INDEXER.get_analysis(), 'number_of_replicas': 0, 'number_of_shards': num_shards, 'store.compress.tv': True, 'store.compress.stored': True, 'refresh_interval': '-1'})
        post_task = post_index.si(new_index, old_index, alias, index_name, {'number_of_replicas': num_replicas, 'refresh_interval': '5s'})
        if (not total):
            chain(pre_task, post_task).apply_async()
        else:
            index_tasks = [run_indexing.si(new_index, index_name, chunk) for chunk in chunks]
            if settings.CELERY_ALWAYS_EAGER:
                index_tasks.insert(0, pre_task)
                index_tasks.append(post_task)
                chain(*index_tasks).apply_async()
            else:
                chain(pre_task, chord(header=index_tasks, body=post_task)).apply_async()
    _print('New    index    and    indexing    tasks    all    queued    up.')
