diff --git a/data/model/repository.py b/data/model/repository.py index c49e5cb05..3f6df779d 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -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