Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

How to work with sub-graphs

Node Clone

Hey guys

I'm currently evaluating neo4j for our hand-knitted graph-like legacy persistence framework, which became too slow for some new use cases

The main requirement is to store temporal master data and query this data for a specific date in the sense of "What did the world look like at that given date", which, as far as I can imagine, corresponds to a query in a sub-graph. The connection may sound a bit poor, but in our current implementation each node and relationship has at least 2 attributes that indicate the start and end dates of its validity. So in this sub-graph query we would only consider nodes and relationships whose start and end attributes form an interval where the query parameter lays in between. That sound reasonable?

I can easily construct a cypher query for a particular use case where the above constraints apply. I even managed to write a procedure that yields paths from a given start node where all following nodes and relationships satisfy the temporal constraints.
But now I'm stuck..! 'Cause my aim is to build an easy to use interface where the client software has no need to know how the temporal aspects affect the final query.

I found that stuff like

MATCH path=(p:Person) -[:WORKS_FOR]-> (c:Company)
WHERE ALL(node IN nodes(path) WHERE node IN subGraph) AND ALL(rel IN relationships(path) WHERE rel IN subGraph)

but with this in hand, a user can not simply write queries like

MATCH (p:Person) -[:WORKS_FOR]-> (c:Company)

because he would have to know about my sub-graph solution. The whole temporal stuff would have to be addable like a parenthesis.
You see the point?

Maybe I am on the wrong track and you see a completely different solution. In any case it would be nice to hear one or the other thought about it.



Hello Matt,

Welcome to the community!

In your environment, I would recommend that you write the user interface in another language for which we have drivers. Then you simply plug in the values entered by the user into the Cypher query.

Here is some information about the drivers supported by Neo4j:


Hi Elaine

Thanks for your respone and welcome.

I think the point with using a driver makes sense. Yet, I'm not completely sure, if

simply plug in the values entered by the user

works as desired. Consider the example I provided above. The query, a user want to write, may have a simple structure like this:

MATCH (p:Person) -[:WORKS_FOR]-> (c:Company)

But I would have a template-like query like (pseudocode)

<fetching the subgraph>
MATCH path=... <user entered stuff>
WHERE ALL ... IN subGraph ...
<user entered stuff>

So in my opinion, the values the user entered have to be splitted somehow. And that's something I most certainly don't want to do

Pleasy correct me, if I missed your point. I am grateful for any help

Hello Matt,

When using Neo4j with a driver, you typically collect the filters (multiple criteria) from the user, then you use the collected filters to create the MATCH clause that will be used for the retrieval.


Ok, I see what you mean...sure that would be a possibility.

But the thing is: I want to give the user the possibility to define his own Cypher queries, which are then applied to the subgraph.

Well if the user is going to define their own Cypher queries, then they would simply use Neo4j Browser right?

You could set them up with the pre-written queries using browser sync in the cloud and they could bring them up in the query pane and modify them.


Ok, I try to clarify my question.

I'm talking about a software client, not an actual user entering queries. So a developer should be able to write his cypher queries resulting from his usecases and hand them over to a wrapper who takes care of all the temporal stuff.

Sorry if I was imprecise.

If developers are going to compose Cypher queries as strings that are sent via the driver, then it is up to the developer to understand proper Cypher syntax and how to create the query string from user input.

You can specify parameters in your query strings using the $xxx syntax which enables the query engine to substitute values in the query at runtime.