Skip to content

Commit 6680006

Browse files
committed
Cleanup search display and logic.
1 parent 3b393b3 commit 6680006

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

readthedocs/search/faceted_search.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,25 @@
1313

1414
log = logging.getLogger(__name__)
1515

16+
ALL_FACETS = ['project', 'version', 'doc_type', 'language', 'index']
17+
1618

1719
class RTDFacetedSearch(FacetedSearch):
1820

1921
def __init__(self, user, **kwargs):
2022
self.user = user
2123
self.filter_by_user = kwargs.pop('filter_by_user', None)
24+
25+
# Set filters properly
2226
for facet in self.facets:
2327
if facet in kwargs:
2428
kwargs.setdefault('filters', {})[facet] = kwargs.pop(facet)
29+
30+
# Don't pass along unnecessary filters
31+
for f in ALL_FACETS:
32+
if f in kwargs:
33+
del kwargs[f]
34+
2535
super().__init__(**kwargs)
2636

2737
def search(self):
@@ -98,6 +108,7 @@ class PageSearch(SettingsOverrideObject):
98108

99109
"""
100110
Allow this class to be overridden based on CLASS_OVERRIDES setting.
111+
101112
This is primary used on the .com to adjust how we filter our search queries
102113
"""
103114

@@ -108,6 +119,7 @@ class ProjectSearch(SettingsOverrideObject):
108119

109120
"""
110121
Allow this class to be overridden based on CLASS_OVERRIDES setting.
122+
111123
This is primary used on the .com to adjust how we filter our search queries
112124
"""
113125

@@ -118,6 +130,7 @@ class DomainSearch(SettingsOverrideObject):
118130

119131
"""
120132
Allow this class to be overridden based on CLASS_OVERRIDES setting.
133+
121134
This is primary used on the .com to adjust how we filter our search queries
122135
"""
123136

readthedocs/search/views.py

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from readthedocs.search.faceted_search import (
1313
AllSearch, ProjectSearch, PageSearch, DomainSearch, ALL_FACETS
1414
)
15-
from readthedocs.search.utils import get_project_list_or_404
16-
1715

1816
log = logging.getLogger(__name__)
1917
LOG_TEMPLATE = '(Elastic Search) [{user}:{type}] [{project}:{version}:{language}] {msg}'
@@ -34,68 +32,62 @@
3432

3533

3634
def elastic_search(request, project_slug=None):
37-
"""Use Elasticsearch for global search."""
35+
"""
36+
Global user search on the dashboard
37+
38+
This is for both the main search and project search.
39+
40+
:param project_slug: Sent when the view is a project search
41+
"""
42+
43+
if project_slug:
44+
queryset = Project.objects.protected(request.user)
45+
project_obj = get_object_or_404(queryset, slug=project_slug)
46+
3847
user_input = UserInput(
3948
query=request.GET.get('q'),
40-
type=request.GET.get('type', 'all'),
49+
type=request.GET.get('type', 'project'),
4150
project=project_slug or request.GET.get('project'),
4251
version=request.GET.get('version', LATEST),
4352
taxonomy=request.GET.get('taxonomy'),
4453
language=request.GET.get('language'),
4554
doc_type=request.GET.get('doc_type'),
4655
index=request.GET.get('index'),
4756
)
57+
4858
results = ''
4959
facets = {}
5060

5161
if user_input.query:
5262
kwargs = {}
5363

54-
if user_input.project:
55-
projects_list = get_project_list_or_404(
56-
project_slug=user_input.project, user=request.user
57-
)
58-
project_slug_list = [project.slug for project in projects_list]
59-
kwargs['project'] = project_slug_list
60-
log.info('Filtering to project list: %s' % project_slug_list)
61-
62-
if user_input.version:
63-
kwargs['version'] = [user_input.version]
64-
65-
if user_input.doc_type:
66-
kwargs['doc_type'] = user_input.doc_type
67-
68-
if user_input.language:
69-
kwargs['language'] = user_input.language
70-
71-
if user_input.index:
72-
kwargs['index'] = user_input.index
64+
for avail_facet in ALL_FACETS:
65+
value = getattr(user_input, avail_facet, None)
66+
if value:
67+
kwargs[avail_facet] = value
7368

7469
if user_input.type == 'project':
75-
project_search = ProjectSearch(
70+
search = ProjectSearch(
7671
query=user_input.query, user=request.user, **kwargs
7772
)
78-
results = project_search.execute()
79-
facets = results.facets
73+
8074
elif user_input.type == 'domain':
81-
project_search = DomainSearch(
75+
search = DomainSearch(
8276
query=user_input.query, user=request.user, **kwargs
8377
)
84-
results = project_search.execute()
85-
facets = results.facets
86-
elif user_input.type == 'file':
8778

88-
page_search = PageSearch(
79+
elif user_input.type == 'file':
80+
search = PageSearch(
8981
query=user_input.query, user=request.user, **kwargs
9082
)
91-
results = page_search.execute()
92-
facets = results.facets
83+
9384
elif user_input.type == 'all':
94-
project_search = AllSearch(
85+
search = AllSearch(
9586
query=user_input.query, user=request.user, **kwargs
9687
)
97-
results = project_search.execute()
98-
facets = results.facets
88+
89+
results = search.execute()
90+
facets = results.facets
9991

10092
log.info(
10193
LOG_TEMPLATE.format(
@@ -135,9 +127,7 @@ def elastic_search(request, project_slug=None):
135127
})
136128

137129
if project_slug:
138-
queryset = Project.objects.protected(request.user)
139-
project = get_object_or_404(queryset, slug=project_slug)
140-
template_vars.update({'project_obj': project})
130+
template_vars.update({'project_obj': project_obj})
141131

142132
return render(
143133
request,

readthedocs/templates/search/elastic_search.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@
3030

3131
<div class="navigable">
3232
<ul>
33-
<!--
3433
<h5>{% trans 'Object Type' %}</h5>
35-
<li class="{% if type == 'all' %}active{% endif %}"><a href="?{% url_replace request 'type' 'all' %}">{% trans 'All' %}</a></li>
34+
{# <li class="{% if type == 'all' %}active{% endif %}"><a href="?{% url_replace request 'type' 'all' %}">{% trans 'All' %}</a></li> #}
3635
<li class="{% if type == 'project' %}active{% endif %}"><a href="?{% url_replace request 'type' 'project' %}">{% trans 'Projects' %}</a></li>
3736
<li class="{% if type == 'file' %}active{% endif %}"><a href="?{% url_replace request 'type' 'file' %}">{% trans 'Files' %}</a></li>
3837
<li class="{% if type == 'domain' %}active{% endif %}"><a href="?{% url_replace request 'type' 'domain' %}">{% trans 'Code API' %}</a></li>
3938

4039
<hr>
41-
-->
4240

4341
{% if facets.index %}
4442

0 commit comments

Comments
 (0)