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 SearchVector
from blog.models import Post

Post.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 a SearchVectorField field for your models. You can read more about this at https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/search/#performance.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset