Create a generalization node and corresponding relationships

Hello guys,

i have several graphs with the same structure (one central node called Perception_Group and several peripheric nodes called Perception). I need to compare Perception_Groups (PGs) by measuring distance of values in their peripheric perception nodes. If the difference between PGs is less than 0.2 i want to create a Generalization (G) node and connect all Perception_Group (PG) nodes that are similar to each other, by using a INSTANCEOF Relationship from each PG to the G node.

The following cypher creates several Generalization Nodes instead of just one for similar PGs.

Can you help me fix it or show me an alternative ?

Thanks

MATCH (pg1:Perception_Group)-[r:RELATED]-(p1:Perception)
WHERE ID(pg1) = 801
WITH pg1, p1
MATCH (pg2:Perception_Group)-[:RELATED]-(p2:Perception)
WHERE pg1 <> pg2 AND p1.name = p2.name
WITH pg1,pg2, sum(abs(p1.value-p2.value)) AS diff
WHERE diff < 0.2
WITH  pg1,pg2,diff
MERGE (pg1)-[:INSTANCEOF {subType:'UE5'}]->(:GENERALIZATION{date: datetime()})<-[:INSTANCEOF {subType:'UE5'}]-(pg2);

got this one working, but i feel there could be a shorter version and also how can i avoid duplication of the Generalization node everytime i run the query ?

MATCH (pg1:Perception_Group)-[r:RELATED]-(p1:Perception)
WHERE ID(pg1) = 801
WITH pg1, p1
MATCH (pg2:Perception_Group)-[:RELATED]-(p2:Perception)
WHERE pg1 <> pg2 AND p1.name = p2.name
WITH pg1,pg2, sum(abs(p1.value-p2.value)) AS diff
WHERE diff < 0.2
WITH  pg1,pg2,diff
MERGE (g:GENERALIZATION{date: datetime()})
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg2)
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg1);

trying this one to avoid "Generalization" nodes duplication in multiple runs, but not working.

MATCH (pg1:Perception_Group)-[r:RELATED]-(p1:Perception)
WHERE ID(pg1) = 801
WITH pg1, p1
MATCH (pg2:Perception_Group)-[:RELATED]-(p2:Perception)
WHERE pg1 <> pg2 AND p1.name = p2.name
WITH pg1,pg2, sum(abs(p1.value-p2.value)) AS diff
WHERE diff < 0.2
WITH  pg1,pg2,diff
OPTIONAL MATCH (g:Generalization {id:$id1})-[:INSTANCEOF]-(pg1)
WITH pg1,pg2,diff,g
CALL apoc.do.when(g is null, 'MERGE (g1:GENERALIZATION) RETURN g1', 'RETURN g', {id:$id1, g:GENERALIZATION}) YIELD value
WITH value.g as g, pg1,pg2
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg2)
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg1);

This query is working. Any suggestion on optimization ?

MATCH (pg1:Perception_Group)-[r:RELATED]-(p1:Perception)
WHERE ID(pg1) = 801
WITH pg1, p1
MATCH (pg2:Perception_Group)-[:RELATED]-(p2:Perception)
WHERE pg1 <> pg2 AND p1.name = p2.name
WITH pg1,pg2, sum(abs(p1.value-p2.value)) AS diff
WHERE diff < 0.2
WITH  pg1,pg2
OPTIONAL MATCH (g:Generalization)-[:INSTANCEOF]-(pg1)
WITH pg1,pg2,g
CALL apoc.do.when(g is null, 'MERGE (g:GENERALIZATION) RETURN g', 'RETURN g', {}) YIELD value
WITH value.g as g, pg1,pg2
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg2)
MERGE (g)<-[:INSTANCEOF {subType:'UE5'}]-(pg1);

This post was flagged by the community and is temporarily hidden.

Hi Chris,

it was not a trick and you shouldn't accuse me like that. As i posted 3 days ago in my 1st post i am a noob at Neo4j. So when i start a query, all seems so strange to me since i am not used to cypher queries (only recently knew about graph databases).

After i post the question, obviously i do my effort and keep looking for solutions as a plan B in case someone here doesn't answer me and also because in respect to everyone here i should progress in the problem as much as possible and not seem that i'm posting questions without making an effort.

It happened in the last posts that while i was waiting for someone to answer, i was able to achieve some degree of a solution, but since i am not an expert in neo4j (not even close) i post my findings and ask for comments to see if i am doing something wrong or i could do something different with a better performance.

Since i got a solution and no-one was answering my question i found it logical to mark it as a solution in case someone looking for an answer to a similar problem needed to know which was the answer i came up with.

I am not interested in earning points in this forum. I am only interested in making my system work and if got a solution i did not expect to get by myself i just share it.

Best Regards
José Salvador

Ps: i marked my answer as the solution instead of yours, because it contained the solution to the initial question. If you had posted a better solution i would have marked your answer as the solution. You didn't, but i thanked your effort in confiming i was doing things the right way

Sorry, man, I've been so direct. I've seen now you're as new as myself here, and it just happened I was the sole beneficiary of the "wasted time" on your last two threads :slight_smile: (I think we're both here in Pacific Time, when the forum is almost empty)

I hope you see my point too. I don't give a dime about earning points or whatever, I was just simply interested in what Cypher issues people have and to help , if possible.

Going forward, I think people should allow several good hours until they post their own solutions. Otherwise, some of us could simply duplicate your work... And yes, wasting time trying to just genuinely help.

regards,
-C

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

Hi guys!

THANK YOU, @cristiscu, for setting aside time to help others. Truly commendable and makes a huge difference for a lot of people struggle to find solutions to their problems. Are you part of the Ninja Program by chance? I have been brainstorming with some of our Ninjas to see how we can improve the "finding information" problems. Maybe you have some ideas? :heart:

@a10554, thank you for updating your question after you found a solution. As @cristiscu said, we do have people who try to help by answering, however, not everything can always get addressed in a timely manner and I can't blame you for looking to solve the problems yourself. I've personally done this myself in the past (posted questions, then found a solution). I should add, very often, people will get their questions answered but don't even mark any of the answers as a solution, which stinks!

I know how much cultural and language differences can make it easy to take things out of context because we work with such a diverse team. I'm sorry I wasn't able to jump in earlier when you first started discussing it but I'm glad you were able to discuss it amongst yourself.
I hope this experience doesn't deter either of you from asking questions or continuing to help others.

1 Like