I have term nodes connected to content nodes and a query that is meant up update a content nodes connections to these term nodes.
First I decrease the count of connected content nodes for each term node originally attached, then delete the relationships.
After that I create a new relationship to all the specified term nodes, attempting to increase the count of connected content nodes for each newly connected term node by one.
The problem is, after the query runs, the count of connected content nodes is not increased by one, but rather increased by what looks like the total number of new term nodes being connected.
It seems I'm still having trouble grasping exactly how the data is being handled behind the query. I suspect the answer may deal with doing a count of the connected nodes as has been the case previous when I've gotten stuck.
Here is the query:
var query = [
"MATCH (contentNode:content {UUID: {contentID} })-[r:TAGGED_WITH]->(oldTermNode:term) ",
"SET oldTermNode.contentConnections = oldTermNode.contentConnections - 1 ",
"DELETE r ",
"WITH contentNode ",
"MATCH (newTermNode:term) ",
"WHERE newTermNode.UUID IN {termIDs} ",
"CREATE UNIQUE contentNode-[:TAGGED_WITH]->newTermNode ",
"SET newTermNode.contentConnections = newTermNode.contentConnections + 1 ",
].join('\n');
As a side question, when updating the terms, often many of the new terms are the same as the old terms (the user only adds/removes one or two terms, leaving the rest the same). Would it make more sense/have faster performance if only the relationships that wouldn't be reconnected were deleted and then only the new terms added?
Thanks a lot.
WITH
, so the second part of the query is executed multiple times. – jjaderbergWITH distinct contentNode
you'll collapse that cardinality back to one (1) and discard all the other paths. – Michael Hunger