- 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.
推薦閱讀
- 從區(qū)塊鏈到Web3:構(gòu)建未來(lái)互聯(lián)網(wǎng)生態(tài)
- Spring Boot 2.0 Projects
- Django 2 by Example
- 社交電商運(yùn)營(yíng)策略、技巧與實(shí)操
- 農(nóng)產(chǎn)品物聯(lián)網(wǎng)研究與應(yīng)用
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來(lái)
- 正在爆發(fā)的互聯(lián)網(wǎng)革命
- Socket.IO Real-time Web Application Development
- Bonita Open Solution 5.x Essentials
- 網(wǎng)絡(luò)工程實(shí)施技術(shù)與方案大全
- 網(wǎng)絡(luò)空間全球治理觀察
- TCP/IP基礎(chǔ)(第2版)
- 數(shù)字王國(guó)里的虛擬人:技術(shù)、商業(yè)與法律解讀
- 趣話通信:6G的前世、今生和未來(lái)
- 視聽(tīng)變革:廣電的新媒體戰(zhàn)略