Merge pull request #2924 from coreos-inc/search-opt

Optimize searching of repositories when there is no query
This commit is contained in:
josephschorr 2017-11-30 21:41:06 +02:00 committed by GitHub
commit 3b8feeba4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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 private repositories. Note that this method does *not* filter results based on visibility
to users. 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) # Always search at least on name (init clause)
clause = Repository.name.match(lookup_value) clause = Repository.name.match(lookup_value)
computed_score = RepositorySearchScore.score.alias('score') 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 clause = Repository.description.match(lookup_value) | clause
cases = [(Repository.name.match(lookup_value), 100 * RepositorySearchScore.score),] cases = [(Repository.name.match(lookup_value), 100 * RepositorySearchScore.score),]
computed_score = case(None, cases, RepositorySearchScore.score).alias('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) query = (Repository.select(Repository, Namespace, computed_score)
.join(Namespace, on=(Namespace.id == Repository.namespace_user)).where(clause) .join(Namespace, on=(Namespace.id == Repository.namespace_user)).where(clause)
.group_by(Repository.id, Namespace.id)) .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: if repo_kind is not None:
query = query.where(Repository.kind == Repository.kind.get_id(repo_kind)) 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: if not include_private:
query = query.where(Repository.visibility == _basequery.get_public_repo_visibility()) 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 return query