<commit_before>
""" Contains code for indexing other objects """

from pyes import ES, QueryStringQuery, FunctionScoreQuery
from pyes.exceptions import IndexAlreadyExistsException, NoServerAvailable

from django.conf import settings

from mygpo.search.json import podcast_to_json
from mygpo.search.models import PodcastResult

import logging
logger = logging.getLogger(__name__)


def get_connection():
    """ Create a connection from Django settings """
    conn = ES([settings.ELASTICSEARCH_SERVER],
              timeout=settings.ELASTICSEARCH_TIMEOUT)
    return conn


def index_podcast(sender, **kwargs):
    """ Indexes a podcast """

    conn = get_connection()
    podcast = kwargs['instance']
    logger.info('Indexing podcast {0}', podcast)

    document = podcast_to_json(podcast)

    try:
        conn.index(document, settings.ELASTICSEARCH_INDEX,
                   'podcast', podcast.id)
    except NoServerAvailable:
        logger.exception('Indexing podcast failed')


def create_index():
    """ Creates the Elasticsearch index """
    conn = get_connection()

    logger.info('Creating index {0}', settings.ELASTICSEARCH_INDEX)
    try:
        conn.indices.create_index(settings.ELASTICSEARCH_INDEX)

    except IndexAlreadyExistsException as ex:
        logger.info(str(ex))


def search_podcasts(query):
    """ Search for podcasts according to 'query' """
    conn = get_connection()

    q = {<commit_msg>[Search] use podcasts ID (instead of UUD repr) for indexing<commit_after>
""" Contains code for indexing other objects """

from pyes import ES, QueryStringQuery, FunctionScoreQuery
from pyes.exceptions import IndexAlreadyExistsException, NoServerAvailable

from django.conf import settings

from mygpo.search.json import podcast_to_json
from mygpo.search.models import PodcastResult

import logging
logger = logging.getLogger(__name__)


def get_connection():
    """ Create a connection from Django settings """
    conn = ES([settings.ELASTICSEARCH_SERVER],
              timeout=settings.ELASTICSEARCH_TIMEOUT)
    return conn


def index_podcast(sender, **kwargs):
    """ Indexes a podcast """

    conn = get_connection()
    podcast = kwargs['instance']
    logger.info('Indexing podcast {0}', podcast)

    document = podcast_to_json(podcast)

    try:
        conn.index(document, settings.ELASTICSEARCH_INDEX,
                   'podcast', podcast.id.hex)
    except NoServerAvailable:
        logger.exception('Indexing podcast failed')


def create_index():
    """ Creates the Elasticsearch index """
    conn = get_connection()

    logger.info('Creating index {0}', settings.ELASTICSEARCH_INDEX)
    try:
        conn.indices.create_index(settings.ELASTICSEARCH_INDEX)

    except IndexAlreadyExistsException as ex:
        logger.info(str(ex))


def search_podcasts(query):
    """ Search for podcasts according to 'query' """
    conn = get_connection()

    q = {
{'fraction_non_alphanumeric': 0.09574468085106383, 'fraction_numerical': 0.0013726835964310226, 'mean_word_length': 3.8341625207296848, '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': '2059153', 'n_tokens_mistral': 835, 'n_tokens_neox': 808, 'n_words': 215}