简体   繁体   English

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

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

I've to implement a search engine on a website I'm working on, I discovered that haystack seems to be the best Django library for that, so I implemented it with elasticsearch. 我要在我正在研究的网站上实现一个搜索引擎,我发现haystack似乎是最好的Django库,所以我用elasticsearch实现了它。

The language of the application is Spanish, so many of you know that there are so many words with accents (á, é, í, ó, ú), and I need haystack to be able to find "canción" if the user types "cancion" (without accent). 该应用程序的语言是西班牙语,所以很多人都知道有很多单词带有重音符号(á,é,í,ó,ú),如果用户键入“我需要干草堆”才能找到“canción” cancion“(没有口音)。

Here is my search view (I'm using haystack's autocomplete feature with jQuery typeahead plugin) 这是我的搜索视图(我正在使用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')

There is nothing special in my settings but the normal configuration of haystack. 我的设置没有什么特别之处,但干草堆的正常配置。

Haystack supports and provides spelling suggestions for queries . 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 should be able to recognise the similarity in canción and cancion and return some results spelling_suggestions应该能够识别相似cancióncancion并返回一些成果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM