Skip to content

Commit

Permalink
working with e.g. http://localhost:8000/search?q=*yue*&predicates=rea…
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Nov 14, 2024
1 parent e4b1675 commit d65a593
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 35 deletions.
26 changes: 8 additions & 18 deletions prez/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ async def generate_search_query(
):
term = request.query_params.get("q")
if term:
escaped_term = escape_for_lucene_and_sparql(term)
# escaped_term = escape_for_lucene_and_sparql(term)
predicates = request.query_params.getlist("predicates")
page = request.query_params.get("page", 1)
limit = request.query_params.get("limit")
Expand All @@ -218,7 +218,7 @@ async def generate_search_query(

if settings.search_method == SearchMethod.DEFAULT:
return SearchQueryRegex(
term=escaped_term,
term=term,
predicates=predicates,
limit=limit,
offset=offset,
Expand All @@ -230,40 +230,30 @@ async def generate_search_query(
tssp_lists = []
tss_list = []
non_shacl_predicates = []
i = 0
i = 100
for pred in predicates:
if pred in shacl_shape_ids:
shacl_shape_uri = shacl_shapes.value(subject=None, predicate=DCTERMS.identifier, object=Literal(pred))
shacl_shape_g = shacl_shapes.cbd(shacl_shape_uri)
search_preds = list(shacl_shape_g.objects(subject=None, predicate=ONT.searchPredicate))
ps = PropertyShape(
uri=shacl_shape_uri,
graph=shacl_shape_g,
kind="fts",
focus_node=Var(value="focus_node"),
shape_number=i
)
tssp_lists.append(ps.tssp_list)
tssp_lists.append((ps.tssp_list, search_preds))
tss_list.extend(ps.tss_list)
i += 1
else:
non_shacl_predicates.append(pred)

ggp_list = []
for inner_list in tssp_lists:
ggp_list.append(
GroupGraphPattern(
content=GroupGraphPatternSub(
triples_block=TriplesBlock.from_tssp_list(inner_list)
)
)
)
gougp = GroupOrUnionGraphPattern(group_graph_patterns=ggp_list)

return SearchQueryFusekiFTS(
term=escaped_term,
term=term,
non_shacl_predicates=non_shacl_predicates,
shacl_gougp=gougp,
shacl_tss_list=tss_list,
shacl_tssp_preds=tssp_lists,
tss_list=tss_list,
limit=limit,
offset=offset
)
Expand Down
146 changes: 132 additions & 14 deletions prez/services/query_generation/search_fuseki_fts.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def __init__(
limit: int,
offset: int,
non_shacl_predicates: list[str] | None = None,
shacl_gougp: GroupOrUnionGraphPattern | None = None,
shacl_tss_list: list[TriplesSameSubject] | None = None
shacl_tssp_preds: list[tuple[list[TriplesSameSubjectPath], list[str]]] | None = None,
tss_list: list[TriplesSameSubjectPath] | None = None
):
limit += 1 # increase the limit by one, so we know if there are further pages of results.
# join search terms with '+' for better results
Expand Down Expand Up @@ -126,20 +126,15 @@ def __init__(
for p, v in ct_map.items()
]

if shacl_tss_list:
construct_tss_list.extend(shacl_tss_list)
if tss_list:
construct_tss_list.extend(tss_list)

construct_template = ConstructTemplate(
construct_triples=ConstructTriples.from_tss_list(construct_tss_list)
)

gpnt_or_triples_list = []
if shacl_gougp:
gpnt = GraphPatternNotTriples(
content=shacl_gougp
)
gpnt_or_triples_list.append(gpnt)
gpnt_or_triples_list.extend([TriplesBlock(
def _generate_fts_triples_block(preds: list[str], sr_uri: Var = sr_uri) -> TriplesBlock:
return TriplesBlock(
triples=TriplesSameSubjectPath(
content=(
TriplesNodePath(
Expand Down Expand Up @@ -212,7 +207,7 @@ def __init__(
)
)
for predicate in
non_shacl_predicates
preds
]
+ [
GraphNodePath(
Expand All @@ -236,7 +231,130 @@ def __init__(
),
)
)
)])
)

ggp_list = []
if non_shacl_predicates:
direct_preds_tb = _generate_fts_triples_block(non_shacl_predicates)
direct_preds_ggp = GroupGraphPattern(
content=GroupGraphPatternSub(
triples_block=direct_preds_tb
)
)
ggp_list.append(direct_preds_ggp)
for tssp_list, preds in shacl_tssp_preds:
path_preds_tb = _generate_fts_triples_block(preds, Var(value="fts_search_node"))
path_preds_tb.triples_block = TriplesBlock.from_tssp_list(tssp_list)
path_preds_ggp = GroupGraphPattern(
content=GroupGraphPatternSub(
triples_block=path_preds_tb
)
)
ggp_list.append(path_preds_ggp)
gpnt = GraphPatternNotTriples(
content=GroupOrUnionGraphPattern(
group_graph_patterns=ggp_list
)
)

# gpnt_or_triples_list.extend([TriplesBlock(
# triples=TriplesSameSubjectPath(
# content=(
# TriplesNodePath(
# coll_path_or_bnpl_path=CollectionPath(
# graphnodepath_list=[
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=sr_uri
# )
# ),
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=weight
# )
# ),
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=match
# )
# ),
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=g
# )
# ),
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=pred
# )
# ),
# ]
# )
# ),
# PropertyListPath(
# plpne=PropertyListPathNotEmpty(
# first_pair=(
# VerbPath(
# path=SG_Path(
# path_alternative=PathAlternative(
# sequence_paths=[
# PathSequence(
# list_path_elt_or_inverse=[
# PathEltOrInverse(
# path_elt=PathElt(
# path_primary=PathPrimary(
# value=text_query
# )
# )
# )
# ]
# )
# ]
# )
# )
# ),
# ObjectListPath(
# object_paths=[
# ObjectPath(
# graph_node_path=GraphNodePath(
# varorterm_or_triplesnodepath=TriplesNodePath(
# coll_path_or_bnpl_path=CollectionPath(
# graphnodepath_list=[
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=GraphTerm(
# content=IRI(
# value=predicate
# )
# )
# )
# )
# for predicate in
# non_shacl_predicates
# ]
# + [
# GraphNodePath(
# varorterm_or_triplesnodepath=VarOrTerm(
# varorterm=GraphTerm(
# content=RDFLiteral(
# value=term
# )
# )
# )
# )
# ]
# )
# )
# )
# )
# ]
# ),
# )
# )
# ),
# )
# )
# )])
where_clause = WhereClause(
group_graph_pattern=GroupGraphPattern(
content=SubSelect(
Expand Down Expand Up @@ -304,7 +422,7 @@ def __init__(
where_clause=WhereClause(
group_graph_pattern=GroupGraphPattern(
content=GroupGraphPatternSub(
graph_patterns_or_triples_blocks=gpnt_or_triples_list
graph_patterns_or_triples_blocks=[gpnt]
)
)
),
Expand Down
4 changes: 2 additions & 2 deletions test_data/fts_property_shapes.ttl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@prefix schema: <http://schema.org/> .
@prefix sdo: <https://schema.org/> .
@prefix ont: <https://prez.dev/ont/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix dwc: <http://rs.tdwg.org/dwc/terms/> .
Expand Down Expand Up @@ -43,5 +43,5 @@ ex:RealExample
sh:path ( sosa:isFeatureOfInterestOf sosa:hasResult ) ;
sh:name "Real Example" ;
dcterms:identifier "real" ;
ont:searchPredicate schema:description ;
ont:searchPredicate sdo:description ;
.
4 changes: 3 additions & 1 deletion test_data/obs.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
@prefix schema: <https://schema.org/> .
@prefix sosa: <http://www.w3.org/ns/sosa/> .

<https://anufncatalogue.anu.edu.au/david-different-uri> sosa:isFeatureOfInterestOf _:Ne7fb721e13d643e4a6d5b9cd2a06cfea .
<https://anufncatalogue.anu.edu.au/david-different-uri> sosa:isFeatureOfInterestOf _:Ne7fb721e13d643e4a6d5b9cd2a06cfea ;
a <https://david/Class> ;
.

_:Ne7fb721e13d643e4a6d5b9cd2a06cfea a sosa:Observation ;
sosa:hasResult _:Nf0b4feb4ba064a39b6bd373d65a22abc ;
Expand Down

0 comments on commit d65a593

Please sign in to comment.