Skip to content

Commit

Permalink
fix: search counts behave the same as other counts; returns a string …
Browse files Browse the repository at this point in the history
…with the number of results or if at the limit=n+1 then ">n" as a string
  • Loading branch information
recalcitrantsupplant committed Aug 15, 2024
1 parent 428ca45 commit b838e57
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions prez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Settings(BaseSettings):
system_uri: Optional[str] = f"{protocol}://{host}:{port}"
order_lists_by_label: bool = True
listing_count_limit: int = 1000
search_count_limit: int = 10
label_predicates: Optional[List[URIRef]] = [
SKOS.prefLabel,
DCTERMS.title,
Expand Down
4 changes: 2 additions & 2 deletions prez/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ async def generate_search_query(request: Request):
escaped_term = escape_for_lucene_and_sparql(term)
predicates = request.query_params.getlist("predicates")
page = request.query_params.get("page", 1)
per_page = request.query_params.get("per_page", 10)
limit = int(per_page)
per_page = request.query_params.get("per_page")
limit = int(per_page) if per_page else settings.search_count_limit
offset = limit * (int(page) - 1)

return SearchQueryRegex(
Expand Down
7 changes: 6 additions & 1 deletion prez/services/listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sparql_grammar_pydantic import IRI, Var, TriplesSameSubject

from prez.cache import endpoints_graph_cache
from prez.config import settings
from prez.reference_data.prez_ns import PREZ, ALTREXT, ONT
from prez.renderers.renderer import return_from_graph
from prez.services.link_generation import add_prez_links
Expand Down Expand Up @@ -98,7 +99,11 @@ async def listing_function(
# count search results - hard to do in SPARQL as the SELECT part of the query is NOT aggregated
if search_query:
count = len(list(item_graph.subjects(RDF.type, PREZ.SearchResult)))
item_graph.add((PREZ.SearchResult, PREZ["count"], Literal(count)))
if count == settings.search_count_limit:
count_literal = f">{count-1}"
else:
count_literal = f"{count}"
item_graph.add((PREZ.SearchResult, PREZ["count"], Literal(count_literal)))
return await return_from_graph(
item_graph,
pmts.selected["mediatype"],
Expand Down
24 changes: 16 additions & 8 deletions prez/services/query_generation/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@


class SearchQueryRegex(ConstructQuery):
limit: int = 10 # specify here to make available as attribute
offset: int = 0 # specify here to make available as attribute

def __init__(
self,
term: str,
predicates: Optional[List[str]] = None,
limit: int = 10,
offset: int = 0,
limit: int = None,
offset: int = None,
):

limit += 1 # increase the limit by one so we know if there are further pages of results.

if not predicates:
predicates = settings.default_search_predicates

sr_uri: Var = Var(value="focus_node")
pred: Var = Var(value="pred")
match: Var = Var(value="match")
weight: Var = Var(value="weight")
hashid: Var = Var(value="hashID")

if not predicates:
predicates = settings.default_search_predicates

ct_map = {
IRI(value=PREZ.searchResultWeight): weight,
IRI(value=PREZ.searchResultPredicate): pred,
Expand Down Expand Up @@ -376,3 +376,11 @@ def order_by(self):
@property
def order_by_direction(self):
return "DESC"

@property
def limit(self):
return self.where_clause.group_graph_pattern.content.solution_modifier.limit_offset.limit_clause.limit

@property
def offset(self):
return self.where_clause.group_graph_pattern.content.solution_modifier.limit_offset.offset_clause.offset

0 comments on commit b838e57

Please sign in to comment.