Searching against multiple fields

You might want to search against multiple fields. In this case, you will need to define SearchVector. Let's build a vector that allows us to search against the title and body fields of the Post model:

from django.contrib.postgres.search import SearchVectorfrom blog.models import PostPost.objects.annotate(    search=SearchVector('title', 'body'),).filter(search='django')

Using annotate and defining SearchVector with both fields, we provide a functionality to match the query against both the title and body of the posts.

Full-text search is an intensive process. If you are searching for more than a few hundred rows, you should define a functional index that matches the search vector you are using. Django provides ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.