From 0164b48a24a8afb7517958f1184f55ad53142620 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 3 May 2017 17:02:24 -0400 Subject: [PATCH] Switch repository search to order matches in repo names higher Helps push better results to the top of the results list --- data/model/repository.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/data/model/repository.py b/data/model/repository.py index c2f65bdfe..1b113fe42 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -4,6 +4,7 @@ import random from enum import Enum from datetime import timedelta, datetime from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError +from playhouse.shortcuts import case from cachetools import ttl_cache from data.model import (config, DataModelException, tag, db_transaction, storage, permission, @@ -485,14 +486,21 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p # Always search at least on name (init clause) clause = Repository.name.match(lookup_value) + computed_score = RepositorySearchScore.score.alias('score') + # If the description field is in the search fields, then we need to compute a synthetic score + # to discount the weight of the description more than the name. if SEARCH_FIELDS.description.name in search_fields: clause = Repository.description.match(lookup_value) | clause - last_week = datetime.now() - timedelta(weeks=1) + cases = [ + (Repository.name.match(lookup_value), 100 * RepositorySearchScore.score), + ] + + computed_score = case(None, cases, RepositorySearchScore.score).alias('score') query = (Repository - .select(Repository, Namespace) + .select(Repository, Namespace, computed_score) .join(Namespace, on=(Namespace.id == Repository.namespace_user)) .where(clause) .group_by(Repository.id, Namespace.id)) @@ -507,7 +515,7 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p .switch(Repository) .join(RepositorySearchScore) .group_by(Repository, Namespace, RepositorySearchScore) - .order_by(RepositorySearchScore.score.desc())) + .order_by(SQL('score').desc())) return query