Roger
Roger

Reputation: 657

If specific relation exist, only return this one

I am new to Neo4j. I have a graph that represent all RPMs installed on a Host, so I have one host node and multiple RPMs node with a "installed in" relation between them. To go with that, I also have some RPM->RPM relationships for RPMs dependencies. So it is a "Depends on" relationship between two RPMs node.

I want to display my whole graph (with a limit) on the Neo4j browser. But because the dependency is also installed on the host, the "installed in" relationship is still being displayed for dependencies and connect to the Host node. That makes the whole graph much harder to read.

I would like to keep only the "depends on" relation for dependencies and mask the "installed in" relation. I will display the "installed in" relation only for top level RPMs that are not a dependency for another RPM.

Is it possible to have a query that says something like that?

Edit:

I have a lot of data (from an elasticsearch), many RPMs and only one host, I would like the host to be always present on the graph no matter the LIMIT I set (basically I want the host to be the first node)

Upvotes: 2

Views: 67

Answers (1)

stdob--
stdob--

Reputation: 29167

1) Add test data:

CREATE (N:Host)
CREATE (P1:RPM)
CREATE (P2:RPM)
CREATE (P3:RPM)
CREATE (P1)-[r1:`installed in`]->(N)
CREATE (P2)-[r2:`installed in`]->(N)
CREATE (P3)-[r3:`installed in`]->(N)
CREATE (P2)-[r4:`depends on`]->(P3)
RETURN *

2) And use CASE:

MATCH (P:RPM)-[r1:`installed in`]->(:Host)
OPTIONAL MATCH (P)-[r2:`depends on`]->(:RPM)
WITH CASE WHEN r2 IS NULL 
          THEN r1 
          ELSE r2 
     END as relation
RETURN startNode(relation) as rpmNode,
       relation,
       endNode(relation) as relatedNode

Upvotes: 2

Related Questions