Use of full-text search + GIN in a view (Django 1.11 ) Use of full-text search + GIN in a view (Django 1.11 ) postgresql postgresql

Use of full-text search + GIN in a view (Django 1.11 )


Answering my own question:

products = (    Product.objects.annotate(rank=SearchRank(F("search_vector"), search_words))    .filter(search_vector=search_words)    .order_by("-rank"))


This means you should search your index field - in my case search_vector field.
Also I have changed my code a bit in ProductManager() class, so now I can just use

products = Product.objects.with_documents(search_words)

Where with_documents() is a custom function of custom ProductManager(). The recipe of this change is here (page 29).

What does all this code do:

  1. creates search_vector with scores to fields, field with bigger score - gets higher place in result sorting.
  2. creates GIN index for full-text search via ORM Django
  3. updates GIN index every time the instance of model is changed

    What this code dosn't do:
  4. It doesn't sort by relevance of substring which is queried. Possible solution.

    Hope this will help somebody with a bit complicated full-text search in Django.