Skip to content

Commit bde3caf

Browse files
committed
Search: exclude some fields from source results
This should hopefully make search a little faster, we don't make use of these fields in the serializer. The big ones are sections and domains, we will be fetching all of them per each page, instead of just the ones that matched. Also fixed a bug, we were omitting the description from the project in the serializer.
1 parent 892db33 commit bde3caf

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

readthedocs/search/faceted_search.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class RTDFacetedSearch(FacetedSearch):
2828

2929
operators = []
3030

31+
# Sources to be excluded from results.
32+
excludes = []
33+
3134
_highlight_options = {
3235
'encoder': 'html',
3336
'number_of_fragments': 1,
@@ -201,7 +204,7 @@ def query(self, search, query):
201204
* Adds HTML encoding of results to avoid XSS issues.
202205
"""
203206
search = search.highlight_options(**self._highlight_options)
204-
search = search.source(exclude=['content', 'headers'])
207+
search = search.source(excludes=self.excludes)
205208

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

224228

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

255+
excludes = ['rank', 'sections', 'domains', 'commit', 'build']
256+
251257
def total_count(self):
252258
"""Returns the total count of results of the current query."""
253259
s = self.build_search()
@@ -261,6 +267,7 @@ def total_count(self):
261267
def query(self, search, query):
262268
"""Manipulates the query to support nested queries and a custom rank for pages."""
263269
search = search.highlight_options(**self._highlight_options)
270+
search = search.source(excludes=self.excludes)
264271

265272
queries = self._get_queries(
266273
query=query,

readthedocs/search/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ProjectSearchSerializer(serializers.Serializer):
3737
name = serializers.CharField()
3838
slug = serializers.CharField()
3939
link = serializers.CharField(source='url')
40+
description = serializers.CharField()
4041
highlights = ProjectHighlightSerializer(source='meta.highlight', default=dict)
4142

4243

0 commit comments

Comments
 (0)