<commit_before>from django.conf import settings
from elasticsearch import Elasticsearch
from elasticsearch.helpers import reindex, bulk

from .utils import (
    get_analyzer,
    get_alias,
)
from configuration.utils import ConfigurationList


def get_elasticsearch():
    """
    Return an instance of the elastic search with the connection as
    specified in the settings (``ES_HOST`` and ``ES_PORT``).

    Returns:
        ``elasticsearch.Elasticsearch``.
    """
    return Elasticsearch(
        [{'host': settings.ES_HOST, 'port': settings.ES_PORT}])

es = get_elasticsearch()


def get_mappings(questionnaire_configuration):
    """
    Return the mappings of the questiongroups of a Questionnaire. This
    is used to identify the types of fields, making it possible to query
    them properly.

    String values are objects themselves with a key for each language
    available.

    Args:
        ``questionnaire_configuration`` (
        :class:`configuration.configuration.QuestionnaireConfiguration`)

    Returns:
        ``dict``. A dict containing the mappings of a questionnaire
        fields.
    """
    language_codes = [l[0] for l in settings.LANGUAGES]

    data_properties = {}
    for questiongroup in questionnaire_configuration.get_questiongroups():
        qg_properties = {}
        for question in questiongroup.questions:
            if question.field_type in ['char', 'text']:
                q_properties = {}
                for language_code in language_codes:
                    q = {
                        'type': 'string',
                    }
                    analyzer = get_analyzer(language_code)
                    if analyzer:
                        q.update({
                            'analyzer': analyzer,
                        })
                    q_properties[language_code] = q
                qg_properties[question.keyword] = {
                    'properties': q_properties,
                }
            elif question.field_type in ['checkbox']:
                qg_properties[question.keyword] = {'type': 'string'}

        data_properties[questiongroup.keyword] = {
            'type': 'nested',
            'properties': qg_properties,
        }

    name_properties = {}
    for language_code in language_codes:
        q = {'type': 'string'}
        analyzer = get_analyzer(language_code)
        if analyzer:
            q.update({'analyer': analyzer})
            name_properties[language_code] = q

    link_properties = {}
    for language_code in language_codes:
        link_properties[language_code] = {'type': 'string'}

    mappings = {
        'questionnaire': {
            'properties': {
                'data': {
                    'properties': data_properties,
                },
                'created': {
                    'type': 'date'
                },
                'updated': {
                    'type': 'date'
                },
                'translations': {
                    'type': 'string'
                },
                'configurations': {
                    'type': 'string'
                },
                'code': {
                    'type': 'string'<commit_msg>759 Typo / bugfix.<commit_after>from django.conf import settings
from elasticsearch import Elasticsearch
from elasticsearch.helpers import reindex, bulk

from .utils import (
    get_analyzer,
    get_alias,
)
from configuration.utils import ConfigurationList


def get_elasticsearch():
    """
    Return an instance of the elastic search with the connection as
    specified in the settings (``ES_HOST`` and ``ES_PORT``).

    Returns:
        ``elasticsearch.Elasticsearch``.
    """
    return Elasticsearch(
        [{'host': settings.ES_HOST, 'port': settings.ES_PORT}])

es = get_elasticsearch()


def get_mappings(questionnaire_configuration):
    """
    Return the mappings of the questiongroups of a Questionnaire. This
    is used to identify the types of fields, making it possible to query
    them properly.

    String values are objects themselves with a key for each language
    available.

    Args:
        ``questionnaire_configuration`` (
        :class:`configuration.configuration.QuestionnaireConfiguration`)

    Returns:
        ``dict``. A dict containing the mappings of a questionnaire
        fields.
    """
    language_codes = [l[0] for l in settings.LANGUAGES]

    data_properties = {}
    for questiongroup in questionnaire_configuration.get_questiongroups():
        qg_properties = {}
        for question in questiongroup.questions:
            if question.field_type in ['char', 'text']:
                q_properties = {}
                for language_code in language_codes:
                    q = {
                        'type': 'string',
                    }
                    analyzer = get_analyzer(language_code)
                    if analyzer:
                        q.update({
                            'analyzer': analyzer,
                        })
                    q_properties[language_code] = q
                qg_properties[question.keyword] = {
                    'properties': q_properties,
                }
            elif question.field_type in ['checkbox']:
                qg_properties[question.keyword] = {'type': 'string'}

        data_properties[questiongroup.keyword] = {
            'type': 'nested',
            'properties': qg_properties,
        }

    name_properties = {}
    for language_code in language_codes:
        q = {'type': 'string'}
        analyzer = get_analyzer(language_code)
        if analyzer:
            q.update({'analyzer': analyzer})
            name_properties[language_code] = q

    link_properties = {}
    for language_code in language_codes:
        link_properties[language_code] = {'type': 'string'}

    mappings = {
        'questionnaire': {
            'properties': {
                'data': {
                    'properties': data_properties,
                },
                'created': {
                    'type': 'date'
                },
                'updated': {
                    'type': 'date'
                },
                'translations': {
                    'type': 'string'
                },
                'configurations': {
                    'type': 'string'
                },
                'code': {
                    'type': 'string'
{'fraction_non_alphanumeric': 0.09505941213258287, 'fraction_numerical': 0.0007817385866166354, 'mean_word_length': 1.6720969089390143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '310986', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1523, 'n_words': 407}