Verified Commit 26f0b014 authored by Kevin Morris's avatar Kevin Morris
Browse files

fix: /packages search by keywords

Signed-off-by: Kevin Morris's avatarKevin Morris <>
parent 83f5d9e4
Pipeline #15635 waiting for manual action with stages
in 6 minutes and 1 second
from typing import Set
from sqlalchemy import and_, case, or_, orm
from aurweb import db, models
......@@ -59,17 +61,24 @@ class PackageSearch:
"l": self._sort_by_last_modified
self._joined = False
self._joined_user = False
self._joined_keywords = False
def _join_user(self, outer: bool = True) -> orm.Query:
""" Centralized joining of a package base's maintainer. """
if not self._joined:
if not self._joined_user:
self.query = self.query.join(
User.ID == PackageBase.MaintainerUID,
self._joined = True
self._joined_user = True
return self.query
def _join_keywords(self) -> orm.Query:
if not self._joined_keywords:
self.query = self.query.join(PackageKeyword)
self._joined_keywords = True
return self.query
def _search_by_namedesc(self, keywords: str) -> orm.Query:
......@@ -100,11 +109,12 @@ class PackageSearch:
self.query = self.query.filter(PackageBase.Name == keywords)
return self
def _search_by_keywords(self, keywords: str) -> orm.Query:
def _search_by_keywords(self, keywords: Set[str]) -> orm.Query:
self.query = self.query.join(PackageKeyword).filter(
PackageKeyword.Keyword == keywords
self.query = self.query.filter(
return self
def _search_by_maintainer(self, keywords: str) -> orm.Query:
......@@ -52,9 +52,14 @@ async def packages_get(request: Request, context: Dict[str, Any],
# This means that for any sentences separated by spaces,
# they are used as if they were ANDed.
keywords = context["K"] = request.query_params.get("K", str())
keywords = keywords.split(" ")
for keyword in keywords:
search.search_by(search_by, keyword)
if search_by == "k":
# If we're searchin by keywords, supply a set of keywords.
search.search_by(search_by, set(keywords))
for keyword in keywords:
search.search_by(search_by, keyword)
# Collect search result count here; we've applied our keywords.
# Including more query operations below, like ordering, will
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment