Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using unaccent with SearchVector and SearchQuery in Django

I have installed UnaccentExtension in Django but I'm having problems using it with this search:

vector = SearchVector('title__unaccent', 'abstract__unaccent')
query = SearchQuery(word) | SearchQuery(word2)
files = Doc.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')

This is the error:

Cannot resolve keyword 'unaccent' into field. Join on 'title' not permitted.

Whit a simplest search it works fine:

Doc.objects.filter(title__unaccent=word)

So, what am I doing wrong?

like image 635
Alan Rivas Avatar asked Nov 10 '17 20:11

Alan Rivas


1 Answers

You can't use 'unaccent' in 'SearchVector' but you have to define a new "unaccented" config in PostgreSQL.

  1. If you missed, installs the unaccent extension.
  2. Create your unaccented dictionary in PostgrSQL or using an empty migrations with this SQL:

    CREATE TEXT SEARCH CONFIGURATION french_unaccent( COPY = french );
    ALTER TEXT SEARCH CONFIGURATION french_unaccent
    ALTER MAPPING FOR hword, hword_part, word
    WITH unaccent, french_stem;
    
  3. Use this configuration in your Django query :

    SearchVector('title','abstract', config='french_unaccent')
    SearchQuery(word, config='french_unaccent')
    

You can find more info about this type of configuration in the official PostgreSQL documentation on in various articles

like image 77
Paolo Melchiorre Avatar answered Nov 16 '22 02:11

Paolo Melchiorre