Skip to content

Refactor search code #5197

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 52 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
47e3c1a
Reactor search code
ericholscher Jan 29, 2019
a84bb15
Show more results in JS search results
ericholscher Jan 29, 2019
6f36acd
Fix docstring
ericholscher Jan 29, 2019
0bfa998
Fix lint
ericholscher Jan 29, 2019
936465d
Fix tests
ericholscher Jan 29, 2019
0496cf6
Small fixes for search
ericholscher Jan 29, 2019
ab28491
Fix comment.
ericholscher Jan 29, 2019
617f47f
Move to pformat and real logging.
ericholscher Jan 29, 2019
d0f6082
Standardize search result listings
ericholscher Jan 29, 2019
af8a28f
Only request 3 fragments, as that’s all we display
ericholscher Jan 29, 2019
5f197ec
Fix pytest test logic.
ericholscher Jan 29, 2019
465628e
Handle changing newlines to periods.
ericholscher Jan 30, 2019
e3e245c
Fix tests.
ericholscher Jan 30, 2019
e909759
Check for content to highlight
ericholscher Jan 30, 2019
49098d7
Attempt to fix tests agian.
ericholscher Jan 30, 2019
07dc26b
Delete some old code, and remove single function/class files.
ericholscher Jan 30, 2019
e9f3f3f
Fix lint error.
ericholscher Jan 30, 2019
d6d02da
Keep all search views in the search app
ericholscher Jan 30, 2019
336e674
remove need to pass around an index_name
ericholscher Jan 30, 2019
eb65817
Properly filter project search
ericholscher Jan 30, 2019
144ed0e
Refactor the Document and FacetedSearch classes
ericholscher Jan 31, 2019
d314f58
Merge remote-tracking branch 'origin/master' into readd-search-signals
ericholscher Jan 31, 2019
03ad482
Fix lint
ericholscher Jan 31, 2019
5dfc17a
Add TODO for signal handling
ericholscher Feb 4, 2019
fcc9620
Use old value for ELASTICSEARCH_DSL_AUTOSYNC in tests
ericholscher Feb 4, 2019
701ecdb
Update JS file
ericholscher Feb 5, 2019
f636737
Add logging to our search JS to debug
ericholscher Feb 5, 2019
7583f9a
Make log a bit better
ericholscher Feb 5, 2019
747da90
Show the actual result length instead of the API count, since it’s wr…
ericholscher Feb 5, 2019
6c317f1
Update bundle with this change.
ericholscher Feb 5, 2019
e2655de
Remove use of signals in favor of SettingsOverrideObject
ericholscher Feb 5, 2019
f457ff7
Simpler super's
ericholscher Feb 5, 2019
7b2013c
Docstring for some methods/functions and small linting
humitos Feb 5, 2019
0741a25
Fix search lint
ericholscher Feb 5, 2019
85956e0
Comment the proper place
ericholscher Feb 5, 2019
6ac2f35
Merge branch 'readd-search-signals' of github.com:rtfd/readthedocs.or…
humitos Feb 5, 2019
e062191
Merge remote-tracking branch 'origin/master' into readd-search-signals
ericholscher Feb 5, 2019
8e4cc2b
Nicer highlight replacement syntax
ericholscher Feb 5, 2019
8c7bda4
Remove search debug logging.
ericholscher Feb 5, 2019
5b9f460
Use normal user object everywhere.
ericholscher Feb 5, 2019
e2e271b
Merge remote-tracking branch 'origin/readd-search-signals' into readd…
ericholscher Feb 5, 2019
a993f08
Fix typo
ericholscher Feb 5, 2019
d52b968
Use classic JS loop
ericholscher Feb 5, 2019
fc277fa
Update docs
ericholscher Feb 5, 2019
417ea45
Cap operators
ericholscher Feb 5, 2019
80c58c7
Fix lint again
ericholscher Feb 5, 2019
72d867f
Once more with the linting
ericholscher Feb 5, 2019
5f118ff
Change API queryset filter to `public(user)`
ericholscher Feb 6, 2019
e263e69
Small doc fixup
ericholscher Feb 6, 2019
1a3e146
Support filter_user argument for not filtering users in corporate sea…
ericholscher Feb 6, 2019
fab9f42
Address review feedback
ericholscher Feb 6, 2019
0a06726
More cleanup.
ericholscher Feb 6, 2019
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
4 changes: 2 additions & 2 deletions docs/development/search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Architecture
------------
The search architecture is devided into 2 parts.

* One part is responsible for **indexing** the documents and projects (`documents.py`)
* The other part is responsible for **querying** the Index to show the proper results to users (`faceted_search.py`)
* One part is responsible for **indexing** the documents and projects (``documents.py``)
* The other part is responsible for **querying** the Index to show the proper results to users (``faceted_search.py``)

