Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Search counts behave the same as listing counts #252

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == search_query.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,
limit: int,
offset: int,
predicates: Optional[List[str]] = None,
limit: int = 10,
offset: int = 0,
):

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
2 changes: 1 addition & 1 deletion tests/test_query_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_basic_listing():


def test_search_query_regex():
sq = SearchQueryRegex(term="test", predicates=[RDFS.label])
sq = SearchQueryRegex(term="test", predicates=[RDFS.label], limit=10, offset=0)
test = PrezQueryConstructor(
profile_triples=[
TriplesSameSubjectPath.from_spo(
Expand Down
Loading