Switch repository search to order matches in repo names higher
Helps push better results to the top of the results list
This commit is contained in:
parent
481d9b2394
commit
0164b48a24
1 changed files with 11 additions and 3 deletions
|
@ -4,6 +4,7 @@ import random
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta, datetime
|
||||||
from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError
|
from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError
|
||||||
|
from playhouse.shortcuts import case
|
||||||
from cachetools import ttl_cache
|
from cachetools import ttl_cache
|
||||||
|
|
||||||
from data.model import (config, DataModelException, tag, db_transaction, storage, permission,
|
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)
|
# 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')
|
||||||
|
|
||||||
|
# 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:
|
if SEARCH_FIELDS.description.name in search_fields:
|
||||||
clause = Repository.description.match(lookup_value) | clause
|
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
|
query = (Repository
|
||||||
.select(Repository, Namespace)
|
.select(Repository, Namespace, computed_score)
|
||||||
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
||||||
.where(clause)
|
.where(clause)
|
||||||
.group_by(Repository.id, Namespace.id))
|
.group_by(Repository.id, Namespace.id))
|
||||||
|
@ -507,7 +515,7 @@ def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_p
|
||||||
.switch(Repository)
|
.switch(Repository)
|
||||||
.join(RepositorySearchScore)
|
.join(RepositorySearchScore)
|
||||||
.group_by(Repository, Namespace, RepositorySearchScore)
|
.group_by(Repository, Namespace, RepositorySearchScore)
|
||||||
.order_by(RepositorySearchScore.score.desc()))
|
.order_by(SQL('score').desc()))
|
||||||
|
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
Reference in a new issue