# Is it possible to filter returned relationships by their properties?

**URL:** <https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139>\
**Category:** Neo4j Graph Platform\
**Tags:** cypher\
**Created:** [January 7, 2022, 11:06pm UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139 "2022-01-07T23:06:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neverstew](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/neverstew/32/21733_2.png) [@neverstew](https://community.neo4j.com/u/neverstew)\
**Post date:** [January 7, 2022, 11:06pm UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139/1 "2022-01-07T23:06:18Z")

</div>

Hello! I was hoping that I could solve a problem of extracting one of many possible paths out of neo4j but I'm starting to think this isn't possible. I'd really appreciate any help/pointers.

## Goal

We have a few "Place" nodes constructed in the test data below. "People" visit "Place"s one at a time and in doing so, create a parent-child relationship between the places. Each person will have a different path through the places. I want to extract each person's path separately.

## Example

Set up some data

```auto
with [{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 }] as nodes
unwind nodes as node
create (p:Place) set p += node;

```

```auto
match (one {id: 1}) 
match (two {id: 2})
match (three {id: 3})
match (four { id: 4 })
match (five { id: 5})
merge (one)<-[:follows { person: 1 }]-(two)
merge (two)<-[:follows { person: 1 }]-(four)
merge (four)<-[:follows { person: 1 }]-(three)
merge (three)<-[:follows { person: 1 }]-(five)
merge (one)<-[:follows { person: 2 }]-(two)
merge (two)<-[:follows { person: 2 }]-(three)
merge (three)<-[:follows { person: 2 }]-(four)
merge (four)<-[:follows { person: 2 }]-(five)

```

With the following query, I'm expecting edges to not be returned if they have a `person` property that is not 1. This doesn't happen - all edges are returned between the matching nodes.

```sh
match (tail:Place)
where not exists {
 match (tail)<-[:follows {person: 1}]-()
}
with tail
match p=(tail)-[*1..2 { person: 1 }]-()
return p

```

Is this possible?

---

<div class="post-metadata">

**Author:** ![tfontanella011](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tfontanella011/32/16879_2.png) [@tfontanella011](https://community.neo4j.com/u/tfontanella011)\
**Post date:** [January 8, 2022, 10:56pm UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139/2 "2022-01-08T22:56:52Z")

</div>

Hi @neverstew,

You can try using two hops as in the query below:

```auto
match (tail:Place)
where not exists {
match (tail)<-[:follows {person: 1}]-()
}
with tail
MATCH p=(tail)-[{ person: 1 }]-()-[{ person: 1 }]-()
return p

```

Hope this helps

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 9, 2022, 4:19am UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139/3 "2022-01-09T04:19:19Z")

</div>

I you are viewing the results in Desktop and looking at the graph, you will see all relationships between the matching nodes. In your case, it shows edges with 'person: 2' attribute. Instead, look at the 'Table' or "Text' views. These show the results of your query. You will notice that your results in these two views only contains edges with 'person: 1' attribute. The following is the 'text' view result.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/8/08992e22fae2723493ce73e1d3963d6fcf8212d0.png)

Just a note, you could simplify your data creation script with the following:

unwind range(1,5) as index  
create (p:Place) set p.id=index

---

<div class="post-metadata">

**Author:** ![neverstew](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/neverstew/32/21733_2.png) [@neverstew](https://community.neo4j.com/u/neverstew)\
**Post date:** [January 9, 2022, 3:38pm UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139/4 "2022-01-09T15:38:18Z")

</div>

Thanks for this very helpful answer, @glilienfield . You're right, the query does what I expected though the browser was throwing me off.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 9, 2022, 3:44pm UTC](https://community.neo4j.com/t/is-it-possible-to-filter-returned-relationships-by-their-properties/50139/5 "2022-01-09T15:44:38Z")

</div>

that is great...glad to help. Also, you can simplify your query, unless you have a specific reason for its structure:

match p=(tail:Place)-[\*1..2 { person: 1 }]-()  
where not exists {  
match (tail)\<-[:follows {person: 1}]-()  
}  
return p
