Skip to content

Use attrgetter in sorted function #5936

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 3 commits into from
Jul 16, 2019
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
3 changes: 2 additions & 1 deletion readthedocs/search/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import logging
from operator import attrgetter
from pprint import pformat

from rest_framework import generics, serializers
Expand Down Expand Up @@ -54,7 +55,7 @@ def get_inner_hits(self, obj):
'_source': hit._source.to_dict(),
'highlight': self._get_inner_hits_highlights(hit),
}
for hit in sorted(all_results, key=utils._get_hit_score, reverse=True)
for hit in sorted(all_results, key=attrgetter('_score'), reverse=True)
]

return sorted_results
Expand Down
5 changes: 0 additions & 5 deletions readthedocs/search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ def _indexing_helper(html_objs_qs, wipe=False):
delete_objects_in_es.delay(**kwargs)


def _get_hit_score(res):
"""Returns the _score of a single ES search result hits."""
return res._score


def _remove_newlines_from_dict(highlight):
"""
Recursively change results to turn newlines into periods.
Expand Down
3 changes: 2 additions & 1 deletion readthedocs/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import collections
import itertools
import logging
from operator import attrgetter
from pprint import pformat

from django.shortcuts import get_object_or_404, render
Expand Down Expand Up @@ -129,7 +130,7 @@ def elastic_search(request, project_slug=None):
hit.highlight.to_dict()
),
}
for hit in sorted(all_results, key=utils._get_hit_score, reverse=True)
for hit in sorted(all_results, key=attrgetter('_score'), reverse=True)
]

result.meta.inner_hits = sorted_results
Expand Down