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

fixes #600 - add a configuration option to mergeNodes that allows to …combine relationships of the same type and direction #850

Merged
merged 1 commit into from
Jul 21, 2018

Conversation

AngeloBusato
Copy link
Contributor

@jexp for merging rels, procedure mergeNodes read the config parameter mergeRels: true/false.

If relationships have same start and end nodes are merged into one and properties combined.

ex. with this data set (we want to merge nodes :Person):
apoc refactor mergenodes createdatasetfirstexample

we have
apoc refactor mergenodes resultfirstexample


if relationships have different end or start nodes (related to direction) are maintained both and properties combine into all relationships

ex. with this data set (we want to merge nodes :Person):

apoc refactor mergenodes createdatasetsecondexample

we have:
apoc refactor mergenodes resultsecondexampledata

apoc refactor mergenodes resultsecondexample

if (delete) source.delete();
PropertiesManager.mergeProperties(properties, target, conf);

//check mergeRels parameter
Object value = config.get("mergeRels") == null ? false : config.get("mergeRels");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really weird, that info should come cleanly out of RefactorConfig please move it there and add a dedicated method

//check mergeRels parameter
Object value = config.get("mergeRels") == null ? false : config.get("mergeRels");
if(value.equals(true)) {
Map<String,Object> map = new HashMap<String, Object>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

singletonMap

if (delete) source.delete();
PropertiesManager.mergeProperties(properties, target, conf);

//check mergeRels parameter
Object value = config.get("mergeRels") == null ? false : config.get("mergeRels");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOrDefault -> in refactorConfig



public void mergeRelsWithSameTypeAndDirectionInMergeNodes (Node node, RefactorConfig config, Direction dir){
List<Relationship> rels = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is really weird, please fix it
use foreach
iterate over the iterator

node.getRelationships(dir).iterator().forEachRemaining(rels::add);
for (int i = 0; i < rels.size()-1; i++) {
for (int j = i + 1; j < rels.size(); j++) {
if (rels.get(i).isType(rels.get(j).getType())){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't call rels.get() repeatedly, use local variables.
they should have the same start/end-node given that you iterate starting from that one

Map<String,Object> params = null;

testCall(db, "MATCH (a1:ALabel {name:'a1'}), (a2:ALabel {name:'a2'}), (a3:ALabel {name:'a3'}), (a4:ALabel {name:'a4'}) " +
" WITH head(collect([a1,a2,a3,a4])) as nodes CALL apoc.refactor.mergeNodes(nodes,{properties:\"combine\",mergeRels:true}) yield node return node",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use single quotes

" (a2:ALabel {name:'a2'})-[:HAS_REL{p:'r2'}]->(b1)," +
" (a3:ALabel {name:'a3'})<-[:HAS_REL{p:'r3'}]-(b1)," +
" (a4:ALabel {name:'a4'})-[:HAS_REL{p:'r4'}]->(b4:BLabel {name:'b4'})");
Map<String,Object> params = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this form?

" (n1)-[:DRIVE{since:2017}]->(n5)," +
" (n2)-[:HAS{since:2013}]->(n6)");

testCall(db, "MATCH (a1:Person{name:'John'}), (a2:Person {name:'Tom'})" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this statement is werid.

row -> {
assertTrue(row.get("node") != null);
assertTrue(row.get("node") instanceof Node);
Node resultingNode = (Node)(row.get("node"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just cast it

assertEquals(4,resultingNode.getDegree(Direction.OUTGOING));
assertEquals(1,c.getDegree(Direction.INCOMING));
assertEquals(true, r.isType(RelationshipType.withName("WORKS_FOR")));
assertEquals(Arrays.asList( 2015L , 2018L).toArray(), new ArrayBackedList(r.getProperty("since")).toArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just two lists or Sets

@AngeloBusato AngeloBusato force-pushed the issue_600 branch 2 times, most recently from 346fdad to 789f61b Compare July 19, 2018 08:24
@@ -335,12 +336,19 @@ public void categorize(
});
}

private Node mergeNodes(Node source, Node target, boolean delete, RefactorConfig conf) {
private Node mergeNodes(Node source, Node target, boolean delete, Map<String, Object> config) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change RefactoringConfig to Map here? and then that map is not even used in the method?

…at allows to combine relationships of the same type and direction
@AngeloBusato
Copy link
Contributor Author

@jexp pr updated

@jexp
Copy link
Member

jexp commented Jul 21, 2018

Thanks a lot

@jexp jexp merged commit e1be94d into neo4j-contrib:3.4 Jul 21, 2018
jexp pushed a commit that referenced this pull request Jul 23, 2018
…combine relationships of the same type and direction (#850)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants