【问题标题】:Search with accents in Django with haystack and elasticsearch在 Django 中使用 haystack 和 elasticsearch 进行重音搜索
【发布时间】:2015-03-02 13:28:51
【问题描述】:

我要在我正在开发的网站上实现一个搜索引擎,我发现 haystack 似乎是最好的 Django 库,所以我使用 elasticsearch 实现它。

应用程序的语言是西班牙语,你们很多人都知道有很多带有口音的单词(á、é、í、ó、ú),如果用户键入“cancion”(不带重音)。

这是我的搜索视图(我正在使用 haystack 的自动完成功能和 jQuery typeahead 插件)

import json
from django.shortcuts import render
from django.http import HttpResponse
from haystack.query import SearchQuerySet


def autocomplete(request):
    sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('q', ''))[:5]
    suggestions = [{'title': result.title, 'url': result.object.get_absulute_url()} for result in sqs]
    # Make sure you return a JSON object, not a bare list.
    # Otherwise, you could be vulnerable to an XSS attack.
    the_data = json.dumps(suggestions)
    return HttpResponse(the_data, content_type='application/json')

我的设置没有什么特别的,就是正常的haystack配置。

【问题讨论】:

标签: python django search elasticsearch django-haystack


【解决方案1】:

Haystack supports and provides spelling suggestions for queries.

def autocomplete(request):
    sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('q', ''))[:5]
    if len(sqs) == 0:
        # if there are no results try with the spelling correction
        suggestion = sqs.spelling_suggestion()
        sqs = SearchQuerySet().autocomplete(content_auto=suggestion)[0:5]
    suggestions = [{'title': result.title, 'url': result.object.get_absolute_url()} for result in sqs]
    # Make sure you return a JSON object, not a bare list.
    # Otherwise, you could be vulnerable to an XSS attack.
    the_data = json.dumps(suggestions)
    return HttpResponse(the_data, content_type='application/json')

spelling_suggestions 应该能够识别cancióncancion 的相似性并返回一些结果

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多