简体   繁体   中英

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.

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).

Here is my search view (I'm using haystack's autocomplete feature with jQuery typeahead plugin)

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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