Schakron
Schakron

Reputation: 167

Neo4j - understanding cypher querying

I'm quite new to Neo4j and Cypher. I have written the following query:

MATCH (tp: TPPRODUCT_VERSION {TPPRODUCTCODE: "Z1115"})
<-[:IS_TPBOMPARENT_OF]-(pv: PRODUCT_VERSION)
<-[:IS_BOMPARENT_OF*..]-(parent: PRODUCT_VERSION)
RETURN parent, pv, tp

Here is the result: http://postimg.org/image/ve76qy977/

Actually I was expecting to get all Product_Versions that have a [IS_TPBOMPARENT_OF] relation to Z1115 plus their parents, no matter if they have one or not.

But instead I only got these PRODUCT_VERSIONs which have parents all which don't seem to be ignored.

Upvotes: 1

Views: 88

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18022

So you probably want to use OPTIONAL MATCH.

Your query forces neo4j to match a certain pattern. If the pattern doesn't exist, then you don't get your matched data back. In this case you want all PRODUCT_VERSIONs, not just the ones that have parents, also the ones that don't.

So try this query instead:

MATCH (tp: TPPRODUCT_VERSION {TPPRODUCTCODE: "Z1115"})<-[:IS_TPBOMPARENT_OF]-(pv: PRODUCT_VERSION)
OPTIONAL MATCH (pv)<-[:IS_BOMPARENT_OF*..]-(parent: PRODUCT_VERSION)
RETURN parent, pv, tp

This should return tp, and all pv objects. But then it will also try to match further from the pv objects to the parent objects, and return them if they exist. But if they don't exist, you should still get pvs that don't have a parent.

Upvotes: 3

Related Questions