Esempio n. 1
0
def post_search(request):

    query = request.GET.get('query', '')
    length = len(query.replace(" ", ""))
    page = int(request.GET.get('page', 1))
    sortmap = dict(edit="lastedit_date", creation="creation_date", type='type')
    sorting = request.GET.get('sort')
    sortedby = [sortmap.get(sorting, "lastedit_date")]

    if length < settings.SEARCH_CHAR_MIN:
        messages.error(request, "Enter more characters before preforming search.")
        return redirect(reverse('post_list'))

    sortedby += ["lastedit_date"]
    sortedby = set(sortedby)
    results = search.preform_whoosh_search(query=query, page=page, sortedby=sortedby,
                                           reverse=True)

    total = results.total
    template_name = "search/search_results.html"

    question_flag = Post.QUESTION
    context = dict(results=results, query=query, total=total, template_name=template_name,
                   question_flag=question_flag, stop_words=','.join(search.STOP),
                   sort=sorting)

    return render(request, template_name=template_name, context=context)
Esempio n. 2
0
def search_spam(
    post,
    ix,
):
    """
    Search spam index for posts similar to this one.
    Returns
    """
    writer = AsyncWriter(ix)
    add_post_to_index(post=post, writer=writer, is_spam=post.is_spam)
    writer.commit()

    # Search for this post in the spam index
    fields = ['uid']

    results = search.preform_whoosh_search(ix=ix,
                                           query=post.uid,
                                           fields=fields)

    # Preform more_like_this on this posts content
    similar_content = results[0].more_like_this('content', top=5)

    # Remove this post from the spam index after results are collected.
    writer = AsyncWriter(ix)
    writer.delete_by_term('uid', text=post.uid)
    writer.commit()

    # Get the results into a list and close the searcher object.
    similar_content = list(map(search.normalize_result, similar_content))

    results.searcher.close()

    return similar_content
Esempio n. 3
0
def post_search(request):

    query = request.GET.get('query', '')
    page = int(request.GET.get('page', 1))

    if not query:
        return redirect(reverse('post_list'))

    results = search.preform_whoosh_search(
        query=query, page=page, per_page=settings.SEARCH_RESULTS_PER_PAGE)

    if isinstance(results, list) or not len(results):
        results = search.SearchResult()

    total = results.total
    template_name = "widgets/search_results.html"

    question_flag = Post.QUESTION
    context = dict(results=results,
                   query=query,
                   total=total,
                   template_name=template_name,
                   question_flag=question_flag,
                   stop_words=','.join(search.STOP))

    return render(request, template_name=template_name, context=context)
Esempio n. 4
0
def post_search(request):

    query = request.GET.get('query', '')
    length = len(query.replace(" ", ""))
    page = int(request.GET.get('page', 1))

    if length < settings.SEARCH_CHAR_MIN:
        messages.error(request,
                       "Enter more characters before preforming search.")
        return redirect(reverse('post_list'))

    results = search.preform_whoosh_search(query=query,
                                           page=page,
                                           sortedby=["lastedit_date"])

    #if not len(results):
    #    results = search.SearchResult()

    total = results.total
    template_name = "search/search_results.html"

    question_flag = Post.QUESTION
    context = dict(results=results,
                   query=query,
                   total=total,
                   template_name=template_name,
                   question_flag=question_flag,
                   stop_words=','.join(search.STOP))

    return render(request, template_name=template_name, context=context)