# Finding paths between array of records

**URL:** <https://community.neo4j.com/t/finding-paths-between-array-of-records/75582>\
**Category:** Neo4j Graph Platform\
**Tags:** apoc, cypher\
**Created:** [October 1, 2025, 3:21pm UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582 "2025-10-01T15:21:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![knirirr](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/knirirr/32/32014_2.png) [@knirirr](https://community.neo4j.com/u/knirirr)\
**Post date:** [October 1, 2025, 3:21pm UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582/1 "2025-10-01T15:21:05Z")

</div>

I my Neo4j database I can find everything linked to a starting record, out to a particular depth, with the following query:

`MATCH (r:Record {record_id: 156}) CALL apoc.path.expand(r, null, null, 0, 2) YIELD path RETURN path, length(path) AS hops ORDER BY hops;`

This produces a graph like the following:

 ![Screenshot From 2025-10-01 16-09-32](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/f/e/fed76f310278e2ec75a7c02ebdd8ef8c58d584ca.jpeg)

This is all fine, but I now have a problem of going the other way, i.e. starting with an array of all the record\_ids from a graph such as the one above and needing to fill in all the paths.

The reason for this is that elsewhere in my application users are performing a variety of searches incrementally and merging the results together. The records they end up with will most likely have some links between them and they want to see them displayed as a graph.

Can anyone suggest where to start, please?

---

<div class="post-metadata">

**Author:** ![Ari\_Neo4j](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ari_neo4j/32/34352_2.png) [@Ari\_Neo4j](https://community.neo4j.com/u/Ari_Neo4j)\
**Post date:** [October 3, 2025, 3:55pm UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582/2 "2025-10-03T15:55:10Z")

</div>

Hi - I see you have not gotten a reply so I can see if I can ask an internal team for some insight.

You’ve already mentioned using `apoc.path.expand`, so we know APOC is in play. With the details below, I'll know whether this is best handled by the APOC team, Cypher team, GDS, or Support and see what I can do.

1. **Which Neo4j edition are you running?** (Community, Enterprise, or Aura)
2. **What Neo4j version are you on?** (e.g., 5.20, 4.4 LTS)
3. **About how big is your dataset?** (dozens, thousands, or millions of nodes/relationships)
4. **Where are you running this query?** (Neo4j Browser, Cypher Shell, or through an application/integration like Spring, GraphQL, Python, etc.)

---

<div class="post-metadata">

**Author:** ![knirirr](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/knirirr/32/32014_2.png) [@knirirr](https://community.neo4j.com/u/knirirr)\
**Post date:** [October 7, 2025, 10:17am UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582/3 "2025-10-07T10:17:15Z")

</div>

Thanks for getting back to me. The answers are as follows:

1. Community.
2. Various versions, e.g. 2025.02.0 and 2025.09.0 depending on the system.
3. Thousands.
4. I’m making calls to the http interface of a Neo4j server.

This is part of a larger application where the data are stored in PostgreSQL, but some parts are copied into Elasticsearch and Neo4j for specific searching needs. The example APOC graph I gave above, for example, is for a part of the application where users plot a graph of relationships between our records from a given starting point.

The problem here is that users will get their record\_ids of interest by performing other searches, e.g. via Elasticsearch, and will want to come back to Neo4j to plot a graph of what they’ve found.

---

<div class="post-metadata">

**Author:** ![pierre.halftermeyer](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pierre.halftermeyer/32/18169_2.png) [@pierre.halftermeyer](https://community.neo4j.com/u/pierre.halftermeyer)\
**Post date:** [October 7, 2025, 6:31pm UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582/4 "2025-10-07T18:31:10Z")

</div>

Hi if you want to see all records with record\_id in $record\_ids, you can try:  
`MATCH (n:Record)`  
`WHERE n.record_id IN $record_ids`  
`OPTIONAL MATCH (n)-[r]->(m)`  
`WHERE m.record_id IN $record_ids`  
`RETURN n, r, m`

---

<div class="post-metadata">

**Author:** ![knirirr](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/knirirr/32/32014_2.png) [@knirirr](https://community.neo4j.com/u/knirirr)\
**Post date:** [October 8, 2025, 12:17pm UTC](https://community.neo4j.com/t/finding-paths-between-array-of-records/75582/5 "2025-10-08T12:17:01Z")

</div>

> [@pierre.halftermeyer](#):
>
> `MATCH (n:Record)`  
> `WHERE n.record_id IN $record_ids`  
> `OPTIONAL MATCH (n)-[r]->(m)`  
> `WHERE m.record_id IN $record_ids`  
> `RETURN n, r, m`

Thanks! It looks like that’s doing just what I need. Here’s what I got from an array of IDs of records I know to be linked:

 ![Screenshot From 2025-10-08 13-15-54](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/1/11f794225b8d48801f5ff967c2c784664a748424.png)