We use the `django-elasticsearch-dsl`_ package for our Document abstraction.
`django-elasticsearch-dsl`_ is a wrapper around `elasticsearch-dsl`_ for easy configuration
Expand Down
7 changes: 5 additions & 2 deletions readthedocs/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_queryset(self):
# Validate all the required params are there
self.validate_query_params()
query = self.request.query_params.get('q', '')
kwargs = {}
kwargs = {'filter_user': False}
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I'd like to come up with a better name for this. I'm thinking about filter_by_user which is a little more explicit.

What we want to communicate here is "filter versions by users permissions" I suppose, but I didn't find a good name for that :(

kwargs['projects_list'] = [p.slug for p in self.get_all_projects()]
kwargs['versions_list'] = self.request.query_params.get('version')
user = self.request.user
Expand Down Expand Up @@ -107,7 +107,10 @@ def get_all_projects(self):
:raises: Http404 if project is not found
"""
project_slug = self.request.query_params.get('project')
all_projects = get_project_list_or_404(project_slug=project_slug, user=self.request.user)
version_slug = self.request.query_params.get('version')
all_projects = get_project_list_or_404(
project_slug=project_slug, user=self.request.user, version_slug=version_slug,
)
return all_projects

def get_all_projects_url(self):
Expand Down
3 changes: 2 additions & 1 deletion readthedocs/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class Meta(object):
ignore_signals = True

@classmethod
def faceted_search(cls, query, user, projects_list=None, versions_list=None):
def faceted_search(cls, query, user, projects_list=None, versions_list=None, filter_user=True):
from readthedocs.search.faceted_search import PageSearch
kwargs = {
'user': user,
'query': query,
'filter_user': filter_user,
}

filters = {}
Expand Down
14 changes: 3 additions & 11 deletions readthedocs/search/faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,10 @@ def __init__(self, user, **kwargs):
but is used on the .com
"""
self.user = user
if 'filter_user' in kwargs:
self.filter_user = kwargs.pop('filter_user')
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: this can be written as

self.filter_user = kwargs.pop('filter_user', None)

to avoid the if.

super().__init__(**kwargs)

def search(self):
"""
Filter out full content on search results.

This was causing all of the indexed content to be returned, which was
never used on the client side.
"""
s = super().search()
s = s.source(exclude=['content', 'headers'])
return s

def query(self, search, query):
"""
Add query part to ``search`` when needed.
Expand All @@ -42,6 +33,7 @@ def query(self, search, query):
"""
search = super().query(search, query)
search = search.highlight_options(encoder='html', number_of_fragments=3)
search = search.source(exclude=['content', 'headers'])
return search


Expand Down
21 changes: 14 additions & 7 deletions readthedocs/search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.shortcuts import get_object_or_404
from django_elasticsearch_dsl.registries import registry

from readthedocs.builds.models import Version
from readthedocs.projects.models import Project


Expand All @@ -15,14 +16,20 @@

# TODO: Rewrite all the views using this in Class Based View,
# and move this function to a mixin
def get_project_list_or_404(project_slug, user):
"""Return list of project and its subprojects."""
queryset = Project.objects.api(user).only('slug')

project = get_object_or_404(queryset, slug=project_slug)
subprojects = queryset.filter(superprojects__parent_id=project.id)
def get_project_list_or_404(project_slug, user, version_slug=None):
"""
Return list of project and its subprojects.

project_list = list(subprojects) + [project]
It filters by Version privacy instead of Project privacy.
"""
# Support private projects with public versions
project_list = []
main_project = get_object_or_404(Project.objects.all(), slug=project_slug)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: no need to .objects.all(), just get_object_or_404(Project, slug=project_slug) works

subprojects = Project.objects.filter(superprojects__parent_id=main_project.id)
for project in list(subprojects) + [main_project]:
Copy link
Member

Choose a reason for hiding this comment

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

Not sure, but I'm thinking that this for could be replaced by a query itself, like:

versions = Version.objects.public(user)
    .filter(project__in=projects, slug=version_slug)
    .values_list('id', flat=True)

projects = Project.objects.filter(versions__id__in=versions)

Use it if you consider it clearer.

version = Version.objects.public(user).filter(project__slug=project.slug, slug=version_slug)
if version.count():
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: .exists() is better for this use case.

project_list.append(version[0].project)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I think using .first() is the Django-way for this.

return project_list


Expand Down
8 changes: 2 additions & 6 deletions readthedocs/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ def elastic_search(request):
elif user_input.type == 'file':
kwargs = {}
if user_input.project:
projects_list = get_project_list_or_404(
project_slug=user_input.project, user=request.user
)
project_slug_list = [project.slug for project in projects_list]
kwargs['projects_list'] = project_slug_list
kwargs['projects_list'] = [user_input.project]
if user_input.version:
kwargs['versions_list'] = user_input.version
kwargs['versions_list'] = [user_input.version]

page_search = PageDocument.faceted_search(
query=user_input.query, user=request.user, **kwargs
Expand Down