官术网_书友最值得收藏!

  • Django 2 by Example
  • Antonio Melé
  • 129字
  • 2021-06-25 21:19:07

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.

主站蜘蛛池模板: 卢龙县| 乳山市| 邵阳县| 灵璧县| 永寿县| 平乡县| 丹巴县| 乐业县| 昌宁县| 卓尼县| 松原市| 浙江省| 浦江县| 灵寿县| 蕲春县| 涟水县| 丹东市| 嘉兴市| 凯里市| 黑水县| 海城市| 湘潭市| 集安市| 黑水县| 金山区| 郸城县| 开化县| 通山县| 威海市| 新竹县| 会同县| 通城县| 清涧县| 淮安市| 洪洞县| 长乐市| 富蕴县| 西宁市| 武陟县| 普宁市| 宁津县|