Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

#6 Improve rank of long documents #13

Open
wants to merge 4 commits into
base: 5-add-weights
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions gutensearch/views.py
Expand Up @@ -3,7 +3,7 @@
from django.contrib.postgres.search import SearchQuery, SearchRank
from django.core.exceptions import BadRequest
from django.db import connection
from django.db.models import F, QuerySet
from django.db.models import F, QuerySet, Value
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
Expand Down Expand Up @@ -59,7 +59,8 @@ def to_tsquery(search_term: str) -> str:

def documents_matching(search_term: str) -> QuerySet[Document]:
search_query = SearchQuery(search_term, search_type="raw")
search_rank = SearchRank(F("search_vector"), search_query)
# Normalization=1 divides the rank by 1 + the logarithm of the document length.
search_rank = SearchRank(F("search_vector"), search_query, normalization=Value(1))
return (
Document.objects.annotate(
rank=search_rank,
Expand Down