<commit_before># coding: utf-8
import hues

from django.core.management.base import BaseCommand

from elasticsearch_dsl.connections import connections
from elasticsearch_flex.indexes import registered_indices


class Command(BaseCommand):
    help = 'Sync search indices, templates, and scripts.'

    def handle(self, *args, **options):
        indices = registered_indices()
        connection = connections.get_connection()
        hues.info('Using connection', connection)
        if len(indices):
            hues.info('Discovered', len(indices), 'Indexes')
        else:
            hues.warn('No search index found')
        for i, index in enumerate(indices, 1):
            hues.info('==> Initializing', index.__name__)

            index_name = index._doc_type.index
            connection.indices.close(index_name)
            index.init()
            connection.indices.open(index_name)

            hues.success('--> Done {0}/{1}'.format(i, len(indices)))
<commit_msg>flex_sync shouldnt fail if the index does not exists, or is closed<commit_after># coding: utf-8
import hues

from django.core.management.base import BaseCommand

from elasticsearch import exceptions
from elasticsearch_dsl.connections import connections
from elasticsearch_flex.indexes import registered_indices


class Command(BaseCommand):
    help = 'Sync search indices, templates, and scripts.'

    def handle(self, *args, **options):
        indices = registered_indices()
        connection = connections.get_connection()
        hues.info('Using connection', connection)
        if len(indices):
            hues.info('Discovered', len(indices), 'Indexes')
        else:
            hues.warn('No search index found')
        for i, index in enumerate(indices, 1):
            hues.info('==> Initializing', index.__name__)

            index_name = index._doc_type.index
            try:
                connection.indices.close(index_name)
            except exceptions.NotFoundError:
                pass
            index.init()
            connection.indices.open(index_name)

            hues.success('--> Done {0}/{1}'.format(i, len(indices)))

{'fraction_non_alphanumeric': 0.10378681626928471, 'fraction_numerical': 0.0037400654511453952, 'mean_word_length': 2.7478108581436076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130131', 'n_tokens_mistral': 573, 'n_tokens_neox': 554, 'n_words': 149}