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

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

Weighting queries

You can boost specific vectors so that more weight is attributed to them when ordering results by relevancy. For example, you can use this to give more relevance to posts that are matched by title rather than by content. Edit the previous lines of the views.py file of your blog application and make them look like this:

search_vector = SearchVector('title', weight='A') + SearchVector('body', weight='B')
search_query = SearchQuery(query)
results = Post.objects.annotate(
rank=SearchRank(search_vector, search_query)
).filter(rank__gte=0.3).order_by('-rank')

In the preceding code, we apply different weights to the search vectors built using the title and body fields. The default weights are D, C, B, and A that refer to the numbers 0.1, 0.2, 0.4, and 1.0, respectively. We apply a weight of 1.0 to the title search vector and a weight of 0.4 to the body vector: title matches will prevail over body content matches. We filter the results to display only the ones with a rank higher than 0.3.

主站蜘蛛池模板: 榆树市| 文安县| 罗甸县| 连山| 太谷县| 加查县| 华宁县| 光山县| 屯留县| 灵川县| 莱西市| 阳西县| 且末县| 堆龙德庆县| 萨嘎县| 江北区| 宁乡县| 延庆县| 定安县| 山西省| 乐昌市| 澜沧| 卫辉市| 屏东县| 桂东县| 沙河市| 泗水县| 山丹县| 白城市| 车致| 资溪县| 伽师县| 博野县| 瓮安县| 美姑县| 丰宁| 白城市| 佛学| 凌源市| 英德市| 卢氏县|