Include external posts in similarity

This commit is contained in:
Jake Howard 2024-05-30 19:20:29 +01:00
parent 26d9e256ea
commit a7ec3a8f8e
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -93,28 +93,35 @@ class BlogPostPage(BaseContentPage):
return SingletonPageCache.get_url(BlogPostListPage) return SingletonPageCache.get_url(BlogPostListPage)
def get_similar_posts(self) -> models.QuerySet: def get_similar_posts(self) -> models.QuerySet:
try: listing_pages = BlogPostListPage.objects.get().get_listing_pages()
listing_pages = BlogPostPage.objects.filter(
id__in=BlogPostListPage.objects.get().get_listing_pages()
)
except BlogPostListPage.DoesNotExist:
return BlogPostPage.objects.none()
similar_posts = listing_pages.exclude(id=self.id).alias( similar_posts = listing_pages.exclude(id=self.id).alias(
title_similarity=TrigramSimilarity("title", self.title), title_similarity=TrigramSimilarity("title", self.title),
) )
page_tags = list(self.tags.public().live().values_list("id", flat=True)) page_tags = list(self.tags.public().live().values_list("id", flat=True))
similar_posts = similar_posts.alias(
# If this page has no tags, ignore it as part of similarity # If this page has no tags, ignore it as part of similarity
divisor = len(page_tags) if page_tags else models.Value(1)
similar_posts = similar_posts.alias(
# NB: Cast to a float, because `COUNT` returns a `bigint`. # NB: Cast to a float, because `COUNT` returns a `bigint`.
tag_similarity=Cast( _blog_tag_similarity=Cast(
models.Count("tags", filter=models.Q(tags__in=page_tags)), models.Count(
"blogpostpage__tags",
filter=models.Q(blogpostpage__tags__in=page_tags),
),
output_field=models.FloatField(), output_field=models.FloatField(),
) )
/ len(page_tags) / divisor,
if page_tags _external_tag_similarity=Cast(
else models.Value(1) models.Count(
"externalblogpostpage__tags",
filter=models.Q(externalblogpostpage__tags__in=page_tags),
),
output_field=models.FloatField(),
)
/ divisor,
tag_similarity=models.F("_blog_tag_similarity")
+ models.F("_external_tag_similarity"),
) )
similar_posts = similar_posts.annotate( similar_posts = similar_posts.annotate(
@ -148,19 +155,11 @@ class BlogPostTagPage(BaseListingPage):
def get_listing_pages(self) -> models.QuerySet: def get_listing_pages(self) -> models.QuerySet:
blog_list_page = BlogPostListPage.objects.get() blog_list_page = BlogPostListPage.objects.get()
listing_pages = blog_list_page.get_listing_pages() listing_pages = blog_list_page.get_listing_pages()
blog_post_tags = list(
BlogPostPage.objects.filter(id__in=listing_pages, tags=self).values_list(
"id", flat=True
)
)
external_post_tags = list(
ExternalBlogPostPage.objects.filter(
id__in=listing_pages, tags=self
).values_list("id", flat=True)
)
return listing_pages.filter( return listing_pages.filter(
id__in=blog_post_tags + external_post_tags models.Q(blogpostpage__tags=self)
).specific() | models.Q(externalblogpostpage__tags=self)
).distinct()
class BlogPostCollectionListPage(BaseListingPage): class BlogPostCollectionListPage(BaseListingPage):