'Neo4J Node's ego network with python

I'm working with Neo4J for the first time and I need to output the ego network of a node whose name was given in input. I'm using python and neo4j library to run queries into my graph.

enter image description here

I'm sorry for the colors, but it's the only way to explain the scenario. The blue node is the input node and the graph you see is the output of Neo4J of the blue node's ego network and it is exactly what I want to achieve in my query. The green edges are the nodes directly connected to the blue, yellow edges are self-relationships and red edges are relationships between the node's neighbours.

With this query I only achieve the green relationships:

MATCH p=(node {name:$name})-[*1]-(othernodes) 
with *, relationships(p) as r  
RETURN node,r,othernodes

I modified this query and I achieved also the yellow relationships (thanks to the comment below)

MATCH p=(node {name:$name})-[*1]-(othernodes) 
with *, relationships(p) as r  
MATCH s=(othernodes)-[*0..1]-(othernodes)
with *, relationships(s) as e
RETURN node,r,e,othernodes

But I'm still missing the correct query to achieve also the red edges. Can you help me out with this? Thanks in advance.



Solution 1:[1]

When you write this query below,

MATCH s=(othernodes)-[*1]-(othernodes)

it means you want othernodes which has an edge onto itself. So it will not include those nodes in gray. If you want to include the nodes (in gray) on the result, then change it to:

MATCH s=(othernodes)-[*0..1]-(othernodes)

which means give me othernodes that has zero or a relationship (edge) onto itself.

Edited: due to comments below

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1