<commit_before>from haystack.query import SearchQuerySet, SQ
from haystack.inputs import AutoQuery
from shownotes.models import Show
from django.db.models import Max
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def search(string, topics, min_show=0, max_show=None):
    if max_show is None:
        max_show = Show.objects.all().aggregate(Max('id'))['id__max']
    if string == '':
        return SearchQuerySet().filter(
            topic_id__in=topics, show_number__gte=min_show,
            show_number__lte=max_show).order_by('-show_number', 'topic_name')
    else:
        return SearchQuerySet().filter(
            SQ(text=AutoQuery(string)), topic_id__in=topics,
            show_number__gte=min_show, show_number__lte=max_show)


def paginate(results, page, limit):
    paginator = Paginator(results, limit)
    try:
        paged_results = paginator.page(page)
        return (paged_results, page, paginator.num_pages)
    except EmptyPage:
        return ([], 1, 1)
    except PageNotAnInteger:
        return (paginator.page(1), 1, paginator.num_pages)


def topics_in_show(number):
    return SearchQuerySet().filter(show_number=number).order_by('topic_name')<commit_msg>sorting ascending/descending in the same query isn't supported by Whoosh fixed to sort both ascending<commit_after>from haystack.query import SearchQuerySet, SQ
from haystack.inputs import AutoQuery
from shownotes.models import Show
from django.db.models import Max
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def search(string, topics, min_show=0, max_show=None):
    if max_show is None:
        max_show = Show.objects.all().aggregate(Max('id'))['id__max']
    if string == '' or string == '*':
        return SearchQuerySet().filter(
            topic_id__in=topics, show_number__gte=min_show,
            show_number__lte=max_show).order_by('show_number', 'topic_name')
    else:
        return SearchQuerySet().filter(
            SQ(text=AutoQuery(string)), topic_id__in=topics,
            show_number__gte=min_show, show_number__lte=max_show)


def paginate(results, page, limit):
    paginator = Paginator(results, limit)
    try:
        paged_results = paginator.page(page)
        return (paged_results, page, paginator.num_pages)
    except EmptyPage:
        return ([], 1, 1)
    except PageNotAnInteger:
        return (paginator.page(1), 1, paginator.num_pages)


def topics_in_show(number):
    return SearchQuerySet().filter(show_number=number).order_by('topic_name')
{'fraction_non_alphanumeric': 0.10347551342812006, 'fraction_numerical': 0.0039494470774091624, 'mean_word_length': 3.9764243614931236, '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': '474214', 'n_tokens_mistral': 804, 'n_tokens_neox': 779, 'n_words': 171}