Skip to content

Commit

Permalink
search penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jul 31, 2023
1 parent fb26937 commit 7e79fe1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions peachjam/models/core_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,11 @@ def get_cited_work_frbr_uris(self):

return work_frbr_uris

def search_penalty(self):
"""Optionally provide a penalty for this document in search results. This cannot be zero or None."""
# provide a very small number instead of zero
return 0.0000001


def file_location(instance, filename):
if not instance.document.pk:
Expand Down
7 changes: 7 additions & 0 deletions peachjam/models/generic_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class Meta(CoreDocument.Meta):
def __str__(self):
return self.title

def search_penalty(self):
# non-principal (ie. amendment) works get a slight search penalty so that principal works
# tend to appear above them in search results
if self.metadata_json and self.metadata_json.get("principal", None) is False:
return 10.0
return super().search_penalty()

def apply_labels(self):
# label to indicate that this legislation is repealed
label, _ = Label.objects.get_or_create(
Expand Down
3 changes: 3 additions & 0 deletions peachjam_search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class SearchableDocument(Document):
nature_pt = fields.KeywordField()

ranking = RankField(attr="work.ranking")
# a negative boost to search results; this must be a positive number, but is treated as a penalty
# it is applied linearly, simply reducing the score by this amount
penalty = RankField(attr="search_penalty", positive_score_impact=False)

pages = fields.NestedField(
properties={
Expand Down
9 changes: 7 additions & 2 deletions peachjam_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ def construct_search(self, request, view, search_backend):
class RankFeatureBackend(BaseSearchQueryBackend):
@classmethod
def construct_search(cls, request, view, search_backend):
# apply penalty as a linear change to the score
# DISABLED until the penalty field is populated
# queries = [Q("rank_feature", field="penalty", boost=1.0, linear={})]
queries = []

if pj_settings().pagerank_boost_value:
rank = Q(
"rank_feature",
field="ranking",
boost=pj_settings().pagerank_boost_value,
)
return [rank]
return []
queries.append(rank)

return queries


class NestedPageQueryBackend(BaseSearchQueryBackend):
Expand Down

0 comments on commit 7e79fe1

Please sign in to comment.