How do to get all relations and nodes related to some node?

For example, we have the following relations: User>Orders>Product>Producer. It will be nice to find a Product by name or id(it does not matter how exactly) and get all nodes and relations related to this Product in any direction and depth.

As the result, we should get a list of orders with this Product and the Users who actually bought them. Also, this approach should be schema-agnostic, because I do not know how many types of nodes and relations it will be and do not want to change the query each time when a new type appears. Approximately it will be 130 different kinds of nodes with more than 290 relations between them.

Why would you have multiple hops between a product and an order? I would think an order would be directly related to a group of products that comprise the order. The following query should work for the simple data model. Modify the return statement to return the exact properties you require.

match(product:Product{id:0})
match(product)<-[:CONTAINS_PRODUCT]-(order:Order)<-[:HAS_ORDER]-(user:User)
return order, user

If you want a subgraph originating from a Product (or any) node, then the APOC path procedures should fit your needs.

Try this:

match (a) where id(a) = 100

match (a)-[r]-(b) where id(b) <> id(a)

with labels(a) as lbl, collect(distinct type(r)) as rels, labels(b) as lbl2

return lbl, rels, lbl2

If you want to do it in pure cypher, the following query will find all the paths of any length origination from the identified Product node. It returns the list of neo4j assigned 'id's for each node, and a list of relationships containing the relationship's neo4j assigned 'id' and the 'id' of the relationship's start and end node. You can modify the query to return any properties of the nodes and relationship to meet your needs.

match(product:Product{id:0})
match p=(product)-[*]-()
with nodes(p) as nodes, relationships(p) as relationships
unwind nodes as node
with collect(distinct id(node) ) as nodeIds, relationships
unwind relationships as relationship
return nodeIds, collect(distinct {id: id(relationship), startNodeId: startNode(relationship), endNodeId: endNode(relationship)}) as relationships

The approach should work well for a graph that looks like a star, i.e. paths originating from the single Product node. It will get less efficient the more the paths share comment subparts. For more complicated subgraphs, one of the APOC 'path' procedures may perform.

This was kind of a general solution. We can address any specific requirements if you have something specific you would like to achieve. Does this answer your question?

This is only an example. My question is how to get all relations and nodes if I am unsure about the relation name or any order.

To be honest, I know all possible relations in the system, but I do not know which of them is attached to some specific node.