Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-18-2019 12:37 AM
I have a database that consists solely of trees. I would like to query and retrieve all found nodes, but also all nodes below the found nodes, as well as the properties of the attached child nodes and the relationship types those nodes are connected with the originally found nodes. I do this with this query:
MATCH (A:MyType1), Ap = (A)-[ * ]->(Ax)
WHERE A.myID = '123'
RETURN A as e, Ax as ex, Ap as ep,
extract(r in relationships(Ap) | type(r)) as et,
extract(n in nodes(Ap) | labels(n)) as en
This works fine. It gives me a table containing packages of found nodes A. Each package contains the node A and the child nodes Ax as well as the list of relationship types the subnode Ax is connected to A and the list of node types that are traversed to reach Ax.
However, when I further constrain my query, so that the nodes A have to match a desired pattern, my approach does not work any more. With this query:
MATCH (A)-[ * ]->(B:MyType), Ap = (A)-[ * ]->(Ax)
WHERE A.myID = '123'
RETURN A as e, Ax as ex, Ap as ep,
extract(r in relationships(Ap) | type(r)) as et,
extract(n in nodes(Ap) | labels(n)) as en
in my current database I find the same original node A, but the package of returned child nodes is much smaller than before. Why does the enlargement of the pattern constraining A reduce the amount of nodes Ax (or paths Ap) that are matched ? And how can write a Cypher query that still does what I am intending to do ?
Greetings
Georg
12-18-2019 08:11 AM
In your queries, you use the same variable A for multiple matches. Can you rewrite it so that you have no ambiguity with respect what A should be?
For example:
MATCH (A:MyType1), Ap = (A)-[ * ]->(Ax)
WHERE A.myID = '123'
RETURN A as e, Ax as ex, Ap as ep,
extract(r in relationships(Ap) | type(r)) as et,
extract(n in nodes(Ap) | labels(n)) as en
should be rewritten as:
MATCH Ap = (A:myType1)-[ * ]->(Ax)
WHERE A.myID = '123'
RETURN A as e, Ax as ex, Ap as ep,
extract(r in relationships(Ap) | type(r)) as et,
extract(n in nodes(Ap) | labels(n)) as en
Then have the second query also use variables exclusively?
Elaine
12-19-2019 12:41 AM
I used the same variable A because I intended it to be the same node. I thought that when in the same MATCH the same variable is used multiple times, then the MATCH-clauses also identifies it as the same node. I would have thought that:
MATCH (A)-->(B), (B)-->(C)
is the same as
MATCH (A)-->(B)-->(C)
Is this not correct and a misunderstanding on my side ?
Greetings
Georg
01-06-2020 03:20 PM
Those should be functionally equivalent, yes.
Now looking at the differences in your two queries at the start, it looks like only the initial pattern for A
nodes is different.
In your original:
(A:MyType1)
And in the newer version that isn't working for you:
(A)-[ * ]->(B:MyType)
You say there is an enlargement of the pattern, but there is no guarantee here that the A nodes from the second query will be superset of the nodes found in the first query.
For one you're using different labels between these (MyType1
in the original vs MyType
in the second), and second we don't even know what labels of nodes will be found in the second pattern...we don't know if they include nodes of type :MyType1, only that they are connected to nodes of :MyType.
Also if you wanted to ensure the A nodes could also include the B
nodes in the pattern, you could use (A)-[ *0.. ]->(B:MyType)
, but I'm not sure if this is something you want.
All the sessions of the conference are now available online