Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 20 additions & 32 deletions readthedocs/search/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import re
from functools import namedtuple
from operator import attrgetter
from urllib.parse import urlparse

from rest_framework import serializers
Expand Down Expand Up @@ -55,6 +54,25 @@ def get_title(self, obj):
return list(getattr(obj, "title", []))


class SectionHighlightSerializer(serializers.Serializer):
title = serializers.SerializerMethodField()
content = serializers.SerializerMethodField()

def get_title(self, obj):
return list(getattr(obj, "sections.title", []))

def get_content(self, obj):
return list(getattr(obj, "sections.content", []))


class SectionSearchSerializer(serializers.Serializer):
type = serializers.CharField(default="section", source=None, read_only=True)
id = serializers.CharField()
title = serializers.CharField()
content = serializers.CharField()
highlights = SectionHighlightSerializer(source="meta.highlight", default=dict)
Comment on lines +57 to +73
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were just moved, as now SectionSearchSerializer needs to be defined first



class PageSearchSerializer(serializers.Serializer):
"""
Page serializer.
Expand All @@ -74,7 +92,7 @@ class PageSearchSerializer(serializers.Serializer):
path = serializers.SerializerMethodField()
domain = serializers.SerializerMethodField()
highlights = PageHighlightSerializer(source="meta.highlight", default=dict)
blocks = serializers.SerializerMethodField()
blocks = SectionSearchSerializer(source="meta.inner_hits.sections", many=True, default=list)

def __init__(self, *args, projects=None, **kwargs):
if projects:
Expand Down Expand Up @@ -152,33 +170,3 @@ def _get_full_path(self, obj):

return docs_url.rstrip("/") + "/" + path.lstrip("/")
return None

def get_blocks(self, obj):
"""Combine and sort inner results (domains and sections)."""
sections = obj.meta.inner_hits.sections or []
sorted_results = sorted(
sections,
key=attrgetter("meta.score"),
reverse=True,
)
sorted_results = [SectionSearchSerializer(hit).data for hit in sorted_results]
return sorted_results


class SectionHighlightSerializer(serializers.Serializer):
title = serializers.SerializerMethodField()
content = serializers.SerializerMethodField()

def get_title(self, obj):
return list(getattr(obj, "sections.title", []))

def get_content(self, obj):
return list(getattr(obj, "sections.content", []))


class SectionSearchSerializer(serializers.Serializer):
type = serializers.CharField(default="section", source=None, read_only=True)
id = serializers.CharField()
title = serializers.CharField()
content = serializers.CharField()
highlights = SectionHighlightSerializer(source="meta.highlight", default=dict)