簡體   English   中英

使用haystack和elasticsearch在Django中使用重音進行搜索

[英]Search with accents in Django with haystack and elasticsearch

我要在我正在研究的網站上實現一個搜索引擎,我發現haystack似乎是最好的Django庫,所以我用elasticsearch實現了它。

該應用程序的語言是西班牙語,所以很多人都知道有很多單詞帶有重音符號(á,é,í,ó,ú),如果用戶鍵入“我需要干草堆”才能找到“canción” 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支持並提供查詢的拼寫建議

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並返回一些成果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM