PATHS with filter in VIA


I want to return the subgraph starting at a given node. So I thought I would like to use PATHS ALL to get the entire subgraph, since this uses BFS (breadth-first-search) and is hopefully faster (lower latency) than having my own BFS in java.

Problem is I have a lot of properties that I want to traverse, so I’m just using a variable ?p. However there are a few properties I don’t want to traverse. So I thought I could maybe use a filter. Something along the lines of:

    PathQuery x = connection.paths("PATHS all START ?x = <"+resource+"> END ?y VIA {?p FILTER(?p != arkiv:parent)}");

You have to write out the graph pattern instead of using a variable:
VIA { ?x ?p ?y. FILTER(?p != arkiv:parent) }

Hi Håvard,

This approach may indeed work but you should be careful when using it on dense graphs. You’re trying to extract a subgraph by enumerating a potentially non-polynomial number of paths. It’s not hard to construct an example where a small subgraph unfolds into a very large number of highly overlapping paths. Your application can keep consuming paths without knowing if it’s going to see any new edges, or if all remaining paths will be just different combinations of previously seen edges.

We’re actually thinking of supporting the inverse of what you’re saying: computing the subgraph which would (compactly) represent all paths according to a given path expression. Then the client app can unwind it into as many paths as it can (or need to) consume. There’s no target date for this feature yet.


Hi Pavel and Jess.

I tested the full expression like you wrote and it worked.

AsPavel noted, this is actually slower than my own BFS, since the PATHS
approach prints out the same paths over and over again for deep and wide

In short, it would be great to have a “extract subgraph for subject"
feature where we could limit which predicates to explore. And also a
"replace subgraph for subject”. We have built both these features in java
for our own system, with BFS using SPARQL VALUES queries.

Thanks for the quick reply :slight_smile:


Yeah, we’re also about to start working on supporting custom DESCRIBE <iri> strategies which might be just what you want.


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.