Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apoc.refactor.mergeNodes choose the result properties randomly #115

Closed
neo-technology-build-agent opened this issue Sep 1, 2022 · 4 comments
Labels

Comments

@neo-technology-build-agent
Copy link
Collaborator

Issue by PeterKazmirsky
Tuesday Feb 19, 2019 at 15:58 GMT
Originally opened as neo4j-contrib/neo4j-apoc-procedures#1107


Guidelines
Imagine that I have user cluster consisting of user ids from different data sources and all users are connected to one Root node. In case that user cluster contains more than one Root node I want to merge this Root nodes to one. For finding the Root nodes in cluster I am using apoc.path.subgraphNodes and the resulting Root nodes I am sending to apoc.refactor.mergeNodes to merge them to one Root node. I also set properties to procedure: mergeRels:true, properties:'overwrite'

Actual Behavior
I cannot influence in what order roots get into procedure apoc.refactor.mergeNodes, so after merging the result Root node and his relationships properties will have random properties.

Expected Behavior
I would like you to consider to add feature to apoc.refactor.mergeNodes where I can tell for example that for merging property "count" choose highest one. Or for "date" property choose latest one.
Thank you.

Versions
Neo4j 3.5.2
Apoc library 3.5.0.1

@neo-technology-build-agent
Copy link
Collaborator Author

Comment by jexp
Tuesday Feb 19, 2019 at 18:16 GMT


MergeNodes uses collection order
You can use apoc.coll.sort to sort by a key you like

@neo-technology-build-agent
Copy link
Collaborator Author

Comment by PeterKazmirsky
Wednesday Feb 20, 2019 at 07:46 GMT


Thanks. But what about relationships. I recently created ticket where I asked to overwrite properties in relationships in procedure mergeNodes. And now I would like to sort properties of relationships and choose the proper one before merging. Is there any solution for that as well ?

@neo-technology-build-agent
Copy link
Collaborator Author

Comment by vga91
Thursday Jun 24, 2021 at 15:06 GMT


With a simple dataset like:

create (r:Root), (r2:Root) with r, r2
create (r2)<-[:MyRel {prop: "prop1"}]-(n1:User{value:'node1'})-[:MyRel {prop: "prop11"}]->(r)
create (r2)<-[:MyRel {prop: "prop2"}]-(n2:User{value:'node2'})-[:MyRel {prop: "prop22"}]->(r)
create (r2)<-[:MyRel {prop: "prop3"}]-(n3:User{value:'node3'})-[:MyRel {prop: "prop33"}]->(r)

This could be solved in this way:

Match (r:Root) 
with collect(r) as nodes    // I CAN ORDER NODES HERE
CALL apoc.refactor.mergeNodes(nodes, {properties:'overwrite'}) YIELD node // NO mergeRels:true
WITH node 
match (node)-[r]-(user:User)
with user, collect(r) as rels   // I CAN ORDER RELS HERE
call apoc.refactor.mergeRelationships(rels, {properties: 'DISCARD'}) yield rel return rel
// with {properties: 'DISCARD'} we choose the first in order of appearance, otherwise the last

@gem-neo4j
Copy link
Contributor

Closing as a workaround was produced and there has been no further comment in over 2 years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants