This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

**Head's Up!** Site migration is underway.
Phase 2: migrate recent content

- Neo4j
- Technical Discussions
- Neo4j Graph Platform
- Check if two nodes are directly connected in cyphe...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Check if two nodes are directly connected in cypher

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

07-20-2022 02:17 AM

Hi everyone,

I'm currently starting to learn how Cypher works and I got stuck trying to find if 2 specific nodes are connected directly. I want to get 1 if the nodes are connected and 0 if they are not.

I write the following but this just find all the neigbors .

MATCH (n where ID(n)=1000)

CALL apoc.path.subgraphNodes(n, {maxLevel: 1}) YIELD node

RETURN node

Could anyone help me find a solution how to do this?

Thank you very much.

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

07-20-2022 03:12 AM - edited 07-20-2022 03:25 AM

If you want to find out if two specific nodes are connected directly (one hop), then you need to find both nodes and test for the existence of a relationships between them. The following query does that. It uses the 'exists' method to determine if a direct connection exists between nodes 'm' and 'n'. The result is either 'true' or 'false'.

```
match (n where ID(n)=1000)
match (m where ID(m)=1001)
return exists( (n)--(m) )
```

If you really want '0' or '1' instead, you can use a 'case' statement to map true/false to 1/0.

```
match (n where ID(n)=1000)
match (m where ID(m)=1001)
return case exists( (n)--(m) ) when true then 1 when false then 0 end
```

Or, a slightly different syntax:

```
match (n where ID(n)=1000)
match (m where ID(m)=1001)
with exists( (n)--(m) ) as isConnected
return case isConnected when true then 1 when false then 0 end
```

BTW, (n)--(m) is shorthand notation of (n)-[]-(m), which represents a direct connection between 'n' and 'm'.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

07-20-2022 04:07 AM

Thank you this works perfect for me

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

07-20-2022 07:30 AM - edited 07-20-2022 07:30 AM

Hello @mixalisnikolaou 😊

If the nodes are densely connected, you should use apoc.nodes.connected() function from the APOC plugin:

```
MATCH (n WHERE id(n) = 1000)
MATCH (m WHERE id(m) = 1001)
RETURN apoc.nodes.connected(n, m) AS output;
```

Regards,

Cobra

Nodes 2022

All the sessions of the conference are now available online

Related Content

- Duplicate Master Records issue in Neo4j Graph Platform
- Noe4j Graphql OGM with NodeJS: Model.create takes several minutes to execute in Drivers & Stacks
- Finding Paths between Node Communities in Neo4j Graph Platform
- Neo4j Enterprise deploy issue (ServiceUnavailable: WebSocket connection failure) in Neo4j Graph Platform
- importing csv data in Neo4j Graph Platform