cancel
Showing results for 
Search instead for 
Did you mean: 

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

Getting nearest neighbours with the same value of relationship property

Michel
Node Clone

Hi,

I want to find the nearest  neighbours with the same value of relationship property.  Eg. the relationship PARENT has the property "color" and that property has values as "yellow", "green" and "red". So I want to find  the the nearest  neighbours only with the same color.

I have already wrtite some code, which work fine. But that code return nodes with mixed  relationship colors. 

Question: How to expand the code to implement thatt restriction ? Perhaps a for each construnt for each value of color ?

Thanks in advance,

Michel

THis question is related to How to get the first node from relationship with property value 

 

match (p1:MAGIC{aprop:'xyz'})
CALL apoc.path.expandConfig(p1, {
	relationshipFilter: "<PARENT",
    labelFilter: "/MAGIC",limit:1
})
yield path1 

SET p1.beforeprop = last(nodes(path1)).aprop
==========================================================
match (p2:MAGIC{aprop:'xyz'})
CALL apoc.path.expandConfig(p2, {
	relationshipFilter: "PARENT>",
    labelFilter: "/MAGIC",limit:1
})
yield path2 

SET p2.afterprop = last(nodes(path2)).aprop

 

 

2 REPLIES 2

glilienfield
Ninja
Ninja

I don't fully understand the requirement, but I will assume that both the relations property 'color' and the related node's property 'color' are both lists of colors, and that you want related nodes that have the identical list of colors.  If so, try the following:

match (n:MAGIC{aprop:'xyz'})
match p = (n)-[r:PARENT]-(p:MAGIC)
where all(x in r.color where x in p.color)
and size(r.color) = size(p.color)
return p

Hi @glilienfield ,

thanks for your effort.  Sorry, the requirements were not enough precise: Only the color of relationship shold be the same.  What I need the nearest neighbours in both directions. Could you please extend your code for getting  both neighbours ? In only one query, not using two queries (s. my code).

Thanks in advance,

Michel