Skip to content

Search: exclude some fields from source results #7640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion readthedocs/search/faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class RTDFacetedSearch(FacetedSearch):

operators = []

# Sources to be excluded from results.
excludes = []

_highlight_options = {
'encoder': 'html',
'number_of_fragments': 1,
Expand Down Expand Up @@ -201,7 +204,7 @@ def query(self, search, query):
* Adds HTML encoding of results to avoid XSS issues.
"""
search = search.highlight_options(**self._highlight_options)
search = search.source(exclude=['content', 'headers'])
search = search.source(excludes=self.excludes)

queries = self._get_queries(
query=query,
Expand All @@ -220,6 +223,7 @@ class ProjectSearchBase(RTDFacetedSearch):
index = ProjectDocument._index._name
fields = ('name^10', 'slug^5', 'description')
operators = ['and', 'or']
excludes = ['users', 'language']


class PageSearchBase(RTDFacetedSearch):
Expand Down Expand Up @@ -248,6 +252,8 @@ class PageSearchBase(RTDFacetedSearch):
# the score of and should be higher as it satisfies both or and and
operators = ['and', 'or']

excludes = ['rank', 'sections', 'domains', 'commit', 'build']

def total_count(self):
"""Returns the total count of results of the current query."""
s = self.build_search()
Expand All @@ -261,6 +267,7 @@ def total_count(self):
def query(self, search, query):
"""Manipulates the query to support nested queries and a custom rank for pages."""
search = search.highlight_options(**self._highlight_options)
search = search.source(excludes=self.excludes)

queries = self._get_queries(
query=query,
Expand Down
1 change: 1 addition & 0 deletions readthedocs/search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ProjectSearchSerializer(serializers.Serializer):
name = serializers.CharField()
slug = serializers.CharField()
link = serializers.CharField(source='url')
description = serializers.CharField()
highlights = ProjectHighlightSerializer(source='meta.highlight', default=dict)


Expand Down