Optimize searching of repositories when there is no query

This commit is contained in:
Joseph Schorr 2017-11-30 14:10:22 -05:00
parent b69015f349
commit c767c88b82

View file

@ -505,11 +505,20 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p
private repositories. Note that this method does *not* filter results based on visibility
to users.
"""
if not lookup_value:
# This is a generic listing of repositories. Simply return the sorted repositories based
# on RepositorySearchScore.
query = (Repository
.select(Repository, Namespace, RepositorySearchScore.score)
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
.switch(Repository)
.join(RepositorySearchScore)
.order_by(RepositorySearchScore.score)
.desc())
else:
if search_fields is None:
search_fields = set([SEARCH_FIELDS.description.name, SEARCH_FIELDS.name.name])
if search_fields is None:
search_fields = set([SEARCH_FIELDS.description.name, SEARCH_FIELDS.name.name])
if lookup_value:
# Always search at least on name (init clause)
clause = Repository.name.match(lookup_value)
computed_score = RepositorySearchScore.score.alias('score')
@ -520,13 +529,13 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p
clause = Repository.description.match(lookup_value) | clause
cases = [(Repository.name.match(lookup_value), 100 * RepositorySearchScore.score),]
computed_score = case(None, cases, RepositorySearchScore.score).alias('score')
else:
clause = (Repository.id >= 0)
computed_score = RepositorySearchScore.score.alias('score')
query = (Repository.select(Repository, Namespace, computed_score)
.join(Namespace, on=(Namespace.id == Repository.namespace_user)).where(clause)
.group_by(Repository.id, Namespace.id))
query = (Repository.select(Repository, Namespace, computed_score)
.join(Namespace, on=(Namespace.id == Repository.namespace_user)).where(clause)
.group_by(Repository.id, Namespace.id))
query = (query.switch(Repository).join(RepositorySearchScore)
.group_by(Repository, Namespace, RepositorySearchScore).order_by(SQL('score').desc()))
if repo_kind is not None:
query = query.where(Repository.kind == Repository.kind.get_id(repo_kind))
@ -534,9 +543,6 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p
if not include_private:
query = query.where(Repository.visibility == _basequery.get_public_repo_visibility())
query = (query.switch(Repository).join(RepositorySearchScore)
.group_by(Repository, Namespace, RepositorySearchScore).order_by(SQL('score').desc()))
return query